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

Error converting APNG to WEBP #7015

Closed
sjbitcode opened this issue Mar 15, 2023 · 8 comments · Fixed by #7018
Closed

Error converting APNG to WEBP #7015

sjbitcode opened this issue Mar 15, 2023 · 8 comments · Fixed by #7018
Labels

Comments

@sjbitcode
Copy link

sjbitcode commented Mar 15, 2023

What did you do?

Opened an animated png file and tried saving it as a webp image.

I tried two animated png's:

What did you expect to happen?

I expected the webp versions to successfully save the animations for both images.

What actually happened?

Error from WebPImagePlugin.py on save:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/Image.py", line 2431, in save
    save_handler(self, fp, filename)
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/WebPImagePlugin.py", line 287, in _save_all
    enc.add(
TypeError: 'float' object cannot be interpreted as an integer

What are your OS, Python and Pillow versions?

  • OS: macOS Monterey v12.5
  • Python: 3.10.2
  • Pillow: 9.4.0
from PIL import Image

# walking man apng
img = Image.open('img/man.png')
print(img.format)
print(img.mode)
print(img.get_format_mimetype())
print(img.info)
img.save('transformed/man.webp', save_all=True)


# beach ball apng
img2 = Image.open('img/beach_ball.png')
print(img2.format)
print(img2.mode)
print(img2.get_format_mimetype())
print(img2.info)
img2.save('transformed/beach_ball.webp', save_all=True)

The output:

>>> img = Image.open('img/man.png')
>>> print(img.format)
PNG
>>> print(img.mode)
RGB
>>> print(img.get_format_mimetype())
image/apng
>>> print(img.info)
{'loop': 0, 'transparency': (0, 0, 16), 'bbox': (0, 0, 1400, 1050), 'duration': 41.666666666666664, 'disposal': 0, 'blend': 0}
>>> img.save('transformed/man.webp', save_all=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/Image.py", line 2431, in save
    save_handler(self, fp, filename)
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/WebPImagePlugin.py", line 287, in _save_all
    enc.add(
TypeError: 'float' object cannot be interpreted as an integer


>>> img2 = Image.open('img/beach_ball.png')
>>> print(img2.format)
PNG
>>> print(img2.mode)
RGBA
>>> print(img2.get_format_mimetype())
image/apng
>>> print(img2.info)
{'loop': 0, 'bbox': (0, 0, 100, 100), 'duration': 75.0, 'disposal': 1, 'blend': 0}
>>> img2.save('transformed/beach_ball.webp', save_all=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/Image.py", line 2431, in save
    save_handler(self, fp, filename)
  File "/Users/sangeeta/Code/python-pillow-practice/venv/lib/python3.10/site-packages/PIL/WebPImagePlugin.py", line 287, in _save_all
    enc.add(
TypeError: 'float' object cannot be interpreted as an integer
@radarhere
Copy link
Member

The error you are encountering can be resolved by either #6996 or #6977

@sjbitcode
Copy link
Author

thanks for the quick reply @radarhere! Looking forward to seeing those changes merged in! 😄

@radarhere
Copy link
Member

Oh, actually, if you want an immediate way to fix the error, you could just insert img.info["duration"] = round(img.info["duration"]).

from PIL import Image

# walking man apng
img = Image.open('img/man.png')
print(img.format)
print(img.mode)
print(img.get_format_mimetype())
print(img.info)
img.info["duration"] = round(img.info["duration"])
img.save('transformed/man.webp', save_all=True)


# beach ball apng
img2 = Image.open('img/beach_ball.png')
print(img2.format)
print(img2.mode)
print(img2.get_format_mimetype())
print(img2.info)
img2.info["duration"] = round(img2.info["duration"])
img2.save('transformed/beach_ball.webp', save_all=True)

@sjbitcode
Copy link
Author

Thanks @radarhere, that's a clever workaround!

This is probably a separate issue, but the man.webp image has a black background after the first frame; I've attached it here as a zip because I couldn't upload a webp image!
man.webp.zip

I started reading through #5755 and #5837, maybe this is a color palette issue?

@radarhere
Copy link
Member

I created PR #7018 to fix that part, applying transparency to the new frame before blending it with the current image.

@sjbitcode
Copy link
Author

sjbitcode commented Mar 17, 2023

Just tested this, and it works perfectly! Thanks for the quick fix again!

Will this be included in Pillow 9.5.0?

@hugovk
Copy link
Member

hugovk commented Mar 23, 2023

Yes, #7018 has been merged and will be in 9.5.0 on 1st April 2023.

Thanks for the report.

@radarhere
Copy link
Member

Pillow 9.5.0 has now been released.

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

Successfully merging a pull request may close this issue.

3 participants