开发者

Python copy on PIL image object

开发者 https://www.devze.com 2023-03-21 18:57 出处:网络
I\'m trying to create a set of thumbnails, each one separately downscaled from the original image. image = Image.open(path)

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 bette开发者_JS百科r solution.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消