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
)
Both JPG and PNG code should run.
PNG code errors out with ValueError: read of closed file
Both the PNG and JPG code run on 6.0.0 and 7.0.0, though!
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.
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()-