mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	do not premultiply alpha when resizing with Image.NEAREST resampling
This commit is contained in:
		
							parent
							
								
									aa35f6b572
								
							
						
					
					
						commit
						49fa3656b1
					
				| 
						 | 
					@ -143,6 +143,42 @@ class TestImageTransform:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self._test_alpha_premult(op)
 | 
					        self._test_alpha_premult(op)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _test_nearest(self, op, mode):
 | 
				
			||||||
 | 
					        # create white image with half transparent,
 | 
				
			||||||
 | 
					        # with the black half transparent.
 | 
				
			||||||
 | 
					        # do op,
 | 
				
			||||||
 | 
					        # the image should be white with half transparent
 | 
				
			||||||
 | 
					        transparent, opaque = {
 | 
				
			||||||
 | 
					            "RGBA": ((255, 255, 255, 0), (255, 255, 255, 255)),
 | 
				
			||||||
 | 
					            "LA": ((255, 0), (255, 255)),
 | 
				
			||||||
 | 
					        }[mode]
 | 
				
			||||||
 | 
					        im = Image.new(mode, (10, 10), transparent)
 | 
				
			||||||
 | 
					        im2 = Image.new(mode, (5, 10), opaque)
 | 
				
			||||||
 | 
					        im.paste(im2, (0, 0))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        im = op(im, (40, 10))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        colors = im.getcolors()
 | 
				
			||||||
 | 
					        assert colors == [
 | 
				
			||||||
 | 
					            (20 * 10, opaque),
 | 
				
			||||||
 | 
					            (20 * 10, transparent),
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @pytest.mark.parametrize("mode", ("RGBA", "LA"))
 | 
				
			||||||
 | 
					    def test_nearest_resize(self, mode):
 | 
				
			||||||
 | 
					        def op(im, sz):
 | 
				
			||||||
 | 
					            return im.resize(sz, Image.NEAREST)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self._test_nearest(op, mode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @pytest.mark.parametrize("mode", ("RGBA", "LA"))
 | 
				
			||||||
 | 
					    def test_nearest_transform(self, mode):
 | 
				
			||||||
 | 
					        def op(im, sz):
 | 
				
			||||||
 | 
					            (w, h) = im.size
 | 
				
			||||||
 | 
					            return im.transform(sz, Image.EXTENT, (0, 0, w, h), Image.NEAREST)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self._test_nearest(op, mode)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_blank_fill(self):
 | 
					    def test_blank_fill(self):
 | 
				
			||||||
        # attempting to hit
 | 
					        # attempting to hit
 | 
				
			||||||
        # https://github.com/python-pillow/Pillow/issues/254 reported
 | 
					        # https://github.com/python-pillow/Pillow/issues/254 reported
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1910,7 +1910,7 @@ class Image:
 | 
				
			||||||
        if self.mode in ("1", "P"):
 | 
					        if self.mode in ("1", "P"):
 | 
				
			||||||
            resample = NEAREST
 | 
					            resample = NEAREST
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.mode in ["LA", "RGBA"]:
 | 
					        if self.mode in ["LA", "RGBA"] and resample != NEAREST:
 | 
				
			||||||
            im = self.convert(self.mode[:-1] + "a")
 | 
					            im = self.convert(self.mode[:-1] + "a")
 | 
				
			||||||
            im = im.resize(size, resample, box)
 | 
					            im = im.resize(size, resample, box)
 | 
				
			||||||
            return im.convert(self.mode)
 | 
					            return im.convert(self.mode)
 | 
				
			||||||
| 
						 | 
					@ -2394,14 +2394,14 @@ class Image:
 | 
				
			||||||
        :returns: An :py:class:`~PIL.Image.Image` object.
 | 
					        :returns: An :py:class:`~PIL.Image.Image` object.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.mode == "LA":
 | 
					        if self.mode == "LA" and resample != NEAREST:
 | 
				
			||||||
            return (
 | 
					            return (
 | 
				
			||||||
                self.convert("La")
 | 
					                self.convert("La")
 | 
				
			||||||
                .transform(size, method, data, resample, fill, fillcolor)
 | 
					                .transform(size, method, data, resample, fill, fillcolor)
 | 
				
			||||||
                .convert("LA")
 | 
					                .convert("LA")
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.mode == "RGBA":
 | 
					        if self.mode == "RGBA" and resample != NEAREST:
 | 
				
			||||||
            return (
 | 
					            return (
 | 
				
			||||||
                self.convert("RGBa")
 | 
					                self.convert("RGBa")
 | 
				
			||||||
                .transform(size, method, data, resample, fill, fillcolor)
 | 
					                .transform(size, method, data, resample, fill, fillcolor)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user