我目前正在使用pil调整文件夹中多个图像的大小,该文件夹包含多个不同格式的图像,如bmp、jpg和tif
我已经成功地实现了以下代码来调整bmp和jpg图像的大小,但是对于tif图像,我没有这样做
import shutil
import os
from PIL import Image
dir_name = "C:/Users/fyp/test1/"
test = os.listdir(dir_name)
destDirectory = "C:/Users/fyp/test2/"
for item in test:
if item.endswith(".tif"):
img = Image.open(dir_name + item)
imgResize = img.resize((224, 224), Image.ANTIALIAS)
imgResize.save(destDirectory + item[:-4] + '.tif', quality = 95)
tif图像中出现一个错误,即:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-8-b666efd7a46f> in <module>
10 if item.endswith(".tif"):
11 img = Image.open(dir_name + item)
---> 12 imgResize = img.resize((224, 224), Image.ANTIALIAS)
13 imgResize.save(destDirectory + item[:-4] + '.tif', quality = 95)
14
~\Anaconda3\lib\site-packages\PIL\Image.py in resize(self, size, resample, box, reducing_gap)
1852
1853 if self.mode in ["LA", "RGBA"]:
-> 1854 im = self.convert(self.mode[:-1] + "a")
1855 im = im.resize(size, resample, box)
1856 return im.convert(self.mode)
~\Anaconda3\lib\site-packages\PIL\Image.py in convert(self, mode, matrix, dither, palette, colors)
871 """
872
--> 873 self.load()
874
875 if not mode and self.mode == "P":
~\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py in load(self)
1068 def load(self):
1069 if self.use_load_libtiff:
-> 1070 return self._load_libtiff()
1071 return super().load()
1072
~\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py in _load_libtiff(self)
1180
1181 if err < 0:
-> 1182 raise OSError(err)
1183
1184 return Image.Image.load(self)
OSError: -2
我可以知道原因吗?多谢各位
更新日期:2021年1月8日
我想我找到了解决这个问题的办法。导致此错误的主要问题可能是由于某些图像正在被同时使用,并且python可能无法执行操作图像的函数。我重新启动电脑,问题就解决了。非常感谢您提供以下所有答案。请随时就任何其他可能的解决方案或原因发表更多评论
有一个神秘的错误。我找不到任何线索来解释原因。如果库不重要,可以使用
opencv
进行映像操作。我进行了测试,它适用于tiff文件装置
源代码
相关问题 更多 >
编程相关推荐