[pillow]ValueError:read of closed file
anlondon Lv6

在使用pillow 7.1.1生成GIF图时出现了

1
2
3
File "C:\Users\MACHENIKE.DESKTOP-UL15BOR\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\ImageFile.py", line 549, in _safe_read
return fp.read(size)
ValueError: read of closed file

python 源码

1
2
3
4
5
6
7
path = os.getcwd() + '\\frames\\'    #获取当前路径
imgs = []
for i in png_list:
pic_name = path + i
temp = Image.open(pic_name)
imgs.append(temp)
imgs[0].save('output.gif',save_all=True,loop=True,append_images=imgs[1:],duration=500)

最后在githun:python-pillow找到了答案

实际上,此问题已由#4528修复,但尚未发布。
解决方法是使用frame.copy()

1
2
3
4
5
6
7
from PIL import Image
images = []
for i in range(5):
frame = Image.open('%05d.png' % i)
images.append(frame.copy())

images[0].save('animation.gif', save_all=True, append_images=images[1:], duration=100, loop=0)

算是一个bug吧,希望早点发布新版本

 Comments