Pillow: PIL.PngImagePlugin.PngImageFile images can't be saved as GIFs in 7.1.1

Created on 23 Apr 2020  路  3Comments  路  Source: python-pillow/Pillow

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!

Duplicate

Most helpful comment

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
)

All 3 comments

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
)

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hxzhao527 picture hxzhao527  路  4Comments

anonymous530 picture anonymous530  路  3Comments

boskicthebrain picture boskicthebrain  路  4Comments

vytisb picture vytisb  路  4Comments

steph-ben picture steph-ben  路  4Comments