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

Saving animated GIF as WebP raises "Background color" error #6145

Closed
jeevic opened this issue Mar 21, 2022 · 1 comment · Fixed by #6147
Closed

Saving animated GIF as WebP raises "Background color" error #6145

jeevic opened this issue Mar 21, 2022 · 1 comment · Fixed by #6147
Labels

Comments

@jeevic
Copy link

jeevic commented Mar 21, 2022

What did you do?

convert a animate gif format to animate webP format

What did you expect to happen?

convert success

What actually happened?

osError happen

Traceback (most recent call last):
  File "/Users/jeevi/Documents/WWW/study/python/pillow_img/img/img.py", line 197, 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 227, in _save_all
    % str(background)
OSError: Background color is not an RGBA tuple clamped to (0-255): 255

What are your OS, Python and Pillow versions?

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

use picture
YD_cnt_11_019HNWhSb3SJ

 file = "./YD_cnt_11_019HNWhSb3SJ.gif"
    t = time.time()
    with Image.open(file) as im:
        print("info:", im.info)
        bts = BytesIO()
        im.save(bts, "webP", save_all=True, quality=75,  optimize=True)
        print("长度:{} \n".format(len(bts.getvalue())))
        with open("YD_cnt_11_019HNWhSb3SJ.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))

print info: {'version': b'GIF89a', 'background': 255, 'duration': 160, 'extension': (b'NETSCAPE2.0', 803), 'loop': 0}

i found load gif the background value is 255 and palette is no

webP plugin code can not support

if "background" in encoderinfo:
background = encoderinfo["background"]
elif "background" in im.info:
background = im.info["background"]
if isinstance(background, int):
# GifImagePlugin stores a global color table index in
# info["background"]. So it must be converted to an RGBA value
palette = im.getpalette()
if palette:
r, g, b = palette[background * 3 : (background + 1) * 3]
background = (r, g, b, 0)

error line

if (
not isinstance(background, (list, tuple))
or len(background) != 4
or not all(v >= 0 and v < 256 for v in background)
):
raise OSError(
"Background color is not an RGBA tuple clamped to (0-255): %s"
% str(background)
)

imagemagick get background is white

 Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 440x261+0+0
  Dispose: None
  Delay: 16x100
  Iterations: 0
  Scene: 18 of 19
  Compression: LZW
  Orientation: Undefined
  Properties:
    date:create: 2022-03-18T09:24:11+00:00
    date:modify: 2022-03-18T09:24:02+00:00
    signature: 34c9a1dea7efa1fa2e31c58a9483d118b808f13774f8e2619dcad80c1cec2119
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 951740B
  Number pixels: 114840
  Pixels per second: 61879P
  User time: 1.830u
  Elapsed time: 0:02.855
  Version: ImageMagick 7.1.0-4 Q16 x86_64 2021-07-18 https://imagemagick.org

use pillow i open gif and save gif is success

i think webP plugin can't compatibility background scene

@radarhere radarhere changed the title convert animate GIF to webP error OSError: Background color is not an RGBA tuple clamped to (0-255): 255 Saving animated GIF as WebP raises "Background color" error Mar 21, 2022
@radarhere radarhere added the WebP label Mar 21, 2022
@radarhere
Copy link
Member

I've created PR #6147 to resolve this.

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