Skip to main content
 首页 » 编程设计

python之在 Python 中裁剪和保存 TIFF 文件

2025年05月04日8kevingrace

我是 python 编码的新手。我一直在尝试裁剪 X 射线生成的一系列 .tif 图像。现在,它成功裁剪并显示生成的图像,但我无法保存它们。保存后的图像全黑。我使用的代码行如下:

    from PIL import Image 
    import numpy 
    startx = 421 
    starty = 118 
    stopx = 1182 
    stopy = 336 
    startfile = 1722 
    stopfile = 1951 
    for i in range(startfile,stopfile+1): 
        image = Image.open("Filepath"+str(startfile)+".tif") 
        cropped_image = image.crop((startx,starty,stopx,stopy)) 
        print("ipp"+str(startfile)+" cropped") 
        image1 = cropped_image.rotate(180,0,1) 
        image1.save("Filepath"+str(startfile),".tif") 
        print("ipp"+str(startfile)+" saved") 
        startfile = startfile + 1 
        starty = starty + 3 
        stopy = stopy + 3 

需要帮助!

请您参考如下方法:

我认为错误在于:

image1.save("Filepath"+str(startfile),".tif") 

第一个参数应该是文件名,包括扩展名。第二个参数是可选的,并给出了格式。您提供的文件名没有扩展名。这一行应该是:

image1.save("Filepath"+str(startfile)+".tif")