&引用;错误:2“;使用Python图像库将TIFF图像转换为PNG时

&引用;错误:2“;使用Python图像库将TIFF图像转换为PNG时,python,python-imaging-library,png,tiff,Python,Python Imaging Library,Png,Tiff,我创建了一个批处理作业,可以打开文件夹中的.TIFF文件,调整其大小,将其转换为.PNG文件,然后将其保存到其他文件夹中。批处理作业运行正常,图片得到正确处理,但在某些特定图片(我可以将其作为普通.TIFF文件打开)处,进程会停止,并显示以下错误日志: --------------------------------------------------------------------------- OSError T

我创建了一个批处理作业,可以打开文件夹中的.TIFF文件,调整其大小,将其转换为.PNG文件,然后将其保存到其他文件夹中。批处理作业运行正常,图片得到正确处理,但在某些特定图片(我可以将其作为普通.TIFF文件打开)处,进程会停止,并显示以下错误日志:

    ---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-3-499452368347> in <module>
     47                 target_size = (target_x, target_y)
     48                 print("Image gets converted from size " + str(img_size) + " to " + str(target_size))
---> 49                 resized_image = image.resize(target_size, Image.BICUBIC)
     50 
     51                 # Save the image as a PNG to the target_dir

C:\EigeneProgramme\Python38-64\lib\site-packages\PIL\Image.py in resize(self, size, resample, box, reducing_gap)
   1897 
   1898         if self.mode in ["LA", "RGBA"]:
-> 1899             im = self.convert(self.mode[:-1] + "a")
   1900             im = im.resize(size, resample, box)
   1901             return im.convert(self.mode)

C:\EigeneProgramme\Python38-64\lib\site-packages\PIL\Image.py in convert(self, mode, matrix, dither, palette, colors)
    891         """
    892 
--> 893         self.load()
    894 
    895         if not mode and self.mode == "P":

C:\EigeneProgramme\Python38-64\lib\site-packages\PIL\TiffImagePlugin.py in load(self)
   1085     def load(self):
   1086         if self.tile and self.use_load_libtiff:
-> 1087             return self._load_libtiff()
   1088         return super().load()
   1089 

C:\EigeneProgramme\Python38-64\lib\site-packages\PIL\TiffImagePlugin.py in _load_libtiff(self)
   1189 
   1190         if err < 0:
-> 1191             raise OSError(err)
   1192 
   1193         return Image.Image.load(self)

OSError: -2
该文件夹在嵌套子目录系统中包含大约2100.tiff图像。其中149个未正确处理,但另一个处理得很好。我看不出正确处理的图片和导致错误的图片之间有什么区别

这是正确处理的图片的示例路径:
/Edge\u Bands\u Scan\u Sorted/woody\TIFF\W_E_B_NG\1475B_W_E_B_NG.TIFF


这是一个未正确处理的图片的示例路径:
/Edge\u Bands\u Scan\u Sorted/woody\TIFF\W\u E\u B\NG\3200B\u W\u E\u B\NG.TIFF

我不能完全确定这里发生了什么。我认为,但我并不确定,问题是文件每像素有4个样本(即RGBA),但“样本格式”标记仅给出4个样本中3个样本的类型(即无符号整数),这会打乱库

以下是突出显示矛盾字段的
tiffdump
输出:

tiffdump 3200B_W_E_B_NG.TIFF 
3200B_W_E_B_NG.TIFF:
Magic: 0x4949 <little-endian> Version: 0x2a <ClassicTIFF>
Directory 0: offset 9441002 (0x900eea) next 0 (0)
SubFileType (254) LONG (4) 1<0>
ImageWidth (256) LONG (4) 1<5477>
ImageLength (257) LONG (4) 1<1248>
BitsPerSample (258) SHORT (3) 4<8 8 8 8>
Compression (259) SHORT (3) 1<5>
Photometric (262) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1248<8 7558 15109 22684 30223 37769 45253 52740 60181 67699 75210 82702 90231 97784 105221 112698 120165 127581 135073 142615 150195 157794 165402 173003 ...>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<4>           <--- 4 SAMPLES PER PIXEL
RowsPerStrip (278) LONG (4) 1<1>
StripByteCounts (279) LONG (4) 1248<7550 7551 7575 7539 7546 7484 7487 7441 7518 7511 7492 7529 7553 7437 7477 7467 7416 7492 7542 7580 7599 7608 7601 7565 ...>
XResolution (282) RATIONAL (5) 1<1600>
YResolution (283) RATIONAL (5) 1<1600>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
Predictor (317) SHORT (3) 1<2>
ExtraSamples (338) SHORT (3) 1<2>
SampleFormat (339) SHORT (3) 3<1 1 1>          <--- ONLY THREE FORMATS BUT 4 SAMPLES

OSError 2表示文件名不存在。不确定您是否意识到,但不需要任何Python。您可以在带有ImageMagick的终端中这样做
magick mogrify-resize 1024x768-path OUTPUTDIRECTORY-format PNG*.TIFF
谢谢您的回答。这些文件之所以存在,是因为我使用python创建了一个文件列表,然后我在os.walk(source_dir)中循环了subdir、dir和files的文件
:对于filein files:#检查file是否是TIFF文件:#如果(file[-5:='.TIFF'或file[-5:='.TIFF'):if file.lower().endswith(“.TIFF”):file_path=os.path.join(子目录,文件)。。。image=image.open(文件路径)。。。resized_image=image.resize(target_size,image.BICUBIC)
由于某些原因,图像可能会被加载,并且在我尝试调整图像大小时出现错误。请显示您的代码,以及您正在处理的文件的长列表。@MarkSetchell谢谢我编辑了问题并添加了代码解决了我的问题,非常感谢你!
tiffdump 3200B_W_E_B_NG.TIFF 
3200B_W_E_B_NG.TIFF:
Magic: 0x4949 <little-endian> Version: 0x2a <ClassicTIFF>
Directory 0: offset 9441002 (0x900eea) next 0 (0)
SubFileType (254) LONG (4) 1<0>
ImageWidth (256) LONG (4) 1<5477>
ImageLength (257) LONG (4) 1<1248>
BitsPerSample (258) SHORT (3) 4<8 8 8 8>
Compression (259) SHORT (3) 1<5>
Photometric (262) SHORT (3) 1<2>
StripOffsets (273) LONG (4) 1248<8 7558 15109 22684 30223 37769 45253 52740 60181 67699 75210 82702 90231 97784 105221 112698 120165 127581 135073 142615 150195 157794 165402 173003 ...>
Orientation (274) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<4>           <--- 4 SAMPLES PER PIXEL
RowsPerStrip (278) LONG (4) 1<1>
StripByteCounts (279) LONG (4) 1248<7550 7551 7575 7539 7546 7484 7487 7441 7518 7511 7492 7529 7553 7437 7477 7467 7416 7492 7542 7580 7599 7608 7601 7565 ...>
XResolution (282) RATIONAL (5) 1<1600>
YResolution (283) RATIONAL (5) 1<1600>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
Predictor (317) SHORT (3) 1<2>
ExtraSamples (338) SHORT (3) 1<2>
SampleFormat (339) SHORT (3) 3<1 1 1>          <--- ONLY THREE FORMATS BUT 4 SAMPLES
from tifffile import imread

# Open with "tifffile"
img = imread('3200B_W_E_B_NG.TIFF')

# Make into "PIL Image" and carry on as usual
pi = Image.fromarray(img)