Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write a GIF file #4543

Closed
ehpor opened this issue Apr 10, 2020 · 5 comments
Closed

Cannot write a GIF file #4543

ehpor opened this issue Apr 10, 2020 · 5 comments

Comments

@ehpor
Copy link

ehpor commented Apr 10, 2020

What did you do?

Tried to write a GIF file with Image.save() with:

from PIL import Image

images = []

for i in range(5):
    frame = Image.open('%05d.png' % i)
    images.append(frame)

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

All images are valid PNG images that were written with Pillow.

What did you expect to happen?

Save the opened images to a GIF file.

What actually happened?

An error occurred:

Traceback (most recent call last):
  File ".\hcipy_issue67.py", line 9, in <module>
    images[0].save('animation.gif', save_all=True, append_images=images[1:], duration=100, loop=0)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2134, in save
    save_handler(self, fp, filename)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\GifImagePlugin.py", line 499, in _save_all
    _save(im, fp, filename, save_all=True)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\GifImagePlugin.py", line 510, in _save
    if not save_all or not _write_multiple_frames(im, fp, palette):
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\GifImagePlugin.py", line 430, in _write_multiple_frames
    for im_frame in ImageSequence.Iterator(imSequence):
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageSequence.py", line 49, in __next__
    self.im.seek(self.position)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py", line 748, in seek
    self._seek(f)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py", line 784, in _seek
    ImageFile._safe_read(self.fp, self.__prepare_idat)
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\ImageFile.py", line 549, in _safe_read
    return fp.read(size)
ValueError: read of closed file

What are your OS, Python and Pillow versions?

  • OS: Windows, Linux or Mac
  • Python: 3.5, 3.6 and 3.7
  • Pillow: 7.1.1 (the error does not occur with 7.0.0)

Error occurs in all my CI tests, see also ehpor/hcipy#67.

@radarhere radarhere added GIF and removed GIF labels Apr 10, 2020
@radarhere
Copy link
Member

This has actually already been fixed by #4528, but not released yet.

A workaround could be to use frame.copy().

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)

@ehpor
Copy link
Author

ehpor commented Apr 11, 2020

Thanks! I guessed it was something like that, but I couldn't figure out if it was fixed already, and I wasn't able to easily install Pillow from source on Windows due to the prerequisites (and the installation manual telling me "please don't"). I'll use the workaround for now.

@nulano
Copy link
Contributor

nulano commented Apr 11, 2020

@ehpor You can try #4495 to build on Windows (although it doesn't contain #4528, you will have to merge it yourself). It should make it easier to build on Windows once it is merged.

@radarhere
Copy link
Member

Pillow 7.1.2 has now been released with the fix for this.

@ehpor
Copy link
Author

ehpor commented Apr 26, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants