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

WebP plugin save animated webp need default duration value #6139

Closed
jeevic opened this issue Mar 17, 2022 · 2 comments · Fixed by #6140
Closed

WebP plugin save animated webp need default duration value #6139

jeevic opened this issue Mar 17, 2022 · 2 comments · Fixed by #6140
Labels

Comments

@jeevic
Copy link

jeevic commented Mar 17, 2022

What did you do?

i only convert a animate gif format to animate webP format

What did you expect to happen?

it can convert success

What actually happened?

python3.7/site-packages/PIL/WebPImagePlugin.py" , line 292, in _save_all timestamp += duration TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

What are your OS, Python and Pillow versions?

  • OS: macos Monterey version 12.2.1
  • Python: 3.7.11
  • Pillow: 8.4.0

through debug, i know the reason is that pillow open gif image use method im.info.get("duration") is null, when i convert to webP format with no Coustom duration param。 because duration come from duration = im.encoderinfo.get("duration", im.info.get("duration")) [WebPImagePlugin.py line 195] be NoneType, it will traceback in WebPImagePlugin.py 292

                # Update timestamp and frame index
                if isinstance(duration, (list, tuple)):
                    timestamp += duration[frame_idx]
                else:
                    timestamp += duration

traceback:

Traceback (most recent call last):
  File "/Users/jeevi/Documents/WWW/study/python/pillow_img/img/img.py", line 198, in <module>
    im.save(bts, "webP", save_all=True, quality=75,  optimize=True)
  File "/Users/jeevi/Documents/WWW/study/python/pillow_img/python/lib/python3.7/site-packages/PIL/Image.py", line 2240, in save
    save_handler(self, fp, filename)
  File "/Users/jeevi/Documents/WWW/study/python/pillow_img/python/lib/python3.7/site-packages/PIL/WebPImagePlugin.py", line 292, in _save_all
    timestamp += duration
TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

i think duration need a default value when not get

here is my code:

file = "./YD_cnt_11_019CS3rXgAIN.gif"
    
    with Image.open(file) as im:
        print(im.format, im.size, im.mode, getattr(im, "is_animated", False), getattr(im, "n_frames", 1),
              getattr(im, "n_frames", 1) // 2)
        bts = BytesIO()
        im.save(bts, "webP", save_all=True, quality=75,  optimize=True)
        print("长度:{} \n".format(len(bts.getvalue())))
        with open("YD_cnt_11_019CS3rXgAIN.gif.webp", "wb") as f:
            f.write(bts.getvalue())
        t2 = time.time()
        cost = int(round(t2 * 1000)) - int(round(t * 1000))
        print("耗时:{}ms\n".format(cost))

the image file in my github : https://github.com/jeevi-cao/study/blob/master/python/YD_cnt_11_019CS3rXgAIN.gif

@radarhere radarhere changed the title webP plugin save animated webp need default duration value WebP plugin save animated webp need default duration value Mar 17, 2022
@radarhere radarhere added the WebP label Mar 17, 2022
@radarhere
Copy link
Member

You're right. I've created PR #6140 to resolve this.

@jeevic
Copy link
Author

jeevic commented Mar 18, 2022

ok thank you

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.

2 participants