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

PIL.PngImagePlugin.PngImageFile images can't be saved as GIFs in 7.1.1 #4578

Closed
maxhumber opened this issue Apr 23, 2020 · 3 comments
Closed

Comments

@maxhumber
Copy link

What did you do?

Asset Download:

from urllib.request import urlopen
from PIL import Image

def download_and_save(url, name):
    im = Image.open(urlopen(url))
    im = im.resize((500, 293)).crop((0, 0, 500, 290))
    im.save(f'{name}.png')
    im.save(f'{name}.jpg')

download_and_save('https://www.nicepng.com/png/detail/50-503681_shaq-face-png-moniece-slaughter.png', 'shaq')
download_and_save('https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/close-up-of-cat-wearing-sunglasses-while-sitting-royalty-free-image-1571755145.jpg', 'cat')

JPG works:

shaq = Image.open('shaq.jpg')
cat = Image.open('cat.jpg')
type(shaq)
# PIL.JpegImagePlugin.JpegImageFile

shaq.save(
    'shaq_cat_jpg.gif',
    save_all=True,
    append_images=[cat],
    duration=200,
    loop=0
)

PNG doesn't work:

shaq = Image.open('shaq.png')
cat = Image.open('cat.png')
type(shaq)
# PIL.PngImagePlugin.PngImageFile

shaq.save(
    'shaq_cat_png.gif',
    save_all=True,
    append_images=[cat],
    duration=200,
    loop=0
)

What did you expect to happen?

Both JPG and PNG code should run.

What actually happened?

PNG code errors out with ValueError: read of closed file

What are your OS, Python and Pillow versions?

  • OS: macOS
  • Python: 3.7
  • Pillow: 7.1.1 (works on 7.0.0)

Both the PNG and JPG code run on 6.0.0 and 7.0.0, though!

@radarhere
Copy link
Member

This is actually a duplicate of #4543.

If you're interested in workaround until #4528 is released as part of the next Pillow, you could use copy() -

from PIL import Image
shaq = Image.open('shaq.png')
cat = Image.open('cat.png')
type(shaq)
# PIL.PngImagePlugin.PngImageFile

shaq.copy().save(
    'shaq_cat_png.gif',
    save_all=True,
    append_images=[cat.copy()],
    duration=200,
    loop=0
)

@maxhumber
Copy link
Author

Awesome! Looking forward to the fix and really appreciate the work around. Thanks @radarhere :)

@radarhere
Copy link
Member

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

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

No branches or pull requests

2 participants