Python 枕头';s Image.open()内存错误,同时打开大tiff文件
我正在编写一个python脚本,该脚本将在服务器上运行,并将调整图像大小(即Indesign链接)。这一切都很好,除了当我试图打开更大的tiff文件(800 MB)时 我正在使用枕头的Image.open()来调整它的大小,但是我得到了MemoryError错误。我尝试使用其他库,如ImageMagick和tifffile,结果相同。我可以将它拆分成更小的块,调整大小,然后合并它们,但我不知道如何在不先打开文件的情况下实现这一点 我进行了广泛的搜索,但找不到适合我的情况的解决方案。我还观察了内存消耗,它似乎并没有不规则的高。可以肯定地说,我完全迷路了 整个轨迹如下所示:Python 枕头';s Image.open()内存错误,同时打开大tiff文件,python,image,tiff,pillow,Python,Image,Tiff,Pillow,我正在编写一个python脚本,该脚本将在服务器上运行,并将调整图像大小(即Indesign链接)。这一切都很好,除了当我试图打开更大的tiff文件(800 MB)时 我正在使用枕头的Image.open()来调整它的大小,但是我得到了MemoryError错误。我尝试使用其他库,如ImageMagick和tifffile,结果相同。我可以将它拆分成更小的块,调整大小,然后合并它们,但我不知道如何在不先打开文件的情况下实现这一点 我进行了广泛的搜索,但找不到适合我的情况的解决方案。我还观察了内存
Traceback (most recent call last):
File "app.py", line 87, in <module>
convert()
File "X:\Development\Python\zipper\converter.py", line 112, in convert
if saveAsJPEG(file, name + ".jpg"): converted_images = updateCounter(all_images, converted_images)
File "X:\Development\Python\zipper\converter.py", line 37, in saveAsJPEG
print Image.open(a).size
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2266, in open
im = factory(fp, filename)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 97, in __init__
self._open()
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 637, in _open
self._seek(0)
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 672, in _seek
self.tag.load(self.fp)
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 458, in load
data = ImageFile._safe_read(fp, size)
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 521, in _safe_read
block = fp.read(min(size, SAFEBLOCK))
MemoryError
您可以发布引发此错误的代码吗。Pillow的Image.open()在被调用后不应立即抛出,因为它只会在需要时将数据加载到内存中。请查看并投票支持我的答案,该答案使用了
VIP
@史蒂夫-我添加了代码。是的,我也认为它不应该,因为我在读一篇关于加载部分图像的帖子(1999年):它使用image.open()然后加载部分图像。MarkSetchell-谢谢,我会看一看:)你能发布抛出这个错误的代码吗。Pillow的Image.open()在被调用后不应立即抛出,因为它只会在需要时将数据加载到内存中。请查看并投票支持我的答案,该答案使用了VIP
@史蒂夫-我添加了代码。是的,我也认为它不应该,因为我在读一篇关于加载部分图像的帖子(1999年):它使用image.open()然后加载部分图像。MarkSetchell-谢谢,我会看一看:)你能发布抛出这个错误的代码吗。Pillow的Image.open()在被调用后不应立即抛出,因为它只会在需要时将数据加载到内存中。请查看并投票支持我的答案,该答案使用了VIP
@史蒂夫-我添加了代码。是的,我也认为它不应该,因为我在读一篇关于加载部分图像的帖子(1999年):它使用image.open()然后加载部分图像。MarkSetchell-谢谢,我会看一看:)
def saveAsJPEG(file, newfile):
print "\nopening:", file
try:
with Image.open(file) as im:
if not im.size[0] < size[0] or im.size[1] < size[1]:
new_im = im.resize(size, Image.ANTIALIAS)
new_im.save(newfile, 'JPEG', quality=100, dpi=(72, 72))
# Force a memory dump. Otherwise memory will get cluttered up -> I am not sure if this is necessary as it doesn't seem to do anything.
del im
del new_im
collect()
return True
except:
print "image is too big -> try something else. But what?"
# this line below throws error (MemoryError). It was the same without try-except part before.
Image.open(file)
for file in images:
#gets extension
name = basename(splitext(file)[0])
ext = splitext(file)[1]
# check for extension type and run appropriate function
if ext == '.jpg' or ext == '.jpeg' or ext == '.tif' or ext == '.tiff':
if saveAsJPEG(file, name + ".jpg"): converted_images = updateCounter(all_images, converted_images)
del file, name, ext