Python copy on PIL image object(PIL图像对象上的Python副本)
问题描述
我正在尝试创建一组缩略图,每个缩略图都是从原始图像单独缩小的.
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)
上面的代码似乎可以正常工作,但是在测试时我发现一些图像(我不知道它们有什么特别之处,可能只适用于 PNG)会引发此错误:
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'
没有 copy()
这些图像就可以正常工作.
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.
推荐答案
我猜 copy.copy()
不适用于 PIL Image
类.尝试使用 Image.copy()
代替,因为它的存在是有原因的:
I guess copy.copy()
does not work for the PIL Image
class. Try using Image.copy()
instead, since it is there for a reason:
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)
这篇关于PIL图像对象上的Python副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PIL图像对象上的Python副本


- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01