mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	Use save parameters as encoderinfo defaults (#9001)
This commit is contained in:
		
						commit
						c22230b761
					
				| 
						 | 
					@ -317,13 +317,24 @@ def test_save_all() -> None:
 | 
				
			||||||
def test_save_xmp() -> None:
 | 
					def test_save_xmp() -> None:
 | 
				
			||||||
    im = Image.new("RGB", (1, 1))
 | 
					    im = Image.new("RGB", (1, 1))
 | 
				
			||||||
    im2 = Image.new("RGB", (1, 1), "#f00")
 | 
					    im2 = Image.new("RGB", (1, 1), "#f00")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def roundtrip_xmp() -> list[Any]:
 | 
				
			||||||
 | 
					        im_reloaded = roundtrip(im, xmp=b"Default", save_all=True, append_images=[im2])
 | 
				
			||||||
 | 
					        xmp = [im_reloaded.info["xmp"]]
 | 
				
			||||||
 | 
					        im_reloaded.seek(1)
 | 
				
			||||||
 | 
					        return xmp + [im_reloaded.info["xmp"]]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Use the save parameters for all frames by default
 | 
				
			||||||
 | 
					    assert roundtrip_xmp() == [b"Default", b"Default"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Specify a value for the first frame
 | 
				
			||||||
 | 
					    im.encoderinfo = {"xmp": b"First frame"}
 | 
				
			||||||
 | 
					    assert roundtrip_xmp() == [b"First frame", b"Default"]
 | 
				
			||||||
 | 
					    del im.encoderinfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Specify value for the second frame
 | 
				
			||||||
    im2.encoderinfo = {"xmp": b"Second frame"}
 | 
					    im2.encoderinfo = {"xmp": b"Second frame"}
 | 
				
			||||||
    im_reloaded = roundtrip(im, xmp=b"First frame", save_all=True, append_images=[im2])
 | 
					    assert roundtrip_xmp() == [b"Default", b"Second frame"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Test that encoderinfo is unchanged
 | 
					    # Test that encoderinfo is unchanged
 | 
				
			||||||
    assert im2.encoderinfo == {"xmp": b"Second frame"}
 | 
					    assert im2.encoderinfo == {"xmp": b"Second frame"}
 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert im_reloaded.info["xmp"] == b"First frame"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    im_reloaded.seek(1)
 | 
					 | 
				
			||||||
    assert im_reloaded.info["xmp"] == b"Second frame"
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -681,16 +681,21 @@ class TestFileTiff:
 | 
				
			||||||
            assert im.tag_v2[278] == 256
 | 
					            assert im.tag_v2[278] == 256
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        im = hopper()
 | 
					        im = hopper()
 | 
				
			||||||
 | 
					        im.encoderinfo = {"tiffinfo": {278: 100}}
 | 
				
			||||||
        im2 = Image.new("L", (128, 128))
 | 
					        im2 = Image.new("L", (128, 128))
 | 
				
			||||||
        im2.encoderinfo = {"tiffinfo": {278: 256}}
 | 
					        im3 = im2.copy()
 | 
				
			||||||
        im.save(outfile, save_all=True, append_images=[im2])
 | 
					        im3.encoderinfo = {"tiffinfo": {278: 300}}
 | 
				
			||||||
 | 
					        im.save(outfile, save_all=True, tiffinfo={278: 200}, append_images=[im2, im3])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        with Image.open(outfile) as im:
 | 
					        with Image.open(outfile) as im:
 | 
				
			||||||
            assert isinstance(im, TiffImagePlugin.TiffImageFile)
 | 
					            assert isinstance(im, TiffImagePlugin.TiffImageFile)
 | 
				
			||||||
            assert im.tag_v2[278] == 128
 | 
					            assert im.tag_v2[278] == 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            im.seek(1)
 | 
					            im.seek(1)
 | 
				
			||||||
            assert im.tag_v2[278] == 256
 | 
					            assert im.tag_v2[278] == 200
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            im.seek(2)
 | 
				
			||||||
 | 
					            assert im.tag_v2[278] == 300
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_strip_raw(self) -> None:
 | 
					    def test_strip_raw(self) -> None:
 | 
				
			||||||
        infile = "Tests/images/tiff_strip_raw.tif"
 | 
					        infile = "Tests/images/tiff_strip_raw.tif"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2556,8 +2556,9 @@ class Image:
 | 
				
			||||||
            self.load()
 | 
					            self.load()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        save_all = params.pop("save_all", None)
 | 
					        save_all = params.pop("save_all", None)
 | 
				
			||||||
 | 
					        self._default_encoderinfo = params
 | 
				
			||||||
        encoderinfo = getattr(self, "encoderinfo", {})
 | 
					        encoderinfo = getattr(self, "encoderinfo", {})
 | 
				
			||||||
        self.encoderinfo = {**encoderinfo, **params}
 | 
					        self._attach_default_encoderinfo(self)
 | 
				
			||||||
        self.encoderconfig: tuple[Any, ...] = ()
 | 
					        self.encoderconfig: tuple[Any, ...] = ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if format.upper() not in SAVE:
 | 
					        if format.upper() not in SAVE:
 | 
				
			||||||
| 
						 | 
					@ -2599,6 +2600,11 @@ class Image:
 | 
				
			||||||
        if open_fp:
 | 
					        if open_fp:
 | 
				
			||||||
            fp.close()
 | 
					            fp.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _attach_default_encoderinfo(self, im: Image) -> dict[str, Any]:
 | 
				
			||||||
 | 
					        encoderinfo = getattr(self, "encoderinfo", {})
 | 
				
			||||||
 | 
					        self.encoderinfo = {**im._default_encoderinfo, **encoderinfo}
 | 
				
			||||||
 | 
					        return encoderinfo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def seek(self, frame: int) -> None:
 | 
					    def seek(self, frame: int) -> None:
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Seeks to the given frame in this sequence file. If you seek
 | 
					        Seeks to the given frame in this sequence file. If you seek
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +69,9 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
 | 
				
			||||||
                JpegImagePlugin._save(im_frame, fp, filename)
 | 
					                JpegImagePlugin._save(im_frame, fp, filename)
 | 
				
			||||||
                offsets.append(fp.tell())
 | 
					                offsets.append(fp.tell())
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
 | 
					                encoderinfo = im_frame._attach_default_encoderinfo(im)
 | 
				
			||||||
                im_frame.save(fp, "JPEG")
 | 
					                im_frame.save(fp, "JPEG")
 | 
				
			||||||
 | 
					                im_frame.encoderinfo = encoderinfo
 | 
				
			||||||
                offsets.append(fp.tell() - offsets[-1])
 | 
					                offsets.append(fp.tell() - offsets[-1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ifd = TiffImagePlugin.ImageFileDirectory_v2()
 | 
					    ifd = TiffImagePlugin.ImageFileDirectory_v2()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2311,8 +2311,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        with AppendingTiffWriter(fp) as tf:
 | 
					        with AppendingTiffWriter(fp) as tf:
 | 
				
			||||||
            for ims in [im] + append_images:
 | 
					            for ims in [im] + append_images:
 | 
				
			||||||
                if not hasattr(ims, "encoderinfo"):
 | 
					                encoderinfo = ims._attach_default_encoderinfo(im)
 | 
				
			||||||
                    ims.encoderinfo = {}
 | 
					 | 
				
			||||||
                if not hasattr(ims, "encoderconfig"):
 | 
					                if not hasattr(ims, "encoderconfig"):
 | 
				
			||||||
                    ims.encoderconfig = ()
 | 
					                    ims.encoderconfig = ()
 | 
				
			||||||
                nfr = getattr(ims, "n_frames", 1)
 | 
					                nfr = getattr(ims, "n_frames", 1)
 | 
				
			||||||
| 
						 | 
					@ -2322,6 +2321,7 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
 | 
				
			||||||
                    ims.load()
 | 
					                    ims.load()
 | 
				
			||||||
                    _save(ims, tf, filename)
 | 
					                    _save(ims, tf, filename)
 | 
				
			||||||
                    tf.newFrame()
 | 
					                    tf.newFrame()
 | 
				
			||||||
 | 
					                ims.encoderinfo = encoderinfo
 | 
				
			||||||
    finally:
 | 
					    finally:
 | 
				
			||||||
        im.seek(cur_idx)
 | 
					        im.seek(cur_idx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user