Python 复制 PIL 图像对象
我正在尝试创建一组缩略图,每个缩略图都从原始图像中单独缩小。
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()
这些图像可以工作很好。
我可以为每个缩略图重新打开并裁剪图像,但我宁愿有一个更好的解决方案。
I'm trying to create a set of thumbnails, each one separately downscaled from the original image.
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)
The above code seemed to work fine but while testing I discovered that some images (I can't tell what's special about them, maybe only for PNG) raise this error:
/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'
Without the copy()
these images work just fine.
I could just open and crop the image anew for every thumbnail, but I'd rather have a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜
copy.copy()
不适用于 PILImage
类。尝试使用 Image.copy() 代替,因为它的存在是有原因的:I guess
copy.copy()
does not work for the PILImage
class. Try usingImage.copy()
instead, since it is there for a reason: