PIL图像对象上的Python复制
发布于 2021-01-29 16:11:31
我正在尝试创建一组缩略图,每个缩略图分别从原始图像缩小。
image = Image.open(path)
image = image.crop((left, upper, right, lower))
for size in sizes:
temp = copy.copy(image)
temp.thumbnail((size, height), Image.ANTIALIAS)
temp.save('%s%s%s.%s' % (path, name, size, format), quality=95)
上面的代码似乎工作正常,但是在测试时我发现一些图像(我无法分辨出它们的特殊之处,也许仅适用于PNG)会引发此错误:
/usr/local/lib/python2.6/site-packages/PIL/PngImagePlugin.py in read(self=<PIL.PngImagePlugin.PngStream instance>)
line: s = self.fp.read(8)
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'read'
没有copy()
这些图像,就可以正常工作。
我可以为每个缩略图重新打开并重新裁剪图像,但是我希望有一个更好的解决方案。
关注者
0
被浏览
167
1 个回答
-
我猜
copy.copy()
对于PILImage
类不起作用。尝试使用Image.copy()
它,因为它存在是有原因的:image = Image.open(path) image = image.crop((left, upper, right, lower)) for size in sizes: temp = image.copy() # <-- Instead of copy.copy(image) temp.thumbnail((size, height), Image.ANTIALIAS) temp.save('%s%s%s.%s' % (path, name, size, format), quality=95)