mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Ignore non-opaque WebP background when saving as GIF
This commit is contained in:
		
							parent
							
								
									0d6440d9f2
								
							
						
					
					
						commit
						66f5ad0eae
					
				| 
						 | 
					@ -859,12 +859,21 @@ def test_background(tmp_path):
 | 
				
			||||||
    im.info["background"] = 1
 | 
					    im.info["background"] = 1
 | 
				
			||||||
    im.save(out)
 | 
					    im.save(out)
 | 
				
			||||||
    with Image.open(out) as reread:
 | 
					    with Image.open(out) as reread:
 | 
				
			||||||
 | 
					 | 
				
			||||||
        assert reread.info["background"] == im.info["background"]
 | 
					        assert reread.info["background"] == im.info["background"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_webp_background(tmp_path):
 | 
				
			||||||
 | 
					    out = str(tmp_path / "temp.gif")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Test opaque WebP background
 | 
				
			||||||
    if features.check("webp") and features.check("webp_anim"):
 | 
					    if features.check("webp") and features.check("webp_anim"):
 | 
				
			||||||
        with Image.open("Tests/images/hopper.webp") as im:
 | 
					        with Image.open("Tests/images/hopper.webp") as im:
 | 
				
			||||||
            assert isinstance(im.info["background"], tuple)
 | 
					            assert im.info["background"] == (255, 255, 255, 255)
 | 
				
			||||||
 | 
					            im.save(out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Test non-opaque WebP background
 | 
				
			||||||
 | 
					    im = Image.new("L", (100, 100), "#000")
 | 
				
			||||||
 | 
					    im.info["background"] = (0, 0, 0, 0)
 | 
				
			||||||
    im.save(out)
 | 
					    im.save(out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -886,20 +886,23 @@ def _get_palette_bytes(im):
 | 
				
			||||||
def _get_background(im, info_background):
 | 
					def _get_background(im, info_background):
 | 
				
			||||||
    background = 0
 | 
					    background = 0
 | 
				
			||||||
    if info_background:
 | 
					    if info_background:
 | 
				
			||||||
        background = info_background
 | 
					        if isinstance(info_background, tuple):
 | 
				
			||||||
        if isinstance(background, tuple):
 | 
					 | 
				
			||||||
            # WebPImagePlugin stores an RGBA value in info["background"]
 | 
					            # WebPImagePlugin stores an RGBA value in info["background"]
 | 
				
			||||||
            # So it must be converted to the same format as GifImagePlugin's
 | 
					            # So it must be converted to the same format as GifImagePlugin's
 | 
				
			||||||
            # info["background"] - a global color table index
 | 
					            # info["background"] - a global color table index
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                background = im.palette.getcolor(background, im)
 | 
					                background = im.palette.getcolor(info_background, im)
 | 
				
			||||||
            except ValueError as e:
 | 
					            except ValueError as e:
 | 
				
			||||||
                if str(e) == "cannot allocate more than 256 colors":
 | 
					                if str(e) not in (
 | 
				
			||||||
                    # If all 256 colors are in use,
 | 
					                    # If all 256 colors are in use,
 | 
				
			||||||
                    # then there is no need for the background color
 | 
					                    # then there is no need for the background color
 | 
				
			||||||
                    return 0
 | 
					                    "cannot allocate more than 256 colors",
 | 
				
			||||||
                else:
 | 
					                    # Ignore non-opaque WebP background
 | 
				
			||||||
 | 
					                    "cannot add non-opaque RGBA color to RGB palette",
 | 
				
			||||||
 | 
					                ):
 | 
				
			||||||
                    raise
 | 
					                    raise
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            background = info_background
 | 
				
			||||||
    return background
 | 
					    return background
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user