Repository: python-pillow/Pillow

Issue:
Error converting APNG to WEBP
<!--
Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

1.  General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    * https://stackoverflow.com/questions/tagged/python-imaging-library

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

2.  Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    * https://stackoverflow.com/help/mcve

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

3.  Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.
-->

### What did you do?
Opened an animated png file and tried saving it as a webp image.

I tried two animated png's:
- without transparency ([source](https://lukeroberts.tv/blog/gif-vs-animated-png/))
![man](https://user-images.githubusercontent.com/6550256/225187639-bd483ce5-1dff-437b-a9c1-86c454e514d6.png)
- with transparency ([source](https://commons.wikimedia.org/wiki/File:Animated_PNG_example_bouncing_beach_ball.png))
![beach_ball](https://user-images.githubusercontent.com/6550256/225187697-c7932f7e-93bc-4569-b400-1e477781e0aa.png)

### 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:
```pytb
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

<!--
Please include **code** that reproduces the issue and whenever possible, an **image** that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as Plone, Django, or Buildout, try to replicate the issue just using Pillow.
-->

```python
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:
```pycon
>>> 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
```



Discussion:
The error you are encountering can be resolved by either #6996 or #6977
thanks for the quick reply @radarhere! Looking forward to seeing those changes merged in! 😄 
Oh, actually, if you want an immediate way to fix the error, you could just insert `img.info["duration"] = round(img.info["duration"])`.
```python
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)
```
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](https://github.com/python-pillow/Pillow/files/10984642/man.webp.zip)

I started reading through https://github.com/python-pillow/Pillow/issues/5755 and https://github.com/python-pillow/Pillow/issues/5837, maybe this is a color palette issue?


Resolving patch:
diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py
--- a/src/PIL/PngImagePlugin.py
+++ b/src/PIL/PngImagePlugin.py
@@ -1003,9 +1003,13 @@ def load_end(self):
         else:
             if self._prev_im and self.blend_op == Blend.OP_OVER:
                 updated = self._crop(self.im, self.dispose_extent)
-                self._prev_im.paste(
-                    updated, self.dispose_extent, updated.convert("RGBA")
-                )
+                if self.im.mode == "RGB" and "transparency" in self.info:
+                    mask = updated.convert_transparent(
+                        "RGBA", self.info["transparency"]
+                    )
+                else:
+                    mask = updated.convert("RGBA")
+                self._prev_im.paste(updated, self.dispose_extent, mask)
                 self.im = self._prev_im
                 if self.pyaccess:
                     self.pyaccess = None


Test patch:
diff --git a/Tests/images/blend_transparency.png b/Tests/images/blend_transparency.png
new file mode 100644
Binary files /dev/null and b/Tests/images/blend_transparency.png differ
diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py
--- a/Tests/test_file_apng.py
+++ b/Tests/test_file_apng.py
@@ -163,6 +163,12 @@ def test_apng_blend():
         assert im.getpixel((64, 32)) == (0, 255, 0, 255)
 
 
+def test_apng_blend_transparency():
+    with Image.open("Tests/images/blend_transparency.png") as im:
+        im.seek(1)
+        assert im.getpixel((0, 0)) == (255, 0, 0)
+
+
 def test_apng_chunk_order():
     with Image.open("Tests/images/apng/fctl_actl.png") as im:
         im.seek(im.n_frames - 1)