mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Added apply_transparency()
This commit is contained in:
		
							parent
							
								
									418820a1e8
								
							
						
					
					
						commit
						11be163143
					
				| 
						 | 
					@ -840,6 +840,35 @@ class TestImage:
 | 
				
			||||||
        im = Image.new("RGB", size)
 | 
					        im = Image.new("RGB", size)
 | 
				
			||||||
        assert im.tobytes() == b""
 | 
					        assert im.tobytes() == b""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_apply_transparency(self):
 | 
				
			||||||
 | 
					        im = Image.new("P", (1, 1))
 | 
				
			||||||
 | 
					        im.putpalette((0, 0, 0, 1, 1, 1))
 | 
				
			||||||
 | 
					        assert im.palette.colors == {(0, 0, 0): 0, (1, 1, 1): 1}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Test that no transformation is applied without transparency
 | 
				
			||||||
 | 
					        im.apply_transparency()
 | 
				
			||||||
 | 
					        assert im.palette.colors == {(0, 0, 0): 0, (1, 1, 1): 1}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Test that a transparency index is applied
 | 
				
			||||||
 | 
					        im.info["transparency"] = 0
 | 
				
			||||||
 | 
					        im.apply_transparency()
 | 
				
			||||||
 | 
					        assert "transparency" not in im.info
 | 
				
			||||||
 | 
					        assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 255): 1}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Test that existing transparency is kept
 | 
				
			||||||
 | 
					        im = Image.new("P", (1, 1))
 | 
				
			||||||
 | 
					        im.putpalette((0, 0, 0, 255, 1, 1, 1, 128), "RGBA")
 | 
				
			||||||
 | 
					        im.info["transparency"] = 0
 | 
				
			||||||
 | 
					        im.apply_transparency()
 | 
				
			||||||
 | 
					        assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 128): 1}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Test that transparency bytes are applied
 | 
				
			||||||
 | 
					        with Image.open("Tests/images/pil123p.png") as im:
 | 
				
			||||||
 | 
					            assert isinstance(im.info["transparency"], bytes)
 | 
				
			||||||
 | 
					            assert im.palette.colors[(27, 35, 6)] == 24
 | 
				
			||||||
 | 
					            im.apply_transparency()
 | 
				
			||||||
 | 
					            assert im.palette.colors[(27, 35, 6, 214)] == 24
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_categories_deprecation(self):
 | 
					    def test_categories_deprecation(self):
 | 
				
			||||||
        with pytest.warns(DeprecationWarning):
 | 
					        with pytest.warns(DeprecationWarning):
 | 
				
			||||||
            assert hopper().category == 0
 | 
					            assert hopper().category == 0
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1449,6 +1449,28 @@ class Image:
 | 
				
			||||||
            rawmode = mode
 | 
					            rawmode = mode
 | 
				
			||||||
        return list(self.im.getpalette(mode, rawmode))
 | 
					        return list(self.im.getpalette(mode, rawmode))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def apply_transparency(self):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        If a P mode image has a "transparency" key in the info dictionary,
 | 
				
			||||||
 | 
					        remove the key and apply the transparency to the palette instead.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        if self.mode != "P" or "transparency" not in self.info:
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        from . import ImagePalette
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        palette = self.getpalette("RGBA")
 | 
				
			||||||
 | 
					        transparency = self.info["transparency"]
 | 
				
			||||||
 | 
					        if isinstance(transparency, bytes):
 | 
				
			||||||
 | 
					            for i, alpha in enumerate(transparency):
 | 
				
			||||||
 | 
					                palette[i * 4 + 3] = alpha
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            palette[transparency * 4 + 3] = 0
 | 
				
			||||||
 | 
					        self.palette = ImagePalette.ImagePalette("RGBA", bytes(palette))
 | 
				
			||||||
 | 
					        self.palette.dirty = 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        del self.info["transparency"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def getpixel(self, xy):
 | 
					    def getpixel(self, xy):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Returns the pixel value at a given position.
 | 
					        Returns the pixel value at a given position.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user