{
    "file_diffs": [
        {
            "old_file_content": "from __future__ import annotations\n\nimport re\nimport sys\nimport warnings\nimport zlib\nfrom io import BytesIO\nfrom pathlib import Path\nfrom types import ModuleType\nfrom typing import Any, cast\n\nimport pytest\n\nfrom PIL import Image, ImageFile, PngImagePlugin, features\n\nfrom .helper import (\n    PillowLeakTestCase,\n    assert_image,\n    assert_image_equal,\n    assert_image_equal_tofile,\n    hopper,\n    is_win32,\n    mark_if_feature_version,\n    skip_unless_feature,\n)\n\nElementTree: ModuleType | None\ntry:\n    from defusedxml import ElementTree\nexcept ImportError:\n    ElementTree = None\n\n# sample png stream\n\nTEST_PNG_FILE = \"Tests/images/hopper.png\"\n\n# stuff to create inline PNG images\n\nMAGIC = PngImagePlugin._MAGIC\n\n\ndef chunk(cid: bytes, *data: bytes) -> bytes:\n    test_file = BytesIO()\n    PngImagePlugin.putchunk(*(test_file, cid) + data)\n    return test_file.getvalue()\n\n\no32 = PngImagePlugin.o32\n\nIHDR = chunk(b\"IHDR\", o32(1), o32(1), b\"\\x08\\x02\", b\"\\0\\0\\0\")\nIDAT = chunk(b\"IDAT\")\nIEND = chunk(b\"IEND\")\n\nHEAD = MAGIC + IHDR\nTAIL = IDAT + IEND\n\n\ndef load(data: bytes) -> Image.Image:\n    return Image.open(BytesIO(data))\n\n\ndef roundtrip(im: Image.Image, **options: Any) -> PngImagePlugin.PngImageFile:\n    out = BytesIO()\n    im.save(out, \"PNG\", **options)\n    out.seek(0)\n    return cast(PngImagePlugin.PngImageFile, Image.open(out))\n\n\n@skip_unless_feature(\"zlib\")\nclass TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_specify_bits(self, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n\n@pytest.mark.skipif(is_win32(), reason=\"Requires Unix or macOS\")\n@skip_unless_feature(\"zlib\")\nclass TestTruncatedPngPLeaks(PillowLeakTestCase):\n    mem_limit = 2 * 1024  # max increase in K\n    iterations = 100  # Leak is 56k/iteration, this will leak 5.6megs\n\n    def test_leak_load(self) -> None:\n        with open(\"Tests/images/hopper.png\", \"rb\") as f:\n            DATA = BytesIO(f.read(16 * 1024))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        with Image.open(DATA) as im:\n            im.load()\n\n        def core() -> None:\n            with Image.open(DATA) as im:\n                im.load()\n\n        try:\n            self._test_leak(core)\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n",
            "new_file_content": "from __future__ import annotations\n\nimport re\nimport sys\nimport warnings\nimport zlib\nfrom io import BytesIO\nfrom pathlib import Path\nfrom types import ModuleType\nfrom typing import Any, cast\n\nimport pytest\n\nfrom PIL import Image, ImageFile, PngImagePlugin, features\n\nfrom .helper import (\n    PillowLeakTestCase,\n    assert_image,\n    assert_image_equal,\n    assert_image_equal_tofile,\n    hopper,\n    is_win32,\n    mark_if_feature_version,\n    skip_unless_feature,\n)\n\nElementTree: ModuleType | None\ntry:\n    from defusedxml import ElementTree\nexcept ImportError:\n    ElementTree = None\n\n# sample png stream\n\nTEST_PNG_FILE = \"Tests/images/hopper.png\"\n\n# stuff to create inline PNG images\n\nMAGIC = PngImagePlugin._MAGIC\n\n\ndef chunk(cid: bytes, *data: bytes) -> bytes:\n    test_file = BytesIO()\n    PngImagePlugin.putchunk(*(test_file, cid) + data)\n    return test_file.getvalue()\n\n\no32 = PngImagePlugin.o32\n\nIHDR = chunk(b\"IHDR\", o32(1), o32(1), b\"\\x08\\x02\", b\"\\0\\0\\0\")\nIDAT = chunk(b\"IDAT\")\nIEND = chunk(b\"IEND\")\n\nHEAD = MAGIC + IHDR\nTAIL = IDAT + IEND\n\n\ndef load(data: bytes) -> Image.Image:\n    return Image.open(BytesIO(data))\n\n\ndef roundtrip(im: Image.Image, **options: Any) -> PngImagePlugin.PngImageFile:\n    out = BytesIO()\n    im.save(out, \"PNG\", **options)\n    out.seek(0)\n    return cast(PngImagePlugin.PngImageFile, Image.open(out))\n\n\n@skip_unless_feature(\"zlib\")\nclass TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    @pytest.mark.parametrize(\"save_all\", (True, False))\n    def test_specify_bits(self, save_all: bool, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4, save_all=save_all)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n\n@pytest.mark.skipif(is_win32(), reason=\"Requires Unix or macOS\")\n@skip_unless_feature(\"zlib\")\nclass TestTruncatedPngPLeaks(PillowLeakTestCase):\n    mem_limit = 2 * 1024  # max increase in K\n    iterations = 100  # Leak is 56k/iteration, this will leak 5.6megs\n\n    def test_leak_load(self) -> None:\n        with open(\"Tests/images/hopper.png\", \"rb\") as f:\n            DATA = BytesIO(f.read(16 * 1024))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        with Image.open(DATA) as im:\n            im.load()\n\n        def core() -> None:\n            with Image.open(DATA) as im:\n                im.load()\n\n        try:\n            self._test_leak(core)\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n",
            "header": {
                "file": {
                    "path": "Tests/test_file_png.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "19462dcb5",
                "new_commit_hash": "55db5ef85",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/Tests/test_file_png.py"
            },
            "plus_file": {
                "path": "b/Tests/test_file_png.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 655,
                            "length": 11
                        },
                        "new_range": {
                            "start": 655,
                            "length": 12
                        },
                        "section": "class TestFilePng:"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "            png.call(cid, 0, 0)",
                                "type": "context"
                            },
                            {
                                "content": "            ImageFile.LOAD_TRUNCATED_IMAGES = False",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    def test_specify_bits(self, tmp_path: Path) -> None:",
                                "type": "deleted"
                            },
                            {
                                "content": "    @pytest.mark.parametrize(\"save_all\", (True, False))",
                                "type": "added"
                            },
                            {
                                "content": "    def test_specify_bits(self, save_all: bool, tmp_path: Path) -> None:",
                                "type": "added"
                            },
                            {
                                "content": "        im = hopper(\"P\")",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "        out = str(tmp_path / \"temp.png\")",
                                "type": "context"
                            },
                            {
                                "content": "        im.save(out, bits=4)",
                                "type": "deleted"
                            },
                            {
                                "content": "        im.save(out, bits=4, save_all=save_all)",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "        with Image.open(out) as reloaded:",
                                "type": "context"
                            },
                            {
                                "content": "            assert len(reloaded.png.im_palette[1]) == 48",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "Tests/test_file_png.py",
                            "type": "method",
                            "name": "TestFilePng.test_specify_bits",
                            "content": "class TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_specify_bits(self, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 658,
                            "end_lineno": 665,
                            "parent": {
                                "file_name": "Tests/test_file_png.py",
                                "type": "class",
                                "name": "TestFilePng",
                                "content": "class TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_specify_bits(self, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False",
                                "ast_type_str": "ClassDef",
                                "start_lineno": 70,
                                "end_lineno": 802,
                                "parent": null
                            }
                        },
                        {
                            "file_name": "Tests/test_file_png.py",
                            "type": "class",
                            "name": "TestFilePng",
                            "content": "class TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_specify_bits(self, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False",
                            "ast_type_str": "ClassDef",
                            "start_lineno": 70,
                            "end_lineno": 802,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        },
        {
            "old_file_content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# PNG support code\n#\n# See \"PNG (Portable Network Graphics) Specification, version 1.0;\n# W3C Recommendation\", 1996-10-01, Thomas Boutell (ed.).\n#\n# history:\n# 1996-05-06 fl   Created (couldn't resist it)\n# 1996-12-14 fl   Upgraded, added read and verify support (0.2)\n# 1996-12-15 fl   Separate PNG stream parser\n# 1996-12-29 fl   Added write support, added getchunks\n# 1996-12-30 fl   Eliminated circular references in decoder (0.3)\n# 1998-07-12 fl   Read/write 16-bit images as mode I (0.4)\n# 2001-02-08 fl   Added transparency support (from Zircon) (0.5)\n# 2001-04-16 fl   Don't close data source in \"open\" method (0.6)\n# 2004-02-24 fl   Don't even pretend to support interlaced files (0.7)\n# 2004-08-31 fl   Do basic sanity check on chunk identifiers (0.8)\n# 2004-09-20 fl   Added PngInfo chunk container\n# 2004-12-18 fl   Added DPI read support (based on code by Niki Spahiev)\n# 2008-08-13 fl   Added tRNS support for RGB images\n# 2009-03-06 fl   Support for preserving ICC profiles (by Florian Hoech)\n# 2009-03-08 fl   Added zTXT support (from Lowell Alleman)\n# 2009-03-29 fl   Read interlaced PNG files (from Conrado Porto Lopes Gouvua)\n#\n# Copyright (c) 1997-2009 by Secret Labs AB\n# Copyright (c) 1996 by Fredrik Lundh\n#\n# See the README file for information on usage and redistribution.\n#\nfrom __future__ import annotations\n\nimport itertools\nimport logging\nimport re\nimport struct\nimport warnings\nimport zlib\nfrom enum import IntEnum\nfrom typing import IO\n\nfrom . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence\nfrom ._binary import i16be as i16\nfrom ._binary import i32be as i32\nfrom ._binary import o8\nfrom ._binary import o16be as o16\nfrom ._binary import o32be as o32\n\nlogger = logging.getLogger(__name__)\n\nis_cid = re.compile(rb\"\\w\\w\\w\\w\").match\n\n\n_MAGIC = b\"\\211PNG\\r\\n\\032\\n\"\n\n\n_MODES = {\n    # supported bits/color combinations, and corresponding modes/rawmodes\n    # Grayscale\n    (1, 0): (\"1\", \"1\"),\n    (2, 0): (\"L\", \"L;2\"),\n    (4, 0): (\"L\", \"L;4\"),\n    (8, 0): (\"L\", \"L\"),\n    (16, 0): (\"I;16\", \"I;16B\"),\n    # Truecolour\n    (8, 2): (\"RGB\", \"RGB\"),\n    (16, 2): (\"RGB\", \"RGB;16B\"),\n    # Indexed-colour\n    (1, 3): (\"P\", \"P;1\"),\n    (2, 3): (\"P\", \"P;2\"),\n    (4, 3): (\"P\", \"P;4\"),\n    (8, 3): (\"P\", \"P\"),\n    # Grayscale with alpha\n    (8, 4): (\"LA\", \"LA\"),\n    (16, 4): (\"RGBA\", \"LA;16B\"),  # LA;16B->LA not yet available\n    # Truecolour with alpha\n    (8, 6): (\"RGBA\", \"RGBA\"),\n    (16, 6): (\"RGBA\", \"RGBA;16B\"),\n}\n\n\n_simple_palette = re.compile(b\"^\\xff*\\x00\\xff*$\")\n\nMAX_TEXT_CHUNK = ImageFile.SAFEBLOCK\n\"\"\"\nMaximum decompressed size for a iTXt or zTXt chunk.\nEliminates decompression bombs where compressed chunks can expand 1000x.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\nMAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK\n\"\"\"\nSet the maximum total text chunk size.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\n\n\n# APNG frame disposal modes\nclass Disposal(IntEnum):\n    OP_NONE = 0\n    \"\"\"\n    No disposal is done on this frame before rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_BACKGROUND = 1\n    \"\"\"\n    This frame’s modified region is cleared to fully transparent black before rendering\n    the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_PREVIOUS = 2\n    \"\"\"\n    This frame’s modified region is reverted to the previous frame’s contents before\n    rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\n# APNG frame blend modes\nclass Blend(IntEnum):\n    OP_SOURCE = 0\n    \"\"\"\n    All color components of this frame, including alpha, overwrite the previous output\n    image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_OVER = 1\n    \"\"\"\n    This frame should be alpha composited with the previous output image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\ndef _safe_zlib_decompress(s):\n    dobj = zlib.decompressobj()\n    plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)\n    if dobj.unconsumed_tail:\n        msg = \"Decompressed Data Too Large\"\n        raise ValueError(msg)\n    return plaintext\n\n\ndef _crc32(data, seed=0):\n    return zlib.crc32(data, seed) & 0xFFFFFFFF\n\n\n# --------------------------------------------------------------------\n# Support classes.  Suitable for PNG and related formats like MNG etc.\n\n\nclass ChunkStream:\n    def __init__(self, fp: IO[bytes]) -> None:\n        self.fp: IO[bytes] | None = fp\n        self.queue: list[tuple[bytes, int, int]] | None = []\n\n    def read(self) -> tuple[bytes, int, int]:\n        \"\"\"Fetch a new chunk. Returns header information.\"\"\"\n        cid = None\n\n        assert self.fp is not None\n        if self.queue:\n            cid, pos, length = self.queue.pop()\n            self.fp.seek(pos)\n        else:\n            s = self.fp.read(8)\n            cid = s[4:]\n            pos = self.fp.tell()\n            length = i32(s)\n\n        if not is_cid(cid):\n            if not ImageFile.LOAD_TRUNCATED_IMAGES:\n                msg = f\"broken PNG file (chunk {repr(cid)})\"\n                raise SyntaxError(msg)\n\n        return cid, pos, length\n\n    def __enter__(self) -> ChunkStream:\n        return self\n\n    def __exit__(self, *args):\n        self.close()\n\n    def close(self) -> None:\n        self.queue = self.fp = None\n\n    def push(self, cid: bytes, pos: int, length: int) -> None:\n        assert self.queue is not None\n        self.queue.append((cid, pos, length))\n\n    def call(self, cid, pos, length):\n        \"\"\"Call the appropriate chunk handler\"\"\"\n\n        logger.debug(\"STREAM %r %s %s\", cid, pos, length)\n        return getattr(self, f\"chunk_{cid.decode('ascii')}\")(pos, length)\n\n    def crc(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read and verify checksum\"\"\"\n\n        # Skip CRC checks for ancillary chunks if allowed to load truncated\n        # images\n        # 5th byte of first char is 1 [specs, section 5.4]\n        if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):\n            self.crc_skip(cid, data)\n            return\n\n        assert self.fp is not None\n        try:\n            crc1 = _crc32(data, _crc32(cid))\n            crc2 = i32(self.fp.read(4))\n            if crc1 != crc2:\n                msg = f\"broken PNG file (bad header checksum in {repr(cid)})\"\n                raise SyntaxError(msg)\n        except struct.error as e:\n            msg = f\"broken PNG file (incomplete checksum in {repr(cid)})\"\n            raise SyntaxError(msg) from e\n\n    def crc_skip(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read checksum\"\"\"\n\n        assert self.fp is not None\n        self.fp.read(4)\n\n    def verify(self, endchunk: bytes = b\"IEND\") -> list[bytes]:\n        # Simple approach; just calculate checksum for all remaining\n        # blocks.  Must be called directly after open.\n\n        cids = []\n\n        while True:\n            try:\n                cid, pos, length = self.read()\n            except struct.error as e:\n                msg = \"truncated PNG file\"\n                raise OSError(msg) from e\n\n            if cid == endchunk:\n                break\n            self.crc(cid, ImageFile._safe_read(self.fp, length))\n            cids.append(cid)\n\n        return cids\n\n\nclass iTXt(str):\n    \"\"\"\n    Subclass of string to allow iTXt chunks to look like strings while\n    keeping their extra information\n\n    \"\"\"\n\n    @staticmethod\n    def __new__(cls, text, lang=None, tkey=None):\n        \"\"\"\n        :param cls: the class to use when creating the instance\n        :param text: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        \"\"\"\n\n        self = str.__new__(cls, text)\n        self.lang = lang\n        self.tkey = tkey\n        return self\n\n\nclass PngInfo:\n    \"\"\"\n    PNG chunk container (for use with save(pnginfo=))\n\n    \"\"\"\n\n    def __init__(self):\n        self.chunks = []\n\n    def add(self, cid, data, after_idat=False):\n        \"\"\"Appends an arbitrary chunk. Use with caution.\n\n        :param cid: a byte string, 4 bytes long.\n        :param data: a byte string of the encoded data\n        :param after_idat: for use with private chunks. Whether the chunk\n                           should be written after IDAT\n\n        \"\"\"\n\n        chunk = [cid, data]\n        if after_idat:\n            chunk.append(True)\n        self.chunks.append(tuple(chunk))\n\n    def add_itxt(self, key, value, lang=\"\", tkey=\"\", zip=False):\n        \"\"\"Appends an iTXt chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        :param zip: compression flag\n\n        \"\"\"\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n        if not isinstance(value, bytes):\n            value = value.encode(\"utf-8\", \"strict\")\n        if not isinstance(lang, bytes):\n            lang = lang.encode(\"utf-8\", \"strict\")\n        if not isinstance(tkey, bytes):\n            tkey = tkey.encode(\"utf-8\", \"strict\")\n\n        if zip:\n            self.add(\n                b\"iTXt\",\n                key + b\"\\0\\x01\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + zlib.compress(value),\n            )\n        else:\n            self.add(b\"iTXt\", key + b\"\\0\\0\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + value)\n\n    def add_text(self, key, value, zip=False):\n        \"\"\"Appends a text chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key, text or an\n           :py:class:`PIL.PngImagePlugin.iTXt` instance\n        :param zip: compression flag\n\n        \"\"\"\n        if isinstance(value, iTXt):\n            return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)\n\n        # The tEXt chunk stores latin-1 text\n        if not isinstance(value, bytes):\n            try:\n                value = value.encode(\"latin-1\", \"strict\")\n            except UnicodeError:\n                return self.add_itxt(key, value, zip=zip)\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n\n        if zip:\n            self.add(b\"zTXt\", key + b\"\\0\\0\" + zlib.compress(value))\n        else:\n            self.add(b\"tEXt\", key + b\"\\0\" + value)\n\n\n# --------------------------------------------------------------------\n# PNG image stream (IHDR/IEND)\n\n\nclass PngStream(ChunkStream):\n    def __init__(self, fp):\n        super().__init__(fp)\n\n        # local copies of Image attributes\n        self.im_info = {}\n        self.im_text = {}\n        self.im_size = (0, 0)\n        self.im_mode = None\n        self.im_tile = None\n        self.im_palette = None\n        self.im_custom_mimetype = None\n        self.im_n_frames = None\n        self._seq_num = None\n        self.rewind_state = None\n\n        self.text_memory = 0\n\n    def check_text_memory(self, chunklen: int) -> None:\n        self.text_memory += chunklen\n        if self.text_memory > MAX_TEXT_MEMORY:\n            msg = (\n                \"Too much memory used in text chunks: \"\n                f\"{self.text_memory}>MAX_TEXT_MEMORY\"\n            )\n            raise ValueError(msg)\n\n    def save_rewind(self) -> None:\n        self.rewind_state = {\n            \"info\": self.im_info.copy(),\n            \"tile\": self.im_tile,\n            \"seq_num\": self._seq_num,\n        }\n\n    def rewind(self) -> None:\n        self.im_info = self.rewind_state[\"info\"].copy()\n        self.im_tile = self.rewind_state[\"tile\"]\n        self._seq_num = self.rewind_state[\"seq_num\"]\n\n    def chunk_iCCP(self, pos: int, length: int) -> bytes:\n        # ICC profile\n        s = ImageFile._safe_read(self.fp, length)\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        i = s.find(b\"\\0\")\n        logger.debug(\"iCCP profile name %r\", s[:i])\n        comp_method = s[i + 1]\n        logger.debug(\"Compression method %s\", comp_method)\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in iCCP chunk\"\n            raise SyntaxError(msg)\n        try:\n            icc_profile = _safe_zlib_decompress(s[i + 2 :])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                icc_profile = None\n            else:\n                raise\n        except zlib.error:\n            icc_profile = None  # FIXME\n        self.im_info[\"icc_profile\"] = icc_profile\n        return s\n\n    def chunk_IHDR(self, pos: int, length: int) -> bytes:\n        # image header\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 13:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated IHDR chunk\"\n            raise ValueError(msg)\n        self.im_size = i32(s, 0), i32(s, 4)\n        try:\n            self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]\n        except Exception:\n            pass\n        if s[12]:\n            self.im_info[\"interlace\"] = 1\n        if s[11]:\n            msg = \"unknown filter category\"\n            raise SyntaxError(msg)\n        return s\n\n    def chunk_IDAT(self, pos, length):\n        # image data\n        if \"bbox\" in self.im_info:\n            tile = [(\"zip\", self.im_info[\"bbox\"], pos, self.im_rawmode)]\n        else:\n            if self.im_n_frames is not None:\n                self.im_info[\"default_image\"] = True\n            tile = [(\"zip\", (0, 0) + self.im_size, pos, self.im_rawmode)]\n        self.im_tile = tile\n        self.im_idat = length\n        msg = \"image data found\"\n        raise EOFError(msg)\n\n    def chunk_IEND(self, pos, length):\n        msg = \"end of PNG image\"\n        raise EOFError(msg)\n\n    def chunk_PLTE(self, pos: int, length: int) -> bytes:\n        # palette\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            self.im_palette = \"RGB\", s\n        return s\n\n    def chunk_tRNS(self, pos: int, length: int) -> bytes:\n        # transparency\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            if _simple_palette.match(s):\n                # tRNS contains only one full-transparent entry,\n                # other entries are full opaque\n                i = s.find(b\"\\0\")\n                if i >= 0:\n                    self.im_info[\"transparency\"] = i\n            else:\n                # otherwise, we have a byte string with one alpha value\n                # for each palette entry\n                self.im_info[\"transparency\"] = s\n        elif self.im_mode in (\"1\", \"L\", \"I;16\"):\n            self.im_info[\"transparency\"] = i16(s)\n        elif self.im_mode == \"RGB\":\n            self.im_info[\"transparency\"] = i16(s), i16(s, 2), i16(s, 4)\n        return s\n\n    def chunk_gAMA(self, pos: int, length: int) -> bytes:\n        # gamma setting\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"gamma\"] = i32(s) / 100000.0\n        return s\n\n    def chunk_cHRM(self, pos: int, length: int) -> bytes:\n        # chromaticity, 8 unsigned ints, actual value is scaled by 100,000\n        # WP x,y, Red x,y, Green x,y Blue x,y\n\n        s = ImageFile._safe_read(self.fp, length)\n        raw_vals = struct.unpack(\">%dI\" % (len(s) // 4), s)\n        self.im_info[\"chromaticity\"] = tuple(elt / 100000.0 for elt in raw_vals)\n        return s\n\n    def chunk_sRGB(self, pos: int, length: int) -> bytes:\n        # srgb rendering intent, 1 byte\n        # 0 perceptual\n        # 1 relative colorimetric\n        # 2 saturation\n        # 3 absolute colorimetric\n\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 1:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated sRGB chunk\"\n            raise ValueError(msg)\n        self.im_info[\"srgb\"] = s[0]\n        return s\n\n    def chunk_pHYs(self, pos: int, length: int) -> bytes:\n        # pixels per unit\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 9:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated pHYs chunk\"\n            raise ValueError(msg)\n        px, py = i32(s, 0), i32(s, 4)\n        unit = s[8]\n        if unit == 1:  # meter\n            dpi = px * 0.0254, py * 0.0254\n            self.im_info[\"dpi\"] = dpi\n        elif unit == 0:\n            self.im_info[\"aspect\"] = px, py\n        return s\n\n    def chunk_tEXt(self, pos: int, length: int) -> bytes:\n        # text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            # fallback for broken tEXt tags\n            k = s\n            v = b\"\"\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v_str = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = v if k == \"exif\" else v_str\n            self.im_text[k] = v_str\n            self.check_text_memory(len(v_str))\n\n        return s\n\n    def chunk_zTXt(self, pos: int, length: int) -> bytes:\n        # compressed text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            k = s\n            v = b\"\"\n        if v:\n            comp_method = v[0]\n        else:\n            comp_method = 0\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in zTXt chunk\"\n            raise SyntaxError(msg)\n        try:\n            v = _safe_zlib_decompress(v[1:])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                v = b\"\"\n            else:\n                raise\n        except zlib.error:\n            v = b\"\"\n\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = self.im_text[k] = v\n            self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_iTXt(self, pos: int, length: int) -> bytes:\n        # international text\n        r = s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, r = r.split(b\"\\0\", 1)\n        except ValueError:\n            return s\n        if len(r) < 2:\n            return s\n        cf, cm, r = r[0], r[1], r[2:]\n        try:\n            lang, tk, v = r.split(b\"\\0\", 2)\n        except ValueError:\n            return s\n        if cf != 0:\n            if cm == 0:\n                try:\n                    v = _safe_zlib_decompress(v)\n                except ValueError:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        return s\n                    else:\n                        raise\n                except zlib.error:\n                    return s\n            else:\n                return s\n        try:\n            k = k.decode(\"latin-1\", \"strict\")\n            lang = lang.decode(\"utf-8\", \"strict\")\n            tk = tk.decode(\"utf-8\", \"strict\")\n            v = v.decode(\"utf-8\", \"strict\")\n        except UnicodeError:\n            return s\n\n        self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)\n        self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_eXIf(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"exif\"] = b\"Exif\\x00\\x00\" + s\n        return s\n\n    # APNG chunks\n    def chunk_acTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 8:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated acTL chunk\"\n            raise ValueError(msg)\n        if self.im_n_frames is not None:\n            self.im_n_frames = None\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        n_frames = i32(s)\n        if n_frames == 0 or n_frames > 0x80000000:\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        self.im_n_frames = n_frames\n        self.im_info[\"loop\"] = i32(s, 4)\n        self.im_custom_mimetype = \"image/apng\"\n        return s\n\n    def chunk_fcTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 26:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated fcTL chunk\"\n            raise ValueError(msg)\n        seq = i32(s)\n        if (self._seq_num is None and seq != 0) or (\n            self._seq_num is not None and self._seq_num != seq - 1\n        ):\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        width, height = i32(s, 4), i32(s, 8)\n        px, py = i32(s, 12), i32(s, 16)\n        im_w, im_h = self.im_size\n        if px + width > im_w or py + height > im_h:\n            msg = \"APNG contains invalid frames\"\n            raise SyntaxError(msg)\n        self.im_info[\"bbox\"] = (px, py, px + width, py + height)\n        delay_num, delay_den = i16(s, 20), i16(s, 22)\n        if delay_den == 0:\n            delay_den = 100\n        self.im_info[\"duration\"] = float(delay_num) / float(delay_den) * 1000\n        self.im_info[\"disposal\"] = s[24]\n        self.im_info[\"blend\"] = s[25]\n        return s\n\n    def chunk_fdAT(self, pos: int, length: int) -> bytes:\n        if length < 4:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                s = ImageFile._safe_read(self.fp, length)\n                return s\n            msg = \"APNG contains truncated fDAT chunk\"\n            raise ValueError(msg)\n        s = ImageFile._safe_read(self.fp, 4)\n        seq = i32(s)\n        if self._seq_num != seq - 1:\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        return self.chunk_IDAT(pos + 4, length - 4)\n\n\n# --------------------------------------------------------------------\n# PNG reader\n\n\ndef _accept(prefix: bytes) -> bool:\n    return prefix[:8] == _MAGIC\n\n\n##\n# Image plugin for PNG images.\n\n\nclass PngImageFile(ImageFile.ImageFile):\n    format = \"PNG\"\n    format_description = \"Portable network graphics\"\n\n    def _open(self) -> None:\n        if not _accept(self.fp.read(8)):\n            msg = \"not a PNG file\"\n            raise SyntaxError(msg)\n        self._fp = self.fp\n        self.__frame = 0\n\n        #\n        # Parse headers up to the first IDAT or fDAT chunk\n\n        self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []\n        self.png: PngStream | None = PngStream(self.fp)\n\n        while True:\n            #\n            # get next chunk\n\n            cid, pos, length = self.png.read()\n\n            try:\n                s = self.png.call(cid, pos, length)\n            except EOFError:\n                break\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s))\n\n            self.png.crc(cid, s)\n\n        #\n        # Copy relevant attributes from the PngStream.  An alternative\n        # would be to let the PngStream class modify these attributes\n        # directly, but that introduces circular references which are\n        # difficult to break if things go wrong in the decoder...\n        # (believe me, I've tried ;-)\n\n        self._mode = self.png.im_mode\n        self._size = self.png.im_size\n        self.info = self.png.im_info\n        self._text = None\n        self.tile = self.png.im_tile\n        self.custom_mimetype = self.png.im_custom_mimetype\n        self.n_frames = self.png.im_n_frames or 1\n        self.default_image = self.info.get(\"default_image\", False)\n\n        if self.png.im_palette:\n            rawmode, data = self.png.im_palette\n            self.palette = ImagePalette.raw(rawmode, data)\n\n        if cid == b\"fdAT\":\n            self.__prepare_idat = length - 4\n        else:\n            self.__prepare_idat = length  # used by load_prepare()\n\n        if self.png.im_n_frames is not None:\n            self._close_exclusive_fp_after_loading = False\n            self.png.save_rewind()\n            self.__rewind_idat = self.__prepare_idat\n            self.__rewind = self._fp.tell()\n            if self.default_image:\n                # IDAT chunk contains default image and not first animation frame\n                self.n_frames += 1\n            self._seek(0)\n        self.is_animated = self.n_frames > 1\n\n    @property\n    def text(self):\n        # experimental\n        if self._text is None:\n            # iTxt, tEXt and zTXt chunks may appear at the end of the file\n            # So load the file to ensure that they are read\n            if self.is_animated:\n                frame = self.__frame\n                # for APNG, seek to the final frame before loading\n                self.seek(self.n_frames - 1)\n            self.load()\n            if self.is_animated:\n                self.seek(frame)\n        return self._text\n\n    def verify(self) -> None:\n        \"\"\"Verify PNG file\"\"\"\n\n        if self.fp is None:\n            msg = \"verify must be called directly after open\"\n            raise RuntimeError(msg)\n\n        # back up to beginning of IDAT block\n        self.fp.seek(self.tile[0][2] - 8)\n\n        assert self.png is not None\n        self.png.verify()\n        self.png.close()\n\n        if self._exclusive_fp:\n            self.fp.close()\n        self.fp = None\n\n    def seek(self, frame: int) -> None:\n        if not self._seek_check(frame):\n            return\n        if frame < self.__frame:\n            self._seek(0, True)\n\n        last_frame = self.__frame\n        for f in range(self.__frame + 1, frame + 1):\n            try:\n                self._seek(f)\n            except EOFError as e:\n                self.seek(last_frame)\n                msg = \"no more images in APNG file\"\n                raise EOFError(msg) from e\n\n    def _seek(self, frame, rewind=False):\n        if frame == 0:\n            if rewind:\n                self._fp.seek(self.__rewind)\n                self.png.rewind()\n                self.__prepare_idat = self.__rewind_idat\n                self.im = None\n                if self.pyaccess:\n                    self.pyaccess = None\n                self.info = self.png.im_info\n                self.tile = self.png.im_tile\n                self.fp = self._fp\n            self._prev_im = None\n            self.dispose = None\n            self.default_image = self.info.get(\"default_image\", False)\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n            self.__frame = 0\n        else:\n            if frame != self.__frame + 1:\n                msg = f\"cannot seek to frame {frame}\"\n                raise ValueError(msg)\n\n            # ensure previous frame was loaded\n            self.load()\n\n            if self.dispose:\n                self.im.paste(self.dispose, self.dispose_extent)\n            self._prev_im = self.im.copy()\n\n            self.fp = self._fp\n\n            # advance to the next frame\n            if self.__prepare_idat:\n                ImageFile._safe_read(self.fp, self.__prepare_idat)\n                self.__prepare_idat = 0\n            frame_start = False\n            while True:\n                self.fp.read(4)  # CRC\n\n                try:\n                    cid, pos, length = self.png.read()\n                except (struct.error, SyntaxError):\n                    break\n\n                if cid == b\"IEND\":\n                    msg = \"No more images in APNG file\"\n                    raise EOFError(msg)\n                if cid == b\"fcTL\":\n                    if frame_start:\n                        # there must be at least one fdAT chunk between fcTL chunks\n                        msg = \"APNG missing frame data\"\n                        raise SyntaxError(msg)\n                    frame_start = True\n\n                try:\n                    self.png.call(cid, pos, length)\n                except UnicodeDecodeError:\n                    break\n                except EOFError:\n                    if cid == b\"fdAT\":\n                        length -= 4\n                        if frame_start:\n                            self.__prepare_idat = length\n                            break\n                    ImageFile._safe_read(self.fp, length)\n                except AttributeError:\n                    logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                    ImageFile._safe_read(self.fp, length)\n\n            self.__frame = frame\n            self.tile = self.png.im_tile\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n\n            if not self.tile:\n                msg = \"image not found in APNG frame\"\n                raise EOFError(msg)\n\n        # setup frame disposal (actual disposal done when needed in the next _seek())\n        if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose_op = Disposal.OP_BACKGROUND\n\n        if self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose = self._prev_im.copy()\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        elif self.dispose_op == Disposal.OP_BACKGROUND:\n            self.dispose = Image.core.fill(self.mode, self.size)\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        else:\n            self.dispose = None\n\n    def tell(self) -> int:\n        return self.__frame\n\n    def load_prepare(self) -> None:\n        \"\"\"internal: prepare to read PNG file\"\"\"\n\n        if self.info.get(\"interlace\"):\n            self.decoderconfig = self.decoderconfig + (1,)\n\n        self.__idat = self.__prepare_idat  # used by load_read()\n        ImageFile.ImageFile.load_prepare(self)\n\n    def load_read(self, read_bytes: int) -> bytes:\n        \"\"\"internal: read more image data\"\"\"\n\n        assert self.png is not None\n        while self.__idat == 0:\n            # end of chunk, skip forward to next one\n\n            self.fp.read(4)  # CRC\n\n            cid, pos, length = self.png.read()\n\n            if cid not in [b\"IDAT\", b\"DDAT\", b\"fdAT\"]:\n                self.png.push(cid, pos, length)\n                return b\"\"\n\n            if cid == b\"fdAT\":\n                try:\n                    self.png.call(cid, pos, length)\n                except EOFError:\n                    pass\n                self.__idat = length - 4  # sequence_num has already been read\n            else:\n                self.__idat = length  # empty chunks are allowed\n\n        # read more data from this chunk\n        if read_bytes <= 0:\n            read_bytes = self.__idat\n        else:\n            read_bytes = min(read_bytes, self.__idat)\n\n        self.__idat = self.__idat - read_bytes\n\n        return self.fp.read(read_bytes)\n\n    def load_end(self) -> None:\n        \"\"\"internal: finished reading image data\"\"\"\n        assert self.png is not None\n        if self.__idat != 0:\n            self.fp.read(self.__idat)\n        while True:\n            self.fp.read(4)  # CRC\n\n            try:\n                cid, pos, length = self.png.read()\n            except (struct.error, SyntaxError):\n                break\n\n            if cid == b\"IEND\":\n                break\n            elif cid == b\"fcTL\" and self.is_animated:\n                # start of the next frame, stop reading\n                self.__prepare_idat = 0\n                self.png.push(cid, pos, length)\n                break\n\n            try:\n                self.png.call(cid, pos, length)\n            except UnicodeDecodeError:\n                break\n            except EOFError:\n                if cid == b\"fdAT\":\n                    length -= 4\n                try:\n                    ImageFile._safe_read(self.fp, length)\n                except OSError as e:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        break\n                    else:\n                        raise e\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s, True))\n        self._text = self.png.im_text\n        if not self.is_animated:\n            self.png.close()\n            self.png = None\n        else:\n            if self._prev_im and self.blend_op == Blend.OP_OVER:\n                updated = self._crop(self.im, self.dispose_extent)\n                if self.im.mode == \"RGB\" and \"transparency\" in self.info:\n                    mask = updated.convert_transparent(\n                        \"RGBA\", self.info[\"transparency\"]\n                    )\n                else:\n                    mask = updated.convert(\"RGBA\")\n                self._prev_im.paste(updated, self.dispose_extent, mask)\n                self.im = self._prev_im\n                if self.pyaccess:\n                    self.pyaccess = None\n\n    def _getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n        if \"exif\" not in self.info and \"Raw profile type exif\" not in self.info:\n            return None\n        return self.getexif()._get_merged_dict()\n\n    def getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n\n        return super().getexif()\n\n    def getxmp(self):\n        \"\"\"\n        Returns a dictionary containing the XMP tags.\n        Requires defusedxml to be installed.\n\n        :returns: XMP tags in a dictionary.\n        \"\"\"\n        return (\n            self._getxmp(self.info[\"XML:com.adobe.xmp\"])\n            if \"XML:com.adobe.xmp\" in self.info\n            else {}\n        )\n\n\n# --------------------------------------------------------------------\n# PNG writer\n\n_OUTMODES = {\n    # supported PIL modes, and corresponding rawmode, bit depth and color type\n    \"1\": (\"1\", b\"\\x01\", b\"\\x00\"),\n    \"L;1\": (\"L;1\", b\"\\x01\", b\"\\x00\"),\n    \"L;2\": (\"L;2\", b\"\\x02\", b\"\\x00\"),\n    \"L;4\": (\"L;4\", b\"\\x04\", b\"\\x00\"),\n    \"L\": (\"L\", b\"\\x08\", b\"\\x00\"),\n    \"LA\": (\"LA\", b\"\\x08\", b\"\\x04\"),\n    \"I\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16B\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"P;1\": (\"P;1\", b\"\\x01\", b\"\\x03\"),\n    \"P;2\": (\"P;2\", b\"\\x02\", b\"\\x03\"),\n    \"P;4\": (\"P;4\", b\"\\x04\", b\"\\x03\"),\n    \"P\": (\"P\", b\"\\x08\", b\"\\x03\"),\n    \"RGB\": (\"RGB\", b\"\\x08\", b\"\\x02\"),\n    \"RGBA\": (\"RGBA\", b\"\\x08\", b\"\\x06\"),\n}\n\n\ndef putchunk(fp, cid, *data):\n    \"\"\"Write a PNG chunk (including CRC field)\"\"\"\n\n    data = b\"\".join(data)\n\n    fp.write(o32(len(data)) + cid)\n    fp.write(data)\n    crc = _crc32(data, _crc32(cid))\n    fp.write(o32(crc))\n\n\nclass _idat:\n    # wrap output from the encoder in IDAT chunks\n\n    def __init__(self, fp, chunk):\n        self.fp = fp\n        self.chunk = chunk\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"IDAT\", data)\n\n\nclass _fdat:\n    # wrap encoder output in fdAT chunks\n\n    def __init__(self, fp, chunk, seq_num):\n        self.fp = fp\n        self.chunk = chunk\n        self.seq_num = seq_num\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"fdAT\", o32(self.seq_num), data)\n        self.seq_num += 1\n\n\ndef _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == rawmode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(rawmode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != rawmode:\n            im = im.convert(rawmode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num\n\n\ndef _save_all(im, fp, filename):\n    _save(im, fp, filename, save_all=True)\n\n\ndef _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            mode = f\"{mode};{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[mode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()\n\n\n# --------------------------------------------------------------------\n# PNG chunk converter\n\n\ndef getchunks(im, **params):\n    \"\"\"Return a list of PNG chunks representing this image.\"\"\"\n\n    class collector:\n        data = []\n\n        def write(self, data: bytes) -> None:\n            pass\n\n        def append(self, chunk: bytes) -> None:\n            self.data.append(chunk)\n\n    def append(fp, cid, *data):\n        data = b\"\".join(data)\n        crc = o32(_crc32(data, _crc32(cid)))\n        fp.append((cid, data, crc))\n\n    fp = collector()\n\n    try:\n        im.encoderinfo = params\n        _save(im, fp, None, append)\n    finally:\n        del im.encoderinfo\n\n    return fp.data\n\n\n# --------------------------------------------------------------------\n# Registry\n\nImage.register_open(PngImageFile.format, PngImageFile, _accept)\nImage.register_save(PngImageFile.format, _save)\nImage.register_save_all(PngImageFile.format, _save_all)\n\nImage.register_extensions(PngImageFile.format, [\".png\", \".apng\"])\n\nImage.register_mime(PngImageFile.format, \"image/png\")\n",
            "new_file_content": "#\n# The Python Imaging Library.\n# $Id$\n#\n# PNG support code\n#\n# See \"PNG (Portable Network Graphics) Specification, version 1.0;\n# W3C Recommendation\", 1996-10-01, Thomas Boutell (ed.).\n#\n# history:\n# 1996-05-06 fl   Created (couldn't resist it)\n# 1996-12-14 fl   Upgraded, added read and verify support (0.2)\n# 1996-12-15 fl   Separate PNG stream parser\n# 1996-12-29 fl   Added write support, added getchunks\n# 1996-12-30 fl   Eliminated circular references in decoder (0.3)\n# 1998-07-12 fl   Read/write 16-bit images as mode I (0.4)\n# 2001-02-08 fl   Added transparency support (from Zircon) (0.5)\n# 2001-04-16 fl   Don't close data source in \"open\" method (0.6)\n# 2004-02-24 fl   Don't even pretend to support interlaced files (0.7)\n# 2004-08-31 fl   Do basic sanity check on chunk identifiers (0.8)\n# 2004-09-20 fl   Added PngInfo chunk container\n# 2004-12-18 fl   Added DPI read support (based on code by Niki Spahiev)\n# 2008-08-13 fl   Added tRNS support for RGB images\n# 2009-03-06 fl   Support for preserving ICC profiles (by Florian Hoech)\n# 2009-03-08 fl   Added zTXT support (from Lowell Alleman)\n# 2009-03-29 fl   Read interlaced PNG files (from Conrado Porto Lopes Gouvua)\n#\n# Copyright (c) 1997-2009 by Secret Labs AB\n# Copyright (c) 1996 by Fredrik Lundh\n#\n# See the README file for information on usage and redistribution.\n#\nfrom __future__ import annotations\n\nimport itertools\nimport logging\nimport re\nimport struct\nimport warnings\nimport zlib\nfrom enum import IntEnum\nfrom typing import IO\n\nfrom . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence\nfrom ._binary import i16be as i16\nfrom ._binary import i32be as i32\nfrom ._binary import o8\nfrom ._binary import o16be as o16\nfrom ._binary import o32be as o32\n\nlogger = logging.getLogger(__name__)\n\nis_cid = re.compile(rb\"\\w\\w\\w\\w\").match\n\n\n_MAGIC = b\"\\211PNG\\r\\n\\032\\n\"\n\n\n_MODES = {\n    # supported bits/color combinations, and corresponding modes/rawmodes\n    # Grayscale\n    (1, 0): (\"1\", \"1\"),\n    (2, 0): (\"L\", \"L;2\"),\n    (4, 0): (\"L\", \"L;4\"),\n    (8, 0): (\"L\", \"L\"),\n    (16, 0): (\"I;16\", \"I;16B\"),\n    # Truecolour\n    (8, 2): (\"RGB\", \"RGB\"),\n    (16, 2): (\"RGB\", \"RGB;16B\"),\n    # Indexed-colour\n    (1, 3): (\"P\", \"P;1\"),\n    (2, 3): (\"P\", \"P;2\"),\n    (4, 3): (\"P\", \"P;4\"),\n    (8, 3): (\"P\", \"P\"),\n    # Grayscale with alpha\n    (8, 4): (\"LA\", \"LA\"),\n    (16, 4): (\"RGBA\", \"LA;16B\"),  # LA;16B->LA not yet available\n    # Truecolour with alpha\n    (8, 6): (\"RGBA\", \"RGBA\"),\n    (16, 6): (\"RGBA\", \"RGBA;16B\"),\n}\n\n\n_simple_palette = re.compile(b\"^\\xff*\\x00\\xff*$\")\n\nMAX_TEXT_CHUNK = ImageFile.SAFEBLOCK\n\"\"\"\nMaximum decompressed size for a iTXt or zTXt chunk.\nEliminates decompression bombs where compressed chunks can expand 1000x.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\nMAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK\n\"\"\"\nSet the maximum total text chunk size.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\n\n\n# APNG frame disposal modes\nclass Disposal(IntEnum):\n    OP_NONE = 0\n    \"\"\"\n    No disposal is done on this frame before rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_BACKGROUND = 1\n    \"\"\"\n    This frame’s modified region is cleared to fully transparent black before rendering\n    the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_PREVIOUS = 2\n    \"\"\"\n    This frame’s modified region is reverted to the previous frame’s contents before\n    rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\n# APNG frame blend modes\nclass Blend(IntEnum):\n    OP_SOURCE = 0\n    \"\"\"\n    All color components of this frame, including alpha, overwrite the previous output\n    image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_OVER = 1\n    \"\"\"\n    This frame should be alpha composited with the previous output image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\ndef _safe_zlib_decompress(s):\n    dobj = zlib.decompressobj()\n    plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)\n    if dobj.unconsumed_tail:\n        msg = \"Decompressed Data Too Large\"\n        raise ValueError(msg)\n    return plaintext\n\n\ndef _crc32(data, seed=0):\n    return zlib.crc32(data, seed) & 0xFFFFFFFF\n\n\n# --------------------------------------------------------------------\n# Support classes.  Suitable for PNG and related formats like MNG etc.\n\n\nclass ChunkStream:\n    def __init__(self, fp: IO[bytes]) -> None:\n        self.fp: IO[bytes] | None = fp\n        self.queue: list[tuple[bytes, int, int]] | None = []\n\n    def read(self) -> tuple[bytes, int, int]:\n        \"\"\"Fetch a new chunk. Returns header information.\"\"\"\n        cid = None\n\n        assert self.fp is not None\n        if self.queue:\n            cid, pos, length = self.queue.pop()\n            self.fp.seek(pos)\n        else:\n            s = self.fp.read(8)\n            cid = s[4:]\n            pos = self.fp.tell()\n            length = i32(s)\n\n        if not is_cid(cid):\n            if not ImageFile.LOAD_TRUNCATED_IMAGES:\n                msg = f\"broken PNG file (chunk {repr(cid)})\"\n                raise SyntaxError(msg)\n\n        return cid, pos, length\n\n    def __enter__(self) -> ChunkStream:\n        return self\n\n    def __exit__(self, *args):\n        self.close()\n\n    def close(self) -> None:\n        self.queue = self.fp = None\n\n    def push(self, cid: bytes, pos: int, length: int) -> None:\n        assert self.queue is not None\n        self.queue.append((cid, pos, length))\n\n    def call(self, cid, pos, length):\n        \"\"\"Call the appropriate chunk handler\"\"\"\n\n        logger.debug(\"STREAM %r %s %s\", cid, pos, length)\n        return getattr(self, f\"chunk_{cid.decode('ascii')}\")(pos, length)\n\n    def crc(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read and verify checksum\"\"\"\n\n        # Skip CRC checks for ancillary chunks if allowed to load truncated\n        # images\n        # 5th byte of first char is 1 [specs, section 5.4]\n        if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):\n            self.crc_skip(cid, data)\n            return\n\n        assert self.fp is not None\n        try:\n            crc1 = _crc32(data, _crc32(cid))\n            crc2 = i32(self.fp.read(4))\n            if crc1 != crc2:\n                msg = f\"broken PNG file (bad header checksum in {repr(cid)})\"\n                raise SyntaxError(msg)\n        except struct.error as e:\n            msg = f\"broken PNG file (incomplete checksum in {repr(cid)})\"\n            raise SyntaxError(msg) from e\n\n    def crc_skip(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read checksum\"\"\"\n\n        assert self.fp is not None\n        self.fp.read(4)\n\n    def verify(self, endchunk: bytes = b\"IEND\") -> list[bytes]:\n        # Simple approach; just calculate checksum for all remaining\n        # blocks.  Must be called directly after open.\n\n        cids = []\n\n        while True:\n            try:\n                cid, pos, length = self.read()\n            except struct.error as e:\n                msg = \"truncated PNG file\"\n                raise OSError(msg) from e\n\n            if cid == endchunk:\n                break\n            self.crc(cid, ImageFile._safe_read(self.fp, length))\n            cids.append(cid)\n\n        return cids\n\n\nclass iTXt(str):\n    \"\"\"\n    Subclass of string to allow iTXt chunks to look like strings while\n    keeping their extra information\n\n    \"\"\"\n\n    @staticmethod\n    def __new__(cls, text, lang=None, tkey=None):\n        \"\"\"\n        :param cls: the class to use when creating the instance\n        :param text: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        \"\"\"\n\n        self = str.__new__(cls, text)\n        self.lang = lang\n        self.tkey = tkey\n        return self\n\n\nclass PngInfo:\n    \"\"\"\n    PNG chunk container (for use with save(pnginfo=))\n\n    \"\"\"\n\n    def __init__(self):\n        self.chunks = []\n\n    def add(self, cid, data, after_idat=False):\n        \"\"\"Appends an arbitrary chunk. Use with caution.\n\n        :param cid: a byte string, 4 bytes long.\n        :param data: a byte string of the encoded data\n        :param after_idat: for use with private chunks. Whether the chunk\n                           should be written after IDAT\n\n        \"\"\"\n\n        chunk = [cid, data]\n        if after_idat:\n            chunk.append(True)\n        self.chunks.append(tuple(chunk))\n\n    def add_itxt(self, key, value, lang=\"\", tkey=\"\", zip=False):\n        \"\"\"Appends an iTXt chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        :param zip: compression flag\n\n        \"\"\"\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n        if not isinstance(value, bytes):\n            value = value.encode(\"utf-8\", \"strict\")\n        if not isinstance(lang, bytes):\n            lang = lang.encode(\"utf-8\", \"strict\")\n        if not isinstance(tkey, bytes):\n            tkey = tkey.encode(\"utf-8\", \"strict\")\n\n        if zip:\n            self.add(\n                b\"iTXt\",\n                key + b\"\\0\\x01\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + zlib.compress(value),\n            )\n        else:\n            self.add(b\"iTXt\", key + b\"\\0\\0\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + value)\n\n    def add_text(self, key, value, zip=False):\n        \"\"\"Appends a text chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key, text or an\n           :py:class:`PIL.PngImagePlugin.iTXt` instance\n        :param zip: compression flag\n\n        \"\"\"\n        if isinstance(value, iTXt):\n            return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)\n\n        # The tEXt chunk stores latin-1 text\n        if not isinstance(value, bytes):\n            try:\n                value = value.encode(\"latin-1\", \"strict\")\n            except UnicodeError:\n                return self.add_itxt(key, value, zip=zip)\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n\n        if zip:\n            self.add(b\"zTXt\", key + b\"\\0\\0\" + zlib.compress(value))\n        else:\n            self.add(b\"tEXt\", key + b\"\\0\" + value)\n\n\n# --------------------------------------------------------------------\n# PNG image stream (IHDR/IEND)\n\n\nclass PngStream(ChunkStream):\n    def __init__(self, fp):\n        super().__init__(fp)\n\n        # local copies of Image attributes\n        self.im_info = {}\n        self.im_text = {}\n        self.im_size = (0, 0)\n        self.im_mode = None\n        self.im_tile = None\n        self.im_palette = None\n        self.im_custom_mimetype = None\n        self.im_n_frames = None\n        self._seq_num = None\n        self.rewind_state = None\n\n        self.text_memory = 0\n\n    def check_text_memory(self, chunklen: int) -> None:\n        self.text_memory += chunklen\n        if self.text_memory > MAX_TEXT_MEMORY:\n            msg = (\n                \"Too much memory used in text chunks: \"\n                f\"{self.text_memory}>MAX_TEXT_MEMORY\"\n            )\n            raise ValueError(msg)\n\n    def save_rewind(self) -> None:\n        self.rewind_state = {\n            \"info\": self.im_info.copy(),\n            \"tile\": self.im_tile,\n            \"seq_num\": self._seq_num,\n        }\n\n    def rewind(self) -> None:\n        self.im_info = self.rewind_state[\"info\"].copy()\n        self.im_tile = self.rewind_state[\"tile\"]\n        self._seq_num = self.rewind_state[\"seq_num\"]\n\n    def chunk_iCCP(self, pos: int, length: int) -> bytes:\n        # ICC profile\n        s = ImageFile._safe_read(self.fp, length)\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        i = s.find(b\"\\0\")\n        logger.debug(\"iCCP profile name %r\", s[:i])\n        comp_method = s[i + 1]\n        logger.debug(\"Compression method %s\", comp_method)\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in iCCP chunk\"\n            raise SyntaxError(msg)\n        try:\n            icc_profile = _safe_zlib_decompress(s[i + 2 :])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                icc_profile = None\n            else:\n                raise\n        except zlib.error:\n            icc_profile = None  # FIXME\n        self.im_info[\"icc_profile\"] = icc_profile\n        return s\n\n    def chunk_IHDR(self, pos: int, length: int) -> bytes:\n        # image header\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 13:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated IHDR chunk\"\n            raise ValueError(msg)\n        self.im_size = i32(s, 0), i32(s, 4)\n        try:\n            self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]\n        except Exception:\n            pass\n        if s[12]:\n            self.im_info[\"interlace\"] = 1\n        if s[11]:\n            msg = \"unknown filter category\"\n            raise SyntaxError(msg)\n        return s\n\n    def chunk_IDAT(self, pos, length):\n        # image data\n        if \"bbox\" in self.im_info:\n            tile = [(\"zip\", self.im_info[\"bbox\"], pos, self.im_rawmode)]\n        else:\n            if self.im_n_frames is not None:\n                self.im_info[\"default_image\"] = True\n            tile = [(\"zip\", (0, 0) + self.im_size, pos, self.im_rawmode)]\n        self.im_tile = tile\n        self.im_idat = length\n        msg = \"image data found\"\n        raise EOFError(msg)\n\n    def chunk_IEND(self, pos, length):\n        msg = \"end of PNG image\"\n        raise EOFError(msg)\n\n    def chunk_PLTE(self, pos: int, length: int) -> bytes:\n        # palette\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            self.im_palette = \"RGB\", s\n        return s\n\n    def chunk_tRNS(self, pos: int, length: int) -> bytes:\n        # transparency\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            if _simple_palette.match(s):\n                # tRNS contains only one full-transparent entry,\n                # other entries are full opaque\n                i = s.find(b\"\\0\")\n                if i >= 0:\n                    self.im_info[\"transparency\"] = i\n            else:\n                # otherwise, we have a byte string with one alpha value\n                # for each palette entry\n                self.im_info[\"transparency\"] = s\n        elif self.im_mode in (\"1\", \"L\", \"I;16\"):\n            self.im_info[\"transparency\"] = i16(s)\n        elif self.im_mode == \"RGB\":\n            self.im_info[\"transparency\"] = i16(s), i16(s, 2), i16(s, 4)\n        return s\n\n    def chunk_gAMA(self, pos: int, length: int) -> bytes:\n        # gamma setting\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"gamma\"] = i32(s) / 100000.0\n        return s\n\n    def chunk_cHRM(self, pos: int, length: int) -> bytes:\n        # chromaticity, 8 unsigned ints, actual value is scaled by 100,000\n        # WP x,y, Red x,y, Green x,y Blue x,y\n\n        s = ImageFile._safe_read(self.fp, length)\n        raw_vals = struct.unpack(\">%dI\" % (len(s) // 4), s)\n        self.im_info[\"chromaticity\"] = tuple(elt / 100000.0 for elt in raw_vals)\n        return s\n\n    def chunk_sRGB(self, pos: int, length: int) -> bytes:\n        # srgb rendering intent, 1 byte\n        # 0 perceptual\n        # 1 relative colorimetric\n        # 2 saturation\n        # 3 absolute colorimetric\n\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 1:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated sRGB chunk\"\n            raise ValueError(msg)\n        self.im_info[\"srgb\"] = s[0]\n        return s\n\n    def chunk_pHYs(self, pos: int, length: int) -> bytes:\n        # pixels per unit\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 9:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated pHYs chunk\"\n            raise ValueError(msg)\n        px, py = i32(s, 0), i32(s, 4)\n        unit = s[8]\n        if unit == 1:  # meter\n            dpi = px * 0.0254, py * 0.0254\n            self.im_info[\"dpi\"] = dpi\n        elif unit == 0:\n            self.im_info[\"aspect\"] = px, py\n        return s\n\n    def chunk_tEXt(self, pos: int, length: int) -> bytes:\n        # text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            # fallback for broken tEXt tags\n            k = s\n            v = b\"\"\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v_str = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = v if k == \"exif\" else v_str\n            self.im_text[k] = v_str\n            self.check_text_memory(len(v_str))\n\n        return s\n\n    def chunk_zTXt(self, pos: int, length: int) -> bytes:\n        # compressed text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            k = s\n            v = b\"\"\n        if v:\n            comp_method = v[0]\n        else:\n            comp_method = 0\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in zTXt chunk\"\n            raise SyntaxError(msg)\n        try:\n            v = _safe_zlib_decompress(v[1:])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                v = b\"\"\n            else:\n                raise\n        except zlib.error:\n            v = b\"\"\n\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = self.im_text[k] = v\n            self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_iTXt(self, pos: int, length: int) -> bytes:\n        # international text\n        r = s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, r = r.split(b\"\\0\", 1)\n        except ValueError:\n            return s\n        if len(r) < 2:\n            return s\n        cf, cm, r = r[0], r[1], r[2:]\n        try:\n            lang, tk, v = r.split(b\"\\0\", 2)\n        except ValueError:\n            return s\n        if cf != 0:\n            if cm == 0:\n                try:\n                    v = _safe_zlib_decompress(v)\n                except ValueError:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        return s\n                    else:\n                        raise\n                except zlib.error:\n                    return s\n            else:\n                return s\n        try:\n            k = k.decode(\"latin-1\", \"strict\")\n            lang = lang.decode(\"utf-8\", \"strict\")\n            tk = tk.decode(\"utf-8\", \"strict\")\n            v = v.decode(\"utf-8\", \"strict\")\n        except UnicodeError:\n            return s\n\n        self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)\n        self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_eXIf(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"exif\"] = b\"Exif\\x00\\x00\" + s\n        return s\n\n    # APNG chunks\n    def chunk_acTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 8:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated acTL chunk\"\n            raise ValueError(msg)\n        if self.im_n_frames is not None:\n            self.im_n_frames = None\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        n_frames = i32(s)\n        if n_frames == 0 or n_frames > 0x80000000:\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        self.im_n_frames = n_frames\n        self.im_info[\"loop\"] = i32(s, 4)\n        self.im_custom_mimetype = \"image/apng\"\n        return s\n\n    def chunk_fcTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 26:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated fcTL chunk\"\n            raise ValueError(msg)\n        seq = i32(s)\n        if (self._seq_num is None and seq != 0) or (\n            self._seq_num is not None and self._seq_num != seq - 1\n        ):\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        width, height = i32(s, 4), i32(s, 8)\n        px, py = i32(s, 12), i32(s, 16)\n        im_w, im_h = self.im_size\n        if px + width > im_w or py + height > im_h:\n            msg = \"APNG contains invalid frames\"\n            raise SyntaxError(msg)\n        self.im_info[\"bbox\"] = (px, py, px + width, py + height)\n        delay_num, delay_den = i16(s, 20), i16(s, 22)\n        if delay_den == 0:\n            delay_den = 100\n        self.im_info[\"duration\"] = float(delay_num) / float(delay_den) * 1000\n        self.im_info[\"disposal\"] = s[24]\n        self.im_info[\"blend\"] = s[25]\n        return s\n\n    def chunk_fdAT(self, pos: int, length: int) -> bytes:\n        if length < 4:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                s = ImageFile._safe_read(self.fp, length)\n                return s\n            msg = \"APNG contains truncated fDAT chunk\"\n            raise ValueError(msg)\n        s = ImageFile._safe_read(self.fp, 4)\n        seq = i32(s)\n        if self._seq_num != seq - 1:\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        return self.chunk_IDAT(pos + 4, length - 4)\n\n\n# --------------------------------------------------------------------\n# PNG reader\n\n\ndef _accept(prefix: bytes) -> bool:\n    return prefix[:8] == _MAGIC\n\n\n##\n# Image plugin for PNG images.\n\n\nclass PngImageFile(ImageFile.ImageFile):\n    format = \"PNG\"\n    format_description = \"Portable network graphics\"\n\n    def _open(self) -> None:\n        if not _accept(self.fp.read(8)):\n            msg = \"not a PNG file\"\n            raise SyntaxError(msg)\n        self._fp = self.fp\n        self.__frame = 0\n\n        #\n        # Parse headers up to the first IDAT or fDAT chunk\n\n        self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []\n        self.png: PngStream | None = PngStream(self.fp)\n\n        while True:\n            #\n            # get next chunk\n\n            cid, pos, length = self.png.read()\n\n            try:\n                s = self.png.call(cid, pos, length)\n            except EOFError:\n                break\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s))\n\n            self.png.crc(cid, s)\n\n        #\n        # Copy relevant attributes from the PngStream.  An alternative\n        # would be to let the PngStream class modify these attributes\n        # directly, but that introduces circular references which are\n        # difficult to break if things go wrong in the decoder...\n        # (believe me, I've tried ;-)\n\n        self._mode = self.png.im_mode\n        self._size = self.png.im_size\n        self.info = self.png.im_info\n        self._text = None\n        self.tile = self.png.im_tile\n        self.custom_mimetype = self.png.im_custom_mimetype\n        self.n_frames = self.png.im_n_frames or 1\n        self.default_image = self.info.get(\"default_image\", False)\n\n        if self.png.im_palette:\n            rawmode, data = self.png.im_palette\n            self.palette = ImagePalette.raw(rawmode, data)\n\n        if cid == b\"fdAT\":\n            self.__prepare_idat = length - 4\n        else:\n            self.__prepare_idat = length  # used by load_prepare()\n\n        if self.png.im_n_frames is not None:\n            self._close_exclusive_fp_after_loading = False\n            self.png.save_rewind()\n            self.__rewind_idat = self.__prepare_idat\n            self.__rewind = self._fp.tell()\n            if self.default_image:\n                # IDAT chunk contains default image and not first animation frame\n                self.n_frames += 1\n            self._seek(0)\n        self.is_animated = self.n_frames > 1\n\n    @property\n    def text(self):\n        # experimental\n        if self._text is None:\n            # iTxt, tEXt and zTXt chunks may appear at the end of the file\n            # So load the file to ensure that they are read\n            if self.is_animated:\n                frame = self.__frame\n                # for APNG, seek to the final frame before loading\n                self.seek(self.n_frames - 1)\n            self.load()\n            if self.is_animated:\n                self.seek(frame)\n        return self._text\n\n    def verify(self) -> None:\n        \"\"\"Verify PNG file\"\"\"\n\n        if self.fp is None:\n            msg = \"verify must be called directly after open\"\n            raise RuntimeError(msg)\n\n        # back up to beginning of IDAT block\n        self.fp.seek(self.tile[0][2] - 8)\n\n        assert self.png is not None\n        self.png.verify()\n        self.png.close()\n\n        if self._exclusive_fp:\n            self.fp.close()\n        self.fp = None\n\n    def seek(self, frame: int) -> None:\n        if not self._seek_check(frame):\n            return\n        if frame < self.__frame:\n            self._seek(0, True)\n\n        last_frame = self.__frame\n        for f in range(self.__frame + 1, frame + 1):\n            try:\n                self._seek(f)\n            except EOFError as e:\n                self.seek(last_frame)\n                msg = \"no more images in APNG file\"\n                raise EOFError(msg) from e\n\n    def _seek(self, frame, rewind=False):\n        if frame == 0:\n            if rewind:\n                self._fp.seek(self.__rewind)\n                self.png.rewind()\n                self.__prepare_idat = self.__rewind_idat\n                self.im = None\n                if self.pyaccess:\n                    self.pyaccess = None\n                self.info = self.png.im_info\n                self.tile = self.png.im_tile\n                self.fp = self._fp\n            self._prev_im = None\n            self.dispose = None\n            self.default_image = self.info.get(\"default_image\", False)\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n            self.__frame = 0\n        else:\n            if frame != self.__frame + 1:\n                msg = f\"cannot seek to frame {frame}\"\n                raise ValueError(msg)\n\n            # ensure previous frame was loaded\n            self.load()\n\n            if self.dispose:\n                self.im.paste(self.dispose, self.dispose_extent)\n            self._prev_im = self.im.copy()\n\n            self.fp = self._fp\n\n            # advance to the next frame\n            if self.__prepare_idat:\n                ImageFile._safe_read(self.fp, self.__prepare_idat)\n                self.__prepare_idat = 0\n            frame_start = False\n            while True:\n                self.fp.read(4)  # CRC\n\n                try:\n                    cid, pos, length = self.png.read()\n                except (struct.error, SyntaxError):\n                    break\n\n                if cid == b\"IEND\":\n                    msg = \"No more images in APNG file\"\n                    raise EOFError(msg)\n                if cid == b\"fcTL\":\n                    if frame_start:\n                        # there must be at least one fdAT chunk between fcTL chunks\n                        msg = \"APNG missing frame data\"\n                        raise SyntaxError(msg)\n                    frame_start = True\n\n                try:\n                    self.png.call(cid, pos, length)\n                except UnicodeDecodeError:\n                    break\n                except EOFError:\n                    if cid == b\"fdAT\":\n                        length -= 4\n                        if frame_start:\n                            self.__prepare_idat = length\n                            break\n                    ImageFile._safe_read(self.fp, length)\n                except AttributeError:\n                    logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                    ImageFile._safe_read(self.fp, length)\n\n            self.__frame = frame\n            self.tile = self.png.im_tile\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n\n            if not self.tile:\n                msg = \"image not found in APNG frame\"\n                raise EOFError(msg)\n\n        # setup frame disposal (actual disposal done when needed in the next _seek())\n        if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose_op = Disposal.OP_BACKGROUND\n\n        if self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose = self._prev_im.copy()\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        elif self.dispose_op == Disposal.OP_BACKGROUND:\n            self.dispose = Image.core.fill(self.mode, self.size)\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        else:\n            self.dispose = None\n\n    def tell(self) -> int:\n        return self.__frame\n\n    def load_prepare(self) -> None:\n        \"\"\"internal: prepare to read PNG file\"\"\"\n\n        if self.info.get(\"interlace\"):\n            self.decoderconfig = self.decoderconfig + (1,)\n\n        self.__idat = self.__prepare_idat  # used by load_read()\n        ImageFile.ImageFile.load_prepare(self)\n\n    def load_read(self, read_bytes: int) -> bytes:\n        \"\"\"internal: read more image data\"\"\"\n\n        assert self.png is not None\n        while self.__idat == 0:\n            # end of chunk, skip forward to next one\n\n            self.fp.read(4)  # CRC\n\n            cid, pos, length = self.png.read()\n\n            if cid not in [b\"IDAT\", b\"DDAT\", b\"fdAT\"]:\n                self.png.push(cid, pos, length)\n                return b\"\"\n\n            if cid == b\"fdAT\":\n                try:\n                    self.png.call(cid, pos, length)\n                except EOFError:\n                    pass\n                self.__idat = length - 4  # sequence_num has already been read\n            else:\n                self.__idat = length  # empty chunks are allowed\n\n        # read more data from this chunk\n        if read_bytes <= 0:\n            read_bytes = self.__idat\n        else:\n            read_bytes = min(read_bytes, self.__idat)\n\n        self.__idat = self.__idat - read_bytes\n\n        return self.fp.read(read_bytes)\n\n    def load_end(self) -> None:\n        \"\"\"internal: finished reading image data\"\"\"\n        assert self.png is not None\n        if self.__idat != 0:\n            self.fp.read(self.__idat)\n        while True:\n            self.fp.read(4)  # CRC\n\n            try:\n                cid, pos, length = self.png.read()\n            except (struct.error, SyntaxError):\n                break\n\n            if cid == b\"IEND\":\n                break\n            elif cid == b\"fcTL\" and self.is_animated:\n                # start of the next frame, stop reading\n                self.__prepare_idat = 0\n                self.png.push(cid, pos, length)\n                break\n\n            try:\n                self.png.call(cid, pos, length)\n            except UnicodeDecodeError:\n                break\n            except EOFError:\n                if cid == b\"fdAT\":\n                    length -= 4\n                try:\n                    ImageFile._safe_read(self.fp, length)\n                except OSError as e:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        break\n                    else:\n                        raise e\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s, True))\n        self._text = self.png.im_text\n        if not self.is_animated:\n            self.png.close()\n            self.png = None\n        else:\n            if self._prev_im and self.blend_op == Blend.OP_OVER:\n                updated = self._crop(self.im, self.dispose_extent)\n                if self.im.mode == \"RGB\" and \"transparency\" in self.info:\n                    mask = updated.convert_transparent(\n                        \"RGBA\", self.info[\"transparency\"]\n                    )\n                else:\n                    mask = updated.convert(\"RGBA\")\n                self._prev_im.paste(updated, self.dispose_extent, mask)\n                self.im = self._prev_im\n                if self.pyaccess:\n                    self.pyaccess = None\n\n    def _getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n        if \"exif\" not in self.info and \"Raw profile type exif\" not in self.info:\n            return None\n        return self.getexif()._get_merged_dict()\n\n    def getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n\n        return super().getexif()\n\n    def getxmp(self):\n        \"\"\"\n        Returns a dictionary containing the XMP tags.\n        Requires defusedxml to be installed.\n\n        :returns: XMP tags in a dictionary.\n        \"\"\"\n        return (\n            self._getxmp(self.info[\"XML:com.adobe.xmp\"])\n            if \"XML:com.adobe.xmp\" in self.info\n            else {}\n        )\n\n\n# --------------------------------------------------------------------\n# PNG writer\n\n_OUTMODES = {\n    # supported PIL modes, and corresponding rawmode, bit depth and color type\n    \"1\": (\"1\", b\"\\x01\", b\"\\x00\"),\n    \"L;1\": (\"L;1\", b\"\\x01\", b\"\\x00\"),\n    \"L;2\": (\"L;2\", b\"\\x02\", b\"\\x00\"),\n    \"L;4\": (\"L;4\", b\"\\x04\", b\"\\x00\"),\n    \"L\": (\"L\", b\"\\x08\", b\"\\x00\"),\n    \"LA\": (\"LA\", b\"\\x08\", b\"\\x04\"),\n    \"I\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16B\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"P;1\": (\"P;1\", b\"\\x01\", b\"\\x03\"),\n    \"P;2\": (\"P;2\", b\"\\x02\", b\"\\x03\"),\n    \"P;4\": (\"P;4\", b\"\\x04\", b\"\\x03\"),\n    \"P\": (\"P\", b\"\\x08\", b\"\\x03\"),\n    \"RGB\": (\"RGB\", b\"\\x08\", b\"\\x02\"),\n    \"RGBA\": (\"RGBA\", b\"\\x08\", b\"\\x06\"),\n}\n\n\ndef putchunk(fp, cid, *data):\n    \"\"\"Write a PNG chunk (including CRC field)\"\"\"\n\n    data = b\"\".join(data)\n\n    fp.write(o32(len(data)) + cid)\n    fp.write(data)\n    crc = _crc32(data, _crc32(cid))\n    fp.write(o32(crc))\n\n\nclass _idat:\n    # wrap output from the encoder in IDAT chunks\n\n    def __init__(self, fp, chunk):\n        self.fp = fp\n        self.chunk = chunk\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"IDAT\", data)\n\n\nclass _fdat:\n    # wrap encoder output in fdAT chunks\n\n    def __init__(self, fp, chunk, seq_num):\n        self.fp = fp\n        self.chunk = chunk\n        self.seq_num = seq_num\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"fdAT\", o32(self.seq_num), data)\n        self.seq_num += 1\n\n\ndef _write_multiple_frames(im, fp, chunk, mode, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == mode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(mode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != mode:\n            im = im.convert(mode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num\n\n\ndef _save_all(im, fp, filename):\n    _save(im, fp, filename, save_all=True)\n\n\ndef _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    outmode = mode\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            outmode += f\";{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[outmode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, mode, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()\n\n\n# --------------------------------------------------------------------\n# PNG chunk converter\n\n\ndef getchunks(im, **params):\n    \"\"\"Return a list of PNG chunks representing this image.\"\"\"\n\n    class collector:\n        data = []\n\n        def write(self, data: bytes) -> None:\n            pass\n\n        def append(self, chunk: bytes) -> None:\n            self.data.append(chunk)\n\n    def append(fp, cid, *data):\n        data = b\"\".join(data)\n        crc = o32(_crc32(data, _crc32(cid)))\n        fp.append((cid, data, crc))\n\n    fp = collector()\n\n    try:\n        im.encoderinfo = params\n        _save(im, fp, None, append)\n    finally:\n        del im.encoderinfo\n\n    return fp.data\n\n\n# --------------------------------------------------------------------\n# Registry\n\nImage.register_open(PngImageFile.format, PngImageFile, _accept)\nImage.register_save(PngImageFile.format, _save)\nImage.register_save_all(PngImageFile.format, _save_all)\n\nImage.register_extensions(PngImageFile.format, [\".png\", \".apng\"])\n\nImage.register_mime(PngImageFile.format, \"image/png\")\n",
            "header": {
                "file": {
                    "path": "src/PIL/PngImagePlugin.py"
                },
                "misc_line": null
            },
            "index_line": {
                "old_commit_hash": "0d5751962",
                "new_commit_hash": "6b6c01d1b",
                "mode": "100644"
            },
            "is_binary_file": false,
            "binary_line": null,
            "minus_file": {
                "path": "a/src/PIL/PngImagePlugin.py"
            },
            "plus_file": {
                "path": "b/src/PIL/PngImagePlugin.py"
            },
            "hunks": [
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1104,
                            "length": 7
                        },
                        "new_range": {
                            "start": 1104,
                            "length": 7
                        },
                        "section": "class _fdat:"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        self.seq_num += 1",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):",
                                "type": "deleted"
                            },
                            {
                                "content": "def _write_multiple_frames(im, fp, chunk, mode, rawmode, default_image, append_images):",
                                "type": "added"
                            },
                            {
                                "content": "    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))",
                                "type": "context"
                            },
                            {
                                "content": "    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))",
                                "type": "context"
                            },
                            {
                                "content": "    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_write_multiple_frames",
                            "content": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == rawmode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(rawmode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != rawmode:\n            im = im.convert(rawmode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1107,
                            "end_lineno": 1234,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1119,
                            "length": 10
                        },
                        "new_range": {
                            "start": 1119,
                            "length": 10
                        },
                        "section": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "    frame_count = 0",
                                "type": "context"
                            },
                            {
                                "content": "    for im_seq in chain:",
                                "type": "context"
                            },
                            {
                                "content": "        for im_frame in ImageSequence.Iterator(im_seq):",
                                "type": "context"
                            },
                            {
                                "content": "            if im_frame.mode == rawmode:",
                                "type": "deleted"
                            },
                            {
                                "content": "            if im_frame.mode == mode:",
                                "type": "added"
                            },
                            {
                                "content": "                im_frame = im_frame.copy()",
                                "type": "context"
                            },
                            {
                                "content": "            else:",
                                "type": "context"
                            },
                            {
                                "content": "                im_frame = im_frame.convert(rawmode)",
                                "type": "deleted"
                            },
                            {
                                "content": "                im_frame = im_frame.convert(mode)",
                                "type": "added"
                            },
                            {
                                "content": "            encoderinfo = im.encoderinfo.copy()",
                                "type": "context"
                            },
                            {
                                "content": "            if isinstance(duration, (list, tuple)):",
                                "type": "context"
                            },
                            {
                                "content": "                encoderinfo[\"duration\"] = duration[frame_count]",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_write_multiple_frames",
                            "content": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == rawmode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(rawmode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != rawmode:\n            im = im.convert(rawmode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1107,
                            "end_lineno": 1234,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1184,
                            "length": 8
                        },
                        "new_range": {
                            "start": 1184,
                            "length": 8
                        },
                        "section": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    # default image IDAT (if it exists)",
                                "type": "context"
                            },
                            {
                                "content": "    if default_image:",
                                "type": "context"
                            },
                            {
                                "content": "        if im.mode != rawmode:",
                                "type": "deleted"
                            },
                            {
                                "content": "            im = im.convert(rawmode)",
                                "type": "deleted"
                            },
                            {
                                "content": "        if im.mode != mode:",
                                "type": "added"
                            },
                            {
                                "content": "            im = im.convert(mode)",
                                "type": "added"
                            },
                            {
                                "content": "        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    seq_num = 0",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_write_multiple_frames",
                            "content": "def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == rawmode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(rawmode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != rawmode:\n            im = im.convert(rawmode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1107,
                            "end_lineno": 1234,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1262,
                            "length": 6
                        },
                        "new_range": {
                            "start": 1262,
                            "length": 7
                        },
                        "section": "def _save(im, fp, filename, chunk=putchunk, save_all=False):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "        size = im.size",
                                "type": "context"
                            },
                            {
                                "content": "        mode = im.mode",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    outmode = mode",
                                "type": "added"
                            },
                            {
                                "content": "    if mode == \"P\":",
                                "type": "context"
                            },
                            {
                                "content": "        #",
                                "type": "context"
                            },
                            {
                                "content": "        # attempt to minimize storage requirements for palette images",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_save",
                            "content": "def _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    outmode = mode\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            outmode += f\";{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[outmode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, mode, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1241,
                            "end_lineno": 1436,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1282,
                            "length": 7
                        },
                        "new_range": {
                            "start": 1283,
                            "length": 7
                        },
                        "section": "def _save(im, fp, filename, chunk=putchunk, save_all=False):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "                bits = 2",
                                "type": "context"
                            },
                            {
                                "content": "            else:",
                                "type": "context"
                            },
                            {
                                "content": "                bits = 4",
                                "type": "context"
                            },
                            {
                                "content": "            mode = f\"{mode};{bits}\"",
                                "type": "deleted"
                            },
                            {
                                "content": "            outmode += f\";{bits}\"",
                                "type": "added"
                            },
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    # encoder options",
                                "type": "context"
                            },
                            {
                                "content": "    im.encoderconfig = (",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_save",
                            "content": "def _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            mode = f\"{mode};{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[mode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1241,
                            "end_lineno": 1435,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1294,
                            "length": 7
                        },
                        "new_range": {
                            "start": 1295,
                            "length": 7
                        },
                        "section": "def _save(im, fp, filename, chunk=putchunk, save_all=False):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    # get the corresponding PNG mode",
                                "type": "context"
                            },
                            {
                                "content": "    try:",
                                "type": "context"
                            },
                            {
                                "content": "        rawmode, bit_depth, color_type = _OUTMODES[mode]",
                                "type": "deleted"
                            },
                            {
                                "content": "        rawmode, bit_depth, color_type = _OUTMODES[outmode]",
                                "type": "added"
                            },
                            {
                                "content": "    except KeyError as e:",
                                "type": "context"
                            },
                            {
                                "content": "        msg = f\"cannot write mode {mode} as PNG\"",
                                "type": "context"
                            },
                            {
                                "content": "        raise OSError(msg) from e",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_save",
                            "content": "def _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            mode = f\"{mode};{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[mode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1241,
                            "end_lineno": 1435,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                },
                {
                    "descriptor": {
                        "old_range": {
                            "start": 1415,
                            "length": 7
                        },
                        "new_range": {
                            "start": 1416,
                            "length": 7
                        },
                        "section": "def _save(im, fp, filename, chunk=putchunk, save_all=False):"
                    },
                    "line_group": {
                        "all_lines": [
                            {
                                "content": "",
                                "type": "context"
                            },
                            {
                                "content": "    if save_all:",
                                "type": "context"
                            },
                            {
                                "content": "        im = _write_multiple_frames(",
                                "type": "context"
                            },
                            {
                                "content": "            im, fp, chunk, rawmode, default_image, append_images",
                                "type": "deleted"
                            },
                            {
                                "content": "            im, fp, chunk, mode, rawmode, default_image, append_images",
                                "type": "added"
                            },
                            {
                                "content": "        )",
                                "type": "context"
                            },
                            {
                                "content": "    if im:",
                                "type": "context"
                            },
                            {
                                "content": "        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])",
                                "type": "context"
                            },
                            {
                                "content": "",
                                "type": "context"
                            }
                        ]
                    },
                    "modified_entities": [
                        {
                            "file_name": "src/PIL/PngImagePlugin.py",
                            "type": "function",
                            "name": "_save",
                            "content": "def _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            mode = f\"{mode};{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[mode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()",
                            "ast_type_str": "FunctionDef",
                            "start_lineno": 1241,
                            "end_lineno": 1435,
                            "parent": null
                        }
                    ],
                    "added_entities": [],
                    "deleted_entities": []
                }
            ]
        }
    ],
    "old_commit_hash": "f34360d1e3c8f29f1a0b0ccae62b5f7c95700440^",
    "new_commit_hash": "f34360d1e3c8f29f1a0b0ccae62b5f7c95700440",
    "commit_message": "When saving multiple frames, convert to mode rather than raw mode",
    "commit_date": "2024-05-27T18:09:46+10:00",
    "metadata": {}
}
{
    "repo_name": "pillow",
    "new_commit_hash": "f34360d1e3c8f29f1a0b0ccae62b5f7c95700440",
    "test_file_codes": [
        "from __future__ import annotations\n\nimport re\nimport sys\nimport warnings\nimport zlib\nfrom io import BytesIO\nfrom pathlib import Path\nfrom types import ModuleType\nfrom typing import Any, cast\n\nimport pytest\n\nfrom PIL import Image, ImageFile, PngImagePlugin, features\n\nfrom .helper import (\n    PillowLeakTestCase,\n    assert_image,\n    assert_image_equal,\n    assert_image_equal_tofile,\n    hopper,\n    is_win32,\n    mark_if_feature_version,\n    skip_unless_feature,\n)\n\nElementTree: ModuleType | None\ntry:\n    from defusedxml import ElementTree\nexcept ImportError:\n    ElementTree = None\n\n# sample png stream\n\nTEST_PNG_FILE = \"Tests/images/hopper.png\"\n\n# stuff to create inline PNG images\n\nMAGIC = PngImagePlugin._MAGIC\n\n\ndef chunk(cid: bytes, *data: bytes) -> bytes:\n    test_file = BytesIO()\n    PngImagePlugin.putchunk(*(test_file, cid) + data)\n    return test_file.getvalue()\n\n\no32 = PngImagePlugin.o32\n\nIHDR = chunk(b\"IHDR\", o32(1), o32(1), b\"\\x08\\x02\", b\"\\0\\0\\0\")\nIDAT = chunk(b\"IDAT\")\nIEND = chunk(b\"IEND\")\n\nHEAD = MAGIC + IHDR\nTAIL = IDAT + IEND\n\n\ndef load(data: bytes) -> Image.Image:\n    return Image.open(BytesIO(data))\n\n\ndef roundtrip(im: Image.Image, **options: Any) -> PngImagePlugin.PngImageFile:\n    out = BytesIO()\n    im.save(out, \"PNG\", **options)\n    out.seek(0)\n    return cast(PngImagePlugin.PngImageFile, Image.open(out))\n\n\n@skip_unless_feature(\"zlib\")\nclass TestFilePng:\n    def get_chunks(self, filename: str) -> list[bytes]:\n        chunks = []\n        with open(filename, \"rb\") as fp:\n            fp.read(8)\n            with PngImagePlugin.PngStream(fp) as png:\n                while True:\n                    cid, pos, length = png.read()\n                    chunks.append(cid)\n                    try:\n                        s = png.call(cid, pos, length)\n                    except EOFError:\n                        break\n                    png.crc(cid, s)\n        return chunks\n\n    def test_sanity(self, tmp_path: Path) -> None:\n        # internal version number\n        assert re.search(\n            r\"\\d+(\\.\\d+){1,3}(\\.zlib\\-ng)?$\", features.version_codec(\"zlib\")\n        )\n\n        test_file = str(tmp_path / \"temp.png\")\n\n        hopper(\"RGB\").save(test_file)\n\n        with Image.open(test_file) as im:\n            im.load()\n            assert im.mode == \"RGB\"\n            assert im.size == (128, 128)\n            assert im.format == \"PNG\"\n            assert im.get_format_mimetype() == \"image/png\"\n\n        for mode in [\"1\", \"L\", \"P\", \"RGB\", \"I\", \"I;16\", \"I;16B\"]:\n            im = hopper(mode)\n            im.save(test_file)\n            with Image.open(test_file) as reloaded:\n                if mode in (\"I\", \"I;16B\"):\n                    reloaded = reloaded.convert(mode)\n                assert_image_equal(reloaded, im)\n\n    def test_invalid_file(self) -> None:\n        invalid_file = \"Tests/images/flower.jpg\"\n\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(invalid_file)\n\n    def test_broken(self) -> None:\n        # Check reading of totally broken files.  In this case, the test\n        # file was checked into Subversion as a text file.\n\n        test_file = \"Tests/images/broken.png\"\n        with pytest.raises(OSError):\n            with Image.open(test_file):\n                pass\n\n    def test_bad_text(self) -> None:\n        # Make sure PIL can read malformed tEXt chunks (@PIL152)\n\n        im = load(HEAD + chunk(b\"tEXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n        im = load(HEAD + chunk(b\"tEXt\", b\"spam\\0egg\\0\") + TAIL)\n        assert im.info == {\"spam\": \"egg\\x00\"}\n\n    def test_bad_ztxt(self) -> None:\n        # Test reading malformed zTXt chunks (python-pillow/Pillow#318)\n\n        im = load(HEAD + chunk(b\"zTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\") + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")[:1]) + TAIL)\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(HEAD + chunk(b\"zTXt\", b\"spam\\0\\0\" + zlib.compress(b\"egg\")) + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n\n    def test_bad_itxt(self) -> None:\n        im = load(HEAD + chunk(b\"iTXt\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\x02\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0foo\\0\") + TAIL)\n        assert im.info == {}\n\n        im = load(HEAD + chunk(b\"iTXt\", b\"spam\\0\\0\\0en\\0Spam\\0egg\") + TAIL)\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\")[:1])\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"\"}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\1en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {}\n\n        im = load(\n            HEAD\n            + chunk(b\"iTXt\", b\"spam\\0\\1\\0en\\0Spam\\0\" + zlib.compress(b\"egg\"))\n            + TAIL\n        )\n        assert im.info == {\"spam\": \"egg\"}\n        assert im.info[\"spam\"].lang == \"en\"\n        assert im.info[\"spam\"].tkey == \"Spam\"\n\n    def test_interlace(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n        test_file = \"Tests/images/pil123rgba.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"RGBA\", (162, 150))\n            assert im.info.get(\"interlace\")\n\n            im.load()\n\n    def test_load_transparent_p(self) -> None:\n        test_file = \"Tests/images/pil123p.png\"\n        with Image.open(test_file) as im:\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_load_transparent_rgb(self) -> None:\n        test_file = \"Tests/images/rgb_trns.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (0, 255, 52)\n\n            assert_image(im, \"RGB\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_palette(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/pil123p.png\"\n        with Image.open(in_file) as im:\n            # 'transparency' contains a byte string with the opacity for\n            # each palette entry\n            assert len(im.info[\"transparency\"]) == 256\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n\n            assert_image(im, \"P\", (162, 150))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (162, 150))\n\n        # image has 124 unique alpha values\n        assert len(im.getchannel(\"A\").getcolors()) == 124\n\n    def test_save_p_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/p_trns_single.png\"\n        with Image.open(in_file) as im:\n            # pixel value 164 is full transparent\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 164\n            assert im.getpixel((31, 31)) == 164\n            assert_image(im, \"P\", (64, 64))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (64, 64))\n\n        assert im.getpixel((31, 31)) == (0, 255, 52, 0)\n\n        # image has 876 transparent pixels\n        assert im.getchannel(\"A\").getcolors()[0][0] == 876\n\n    def test_save_p_transparent_black(self, tmp_path: Path) -> None:\n        # check if solid black image with full transparency\n        # is supported (check for #1838)\n        im = Image.new(\"RGBA\", (10, 10), (0, 0, 0, 0))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n        im = im.convert(\"P\")\n        test_file = str(tmp_path / \"temp.png\")\n        im.save(test_file)\n\n        # check if saved image contains same transparency\n        with Image.open(test_file) as im:\n            assert len(im.info[\"transparency\"]) == 256\n            assert_image(im, \"P\", (10, 10))\n            im = im.convert(\"RGBA\")\n        assert_image(im, \"RGBA\", (10, 10))\n        assert im.getcolors() == [(100, (0, 0, 0, 0))]\n\n    def test_save_grayscale_transparency(self, tmp_path: Path) -> None:\n        for mode, num_transparent in {\"1\": 1994, \"L\": 559, \"I;16\": 559}.items():\n            in_file = \"Tests/images/\" + mode.split(\";\")[0].lower() + \"_trns.png\"\n            with Image.open(in_file) as im:\n                assert im.mode == mode\n                assert im.info[\"transparency\"] == 255\n\n                im_rgba = im.convert(\"RGBA\")\n            assert im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n            with Image.open(test_file) as test_im:\n                assert test_im.mode == mode\n                assert test_im.info[\"transparency\"] == 255\n                assert_image_equal(im, test_im)\n\n            test_im_rgba = test_im.convert(\"RGBA\")\n            assert test_im_rgba.getchannel(\"A\").getcolors()[0][0] == num_transparent\n\n    def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:\n        in_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(in_file) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file)\n\n    def test_load_verify(self) -> None:\n        # Check open/load/verify exception (@PIL150)\n\n        with Image.open(TEST_PNG_FILE) as im:\n            # Assert that there is no unclosed file warning\n            with warnings.catch_warnings():\n                im.verify()\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.load()\n            with pytest.raises(RuntimeError):\n                im.verify()\n\n    def test_verify_struct_error(self) -> None:\n        # Check open/load/verify exception (#1755)\n\n        # offsets to test, -10: breaks in i32() in read. (OSError)\n        #                  -13: breaks in crc, txt chunk.\n        #                  -14: malformed chunk\n\n        for offset in (-10, -13, -14):\n            with open(TEST_PNG_FILE, \"rb\") as f:\n                test_file = f.read()[:offset]\n\n            with Image.open(BytesIO(test_file)) as im:\n                assert im.fp is not None\n                with pytest.raises((OSError, SyntaxError)):\n                    im.verify()\n\n    def test_verify_ignores_crc_error(self) -> None:\n        # check ignores crc errors in ancillary chunks\n\n        chunk_data = chunk(b\"tEXt\", b\"spam\")\n        broken_crc_chunk_data = chunk_data[:-1] + b\"q\"  # break CRC\n\n        image_data = HEAD + broken_crc_chunk_data + TAIL\n        with pytest.raises(SyntaxError):\n            PngImagePlugin.PngImageFile(BytesIO(image_data))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            im = load(image_data)\n            assert im is not None\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_verify_not_ignores_crc_error_in_required_chunk(self) -> None:\n        # check does not ignore crc errors in required chunks\n\n        image_data = MAGIC + IHDR[:-1] + b\"q\" + TAIL\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with pytest.raises(SyntaxError):\n                PngImagePlugin.PngImageFile(BytesIO(image_data))\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    def test_roundtrip_dpi(self) -> None:\n        # Check dpi roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im = roundtrip(im, dpi=(100.33, 100.33))\n        assert im.info[\"dpi\"] == (100.33, 100.33)\n\n    def test_load_float_dpi(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.info[\"dpi\"] == (95.9866, 95.9866)\n\n    def test_roundtrip_text(self) -> None:\n        # Check text roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"TXT\", \"VALUE\")\n            info.add_text(\"ZIP\", \"VALUE\", zip=True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n        assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_roundtrip_itxt(self) -> None:\n        # Check iTXt roundtripping\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_itxt(\"spam\", \"Eggs\", \"en\", \"Spam\")\n        info.add_text(\"eggs\", PngImagePlugin.iTXt(\"Spam\", \"en\", \"Eggs\"), zip=True)\n\n        im = roundtrip(im, pnginfo=info)\n        assert im.info == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text == {\"spam\": \"Eggs\", \"eggs\": \"Spam\"}\n        assert im.text[\"spam\"].lang == \"en\"\n        assert im.text[\"spam\"].tkey == \"Spam\"\n        assert im.text[\"eggs\"].lang == \"en\"\n        assert im.text[\"eggs\"].tkey == \"Eggs\"\n\n    def test_nonunicode_text(self) -> None:\n        # Check so that non-Unicode text is saved as a tEXt rather than iTXt\n\n        im = Image.new(\"RGB\", (32, 32))\n        info = PngImagePlugin.PngInfo()\n        info.add_text(\"Text\", \"Ascii\")\n        im = roundtrip(im, pnginfo=info)\n        assert isinstance(im.info[\"Text\"], str)\n\n    def test_unicode_text(self) -> None:\n        # Check preservation of non-ASCII characters\n\n        def rt_text(value: str) -> None:\n            im = Image.new(\"RGB\", (32, 32))\n            info = PngImagePlugin.PngInfo()\n            info.add_text(\"Text\", value)\n            im = roundtrip(im, pnginfo=info)\n            assert im.info == {\"Text\": value}\n\n        rt_text(\" Aa\" + chr(0xA0) + chr(0xC4) + chr(0xFF))  # Latin1\n        rt_text(chr(0x400) + chr(0x472) + chr(0x4FF))  # Cyrillic\n        # CJK:\n        rt_text(chr(0x4E00) + chr(0x66F0) + chr(0x9FBA) + chr(0x3042) + chr(0xAC00))\n        rt_text(\"A\" + chr(0xC4) + chr(0x472) + chr(0x3042))  # Combined\n\n    def test_scary(self) -> None:\n        # Check reading of evil PNG file.  For information, see:\n        # http://scary.beasts.org/security/CESA-2004-001.txt\n        # The first byte is removed from pngtest_bad.png\n        # to avoid classification as malware.\n\n        with open(\"Tests/images/pngtest_bad.png.bin\", \"rb\") as fd:\n            data = b\"\\x89\" + fd.read()\n\n        pngfile = BytesIO(data)\n        with pytest.raises(OSError):\n            with Image.open(pngfile):\n                pass\n\n    def test_trns_rgb(self) -> None:\n        # Check writing and reading of tRNS chunks for RGB images.\n        # Independent file sample provided by Sebastian Spaeth.\n\n        test_file = \"Tests/images/caption_6_33_22.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == (248, 248, 248)\n\n            # check saving transparency by default\n            im = roundtrip(im)\n        assert im.info[\"transparency\"] == (248, 248, 248)\n\n        im = roundtrip(im, transparency=(0, 1, 2))\n        assert im.info[\"transparency\"] == (0, 1, 2)\n\n    def test_trns_p(self, tmp_path: Path) -> None:\n        # Check writing a transparency of 0, issue #528\n        im = hopper(\"P\")\n        im.info[\"transparency\"] = 0\n\n        f = str(tmp_path / \"temp.png\")\n        im.save(f)\n\n        with Image.open(f) as im2:\n            assert \"transparency\" in im2.info\n\n            assert_image_equal(im2.convert(\"RGBA\"), im.convert(\"RGBA\"))\n\n    def test_trns_null(self) -> None:\n        # Check reading images with null tRNS value, issue #1239\n        test_file = \"Tests/images/tRNS_null_1x1.png\"\n        with Image.open(test_file) as im:\n            assert im.info[\"transparency\"] == 0\n\n    def test_save_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            with Image.open(\"Tests/images/icc_profile.png\") as with_icc:\n                expected_icc = with_icc.info[\"icc_profile\"]\n\n                im = roundtrip(im, icc_profile=expected_icc)\n                assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_discard_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            assert \"icc_profile\" in im.info\n\n            im = roundtrip(im, icc_profile=None)\n        assert \"icc_profile\" not in im.info\n\n    def test_roundtrip_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            expected_icc = im.info[\"icc_profile\"]\n\n            im = roundtrip(im)\n        assert im.info[\"icc_profile\"] == expected_icc\n\n    def test_roundtrip_no_icc_profile(self) -> None:\n        with Image.open(\"Tests/images/icc_profile_none.png\") as im:\n            assert im.info[\"icc_profile\"] is None\n\n            im = roundtrip(im)\n        assert \"icc_profile\" not in im.info\n\n    def test_repr_png(self) -> None:\n        im = hopper()\n\n        with Image.open(BytesIO(im._repr_png_())) as repr_png:\n            assert repr_png.format == \"PNG\"\n            assert_image_equal(im, repr_png)\n\n    def test_repr_png_error_returns_none(self) -> None:\n        im = hopper(\"F\")\n\n        assert im._repr_png_() is None\n\n    def test_chunk_order(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/icc_profile.png\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.convert(\"P\").save(test_file, dpi=(100, 100))\n\n        chunks = self.get_chunks(test_file)\n\n        # https://www.w3.org/TR/PNG/#5ChunkOrdering\n        # IHDR - shall be first\n        assert chunks.index(b\"IHDR\") == 0\n        # PLTE - before first IDAT\n        assert chunks.index(b\"PLTE\") < chunks.index(b\"IDAT\")\n        # iCCP - before PLTE and IDAT\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"PLTE\")\n        assert chunks.index(b\"iCCP\") < chunks.index(b\"IDAT\")\n        # tRNS - after PLTE, before IDAT\n        assert chunks.index(b\"tRNS\") > chunks.index(b\"PLTE\")\n        assert chunks.index(b\"tRNS\") < chunks.index(b\"IDAT\")\n        # pHYs - before IDAT\n        assert chunks.index(b\"pHYs\") < chunks.index(b\"IDAT\")\n\n    def test_getchunks(self) -> None:\n        im = hopper()\n\n        chunks = PngImagePlugin.getchunks(im)\n        assert len(chunks) == 3\n\n    def test_read_private_chunks(self) -> None:\n        with Image.open(\"Tests/images/exif.png\") as im:\n            assert im.private_chunks == [(b\"orNT\", b\"\\x01\")]\n\n    def test_roundtrip_private_chunk(self) -> None:\n        # Check private chunk roundtripping\n\n        with Image.open(TEST_PNG_FILE) as im:\n            info = PngImagePlugin.PngInfo()\n            info.add(b\"prIV\", b\"VALUE\")\n            info.add(b\"atEC\", b\"VALUE2\")\n            info.add(b\"prIV\", b\"VALUE3\", True)\n\n            im = roundtrip(im, pnginfo=info)\n        assert im.private_chunks == [(b\"prIV\", b\"VALUE\"), (b\"atEC\", b\"VALUE2\")]\n        im.load()\n        assert im.private_chunks == [\n            (b\"prIV\", b\"VALUE\"),\n            (b\"atEC\", b\"VALUE2\"),\n            (b\"prIV\", b\"VALUE3\", True),\n        ]\n\n    def test_textual_chunks_after_idat(self) -> None:\n        with Image.open(\"Tests/images/hopper.png\") as im:\n            assert \"comment\" in im.text\n            for k, v in {\n                \"date:create\": \"2014-09-04T09:37:08+03:00\",\n                \"date:modify\": \"2014-09-04T09:37:08+03:00\",\n            }.items():\n                assert im.text[k] == v\n\n        # Raises a SyntaxError in load_end\n        with Image.open(\"Tests/images/broken_data_stream.png\") as im:\n            with pytest.raises(OSError):\n                assert isinstance(im.text, dict)\n\n        # Raises a UnicodeDecodeError in load_end\n        with Image.open(\"Tests/images/truncated_image.png\") as im:\n            # The file is truncated\n            with pytest.raises(OSError):\n                im.text()\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            assert isinstance(im.text, dict)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n        # Raises an EOFError in load_end\n        with Image.open(\"Tests/images/hopper_idat_after_image_end.png\") as im:\n            assert im.text == {\"TXT\": \"VALUE\", \"ZIP\": \"VALUE\"}\n\n    def test_unknown_compression_method(self) -> None:\n        with pytest.raises(SyntaxError, match=\"Unknown compression method\"):\n            PngImagePlugin.PngImageFile(\"Tests/images/unknown_compression_method.png\")\n\n    def test_padded_idat(self) -> None:\n        # This image has been manually hexedited\n        # so that the IDAT chunk has padding at the end\n        # Set MAXBLOCK to the length of the actual data\n        # so that the decoder finishes reading before the chunk ends\n        MAXBLOCK = ImageFile.MAXBLOCK\n        ImageFile.MAXBLOCK = 45\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n\n        with Image.open(\"Tests/images/padded_idat.png\") as im:\n            im.load()\n\n            ImageFile.MAXBLOCK = MAXBLOCK\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n            assert_image_equal_tofile(im, \"Tests/images/bw_gradient.png\")\n\n    @pytest.mark.parametrize(\n        \"cid\", (b\"IHDR\", b\"sRGB\", b\"pHYs\", b\"acTL\", b\"fcTL\", b\"fdAT\")\n    )\n    def test_truncated_chunks(self, cid: bytes) -> None:\n        fp = BytesIO()\n        with PngImagePlugin.PngStream(fp) as png:\n            with pytest.raises(ValueError):\n                png.call(cid, 0, 0)\n\n            ImageFile.LOAD_TRUNCATED_IMAGES = True\n            png.call(cid, 0, 0)\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n    @pytest.mark.parametrize(\"save_all\", (True, False))\n    def test_specify_bits(self, save_all: bool, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(out, bits=4, save_all=save_all)\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 48\n\n    def test_plte_length(self, tmp_path: Path) -> None:\n        im = Image.new(\"P\", (1, 1))\n        im.putpalette((1, 1, 1))\n\n        out = str(tmp_path / \"temp.png\")\n        im.save(str(tmp_path / \"temp.png\"))\n\n        with Image.open(out) as reloaded:\n            assert len(reloaded.png.im_palette[1]) == 3\n\n    def test_getxmp(self) -> None:\n        with Image.open(\"Tests/images/color_snakes.png\") as im:\n            if ElementTree is None:\n                with pytest.warns(\n                    UserWarning,\n                    match=\"XMP data cannot be read without defusedxml dependency\",\n                ):\n                    assert im.getxmp() == {}\n            else:\n                xmp = im.getxmp()\n\n                description = xmp[\"xmpmeta\"][\"RDF\"][\"Description\"]\n                assert description[\"PixelXDimension\"] == \"10\"\n                assert description[\"subject\"][\"Seq\"] is None\n\n    def test_exif(self) -> None:\n        # With an EXIF chunk\n        with Image.open(\"Tests/images/exif.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With an ImageMagick zTXt chunk\n        with Image.open(\"Tests/images/exif_imagemagick.png\") as im:\n            exif = im._getexif()\n            assert exif[274] == 1\n\n            # Assert that info still can be extracted\n            # when the image is no longer a PngImageFile instance\n            exif = im.copy().getexif()\n            assert exif[274] == 1\n\n        # With a tEXt chunk\n        with Image.open(\"Tests/images/exif_text.png\") as im:\n            exif = im._getexif()\n        assert exif[274] == 1\n\n        # With XMP tags\n        with Image.open(\"Tests/images/xmp_tags_orientation.png\") as im:\n            exif = im.getexif()\n        assert exif[274] == 3\n\n    def test_exif_save(self, tmp_path: Path) -> None:\n        # Test exif is not saved from info\n        test_file = str(tmp_path / \"temp.png\")\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file)\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded._getexif() is None\n\n        # Test passing in exif\n        with Image.open(\"Tests/images/exif.png\") as im:\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[274] == 1\n\n    @mark_if_feature_version(\n        pytest.mark.valgrind_known_error, \"libjpeg_turbo\", \"2.0\", reason=\"Known Failing\"\n    )\n    def test_exif_from_jpg(self, tmp_path: Path) -> None:\n        with Image.open(\"Tests/images/pil_sample_rgb.jpg\") as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=im.getexif())\n\n        with Image.open(test_file) as reloaded:\n            exif = reloaded._getexif()\n        assert exif[305] == \"Adobe Photoshop CS Macintosh\"\n\n    def test_exif_argument(self, tmp_path: Path) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            test_file = str(tmp_path / \"temp.png\")\n            im.save(test_file, exif=b\"exifstring\")\n\n        with Image.open(test_file) as reloaded:\n            assert reloaded.info[\"exif\"] == b\"Exif\\x00\\x00exifstring\"\n\n    def test_tell(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            assert im.tell() == 0\n\n    def test_seek(self) -> None:\n        with Image.open(TEST_PNG_FILE) as im:\n            im.seek(0)\n\n            with pytest.raises(EOFError):\n                im.seek(1)\n\n    @pytest.mark.parametrize(\"buffer\", (True, False))\n    def test_save_stdout(self, buffer: bool) -> None:\n        old_stdout = sys.stdout\n\n        if buffer:\n\n            class MyStdOut:\n                buffer = BytesIO()\n\n            mystdout = MyStdOut()\n        else:\n            mystdout = BytesIO()\n\n        sys.stdout = mystdout\n\n        with Image.open(TEST_PNG_FILE) as im:\n            im.save(sys.stdout, \"PNG\")\n\n        # Reset stdout\n        sys.stdout = old_stdout\n\n        if buffer:\n            mystdout = mystdout.buffer\n        with Image.open(mystdout) as reloaded:\n            assert_image_equal_tofile(reloaded, TEST_PNG_FILE)\n\n    def test_truncated_end_chunk(self) -> None:\n        with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n            with pytest.raises(OSError):\n                im.load()\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        try:\n            with Image.open(\"Tests/images/truncated_end_chunk.png\") as im:\n                assert_image_equal_tofile(im, \"Tests/images/hopper.png\")\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n\n\n@pytest.mark.skipif(is_win32(), reason=\"Requires Unix or macOS\")\n@skip_unless_feature(\"zlib\")\nclass TestTruncatedPngPLeaks(PillowLeakTestCase):\n    mem_limit = 2 * 1024  # max increase in K\n    iterations = 100  # Leak is 56k/iteration, this will leak 5.6megs\n\n    def test_leak_load(self) -> None:\n        with open(\"Tests/images/hopper.png\", \"rb\") as f:\n            DATA = BytesIO(f.read(16 * 1024))\n\n        ImageFile.LOAD_TRUNCATED_IMAGES = True\n        with Image.open(DATA) as im:\n            im.load()\n\n        def core() -> None:\n            with Image.open(DATA) as im:\n                im.load()\n\n        try:\n            self._test_leak(core)\n        finally:\n            ImageFile.LOAD_TRUNCATED_IMAGES = False\n"
    ],
    "test_file_names": [
        "test_1.py"
    ],
    "setup_res_code": 0,
    "setup_res_stdout": "Starting Pillow installation attempts...\nVerifying Pillow installation...\n✅ Pillow installation successful!\nSuccessfully installed Pillow\n",
    "setup_res_stderr": "warning: `VIRTUAL_ENV=/home/gcpuser/r2e-edits-internal/.venv` does not match the project environment path `.venv` and will be ignored\nUsing CPython 3.9.21\nCreating virtual environment at: .venv\nActivate with: source .venv/bin/activate\nResolved 12 packages in 22ms\nInstalled 12 packages in 150ms\n + coverage==7.6.10\n + exceptiongroup==1.2.2\n + iniconfig==2.0.0\n + packaging==24.2\n + pluggy==1.5.0\n + pyqt5==5.15.11\n + pyqt5-qt5==5.15.16\n + pyqt5-sip==12.16.1\n + pytest==8.3.4\n + pytest-cov==6.0.0\n + setuptools==75.8.0\n + tomli==2.2.1\nResolved 1 package in 1.14s\nPrepared 1 package in 34.57s\nInstalled 1 package in 1ms\n + pillow==10.4.0.dev0 (from file:///home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_f34360d1e3c8f29f1a0b0ccae62b5f7c95700440)\n",
    "new_commit_res_code": 0,
    "new_commit_res_stdout": "\u001b[1m============================= test session starts ==============================\u001b[0m\nplatform linux -- Python 3.9.21, pytest-8.3.4, pluggy-1.5.0\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_f34360d1e3c8f29f1a0b0ccae62b5f7c95700440\nconfigfile: pyproject.toml\nplugins: cov-6.0.0\ncollected 61 items\n\nr2e_tests/test_1.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m [ 85%]\n\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m                                                                [100%]\u001b[0m\n\n==================================== PASSES ====================================\n\u001b[36m\u001b[1m=========================== short test summary info ============================\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_sanity\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_invalid_file\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_broken\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_ztxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_itxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_interlace\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_transparent_p\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_transparent_rgb\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_transparent_palette\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_single_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_transparent_black\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_grayscale_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_rgb_single_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_verify\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_struct_error\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_ignores_crc_error\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_not_ignores_crc_error_in_required_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_dpi\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_float_dpi\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_itxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_nonunicode_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_unicode_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_scary\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_rgb\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_p\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_null\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_discard_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_no_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_repr_png\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_repr_png_error_returns_none\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_chunk_order\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_getchunks\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_read_private_chunks\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_private_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_textual_chunks_after_idat\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_unknown_compression_method\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_padded_idat\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[IHDR]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[sRGB]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[pHYs]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[acTL]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[fcTL]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[fdAT]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_specify_bits[True]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_specify_bits[False]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_plte_length\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_getxmp\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_save\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_from_jpg\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_argument\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_tell\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_seek\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_stdout[True]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_stdout[False]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_end_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestTruncatedPngPLeaks::test_leak_load\u001b[0m\n\u001b[32m============================== \u001b[32m\u001b[1m61 passed\u001b[0m\u001b[32m in 0.57s\u001b[0m\u001b[32m ==============================\u001b[0m\n",
    "new_commit_res_stderr": "",
    "old_commit_res_code": 1,
    "old_commit_res_stdout": "\u001b[1m============================= test session starts ==============================\u001b[0m\nplatform linux -- Python 3.9.21, pytest-8.3.4, pluggy-1.5.0\nrootdir: /home/gcpuser/buckets/local_repoeval_bucket/repos/pillow_f34360d1e3c8f29f1a0b0ccae62b5f7c95700440\nconfigfile: pyproject.toml\nplugins: cov-6.0.0\ncollected 61 items\n\nr2e_tests/test_1.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[31mF\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[31m [ 85%]\n\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[31m                                                                [100%]\u001b[0m\n\n=================================== FAILURES ===================================\n\u001b[31m\u001b[1m_____________________ TestFilePng.test_specify_bits[True] ______________________\u001b[0m\n\nself = <r2e_tests.test_1.TestFilePng object at 0x794c941013a0>, save_all = True\ntmp_path = PosixPath('/tmp/pytest-of-gcpuser/pytest-726/test_specify_bits_True_0')\n\n    @pytest.mark.parametrize(\"save_all\", (True, False))\n    def test_specify_bits(self, save_all: bool, tmp_path: Path) -> None:\n        im = hopper(\"P\")\n    \n        out = str(tmp_path / \"temp.png\")\n>       im.save(out, bits=4, save_all=save_all)\n\n\u001b[1m\u001b[31mr2e_tests/test_1.py\u001b[0m:663: \n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n\u001b[1m\u001b[31msrc/PIL/Image.py\u001b[0m:2494: in save\n    save_handler(self, fp, filename)\n\u001b[1m\u001b[31msrc/PIL/PngImagePlugin.py\u001b[0m:1238: in _save_all\n    _save(im, fp, filename, save_all=True)\n\u001b[1m\u001b[31msrc/PIL/PngImagePlugin.py\u001b[0m:1417: in _save\n    im = _write_multiple_frames(\n\u001b[1m\u001b[31msrc/PIL/PngImagePlugin.py\u001b[0m:1125: in _write_multiple_frames\n    im_frame = im_frame.convert(rawmode)\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ \n\nself = <PIL.Image.Image image mode=P size=128x128 at 0x794C9413AEE0>\nmode = 'P;4', matrix = None, dither = <Dither.FLOYDSTEINBERG: 3>\npalette = <Palette.WEB: 0>, colors = 256\n\n    def convert(\n        self,\n        mode: str | None = None,\n        matrix: tuple[float, ...] | None = None,\n        dither: Dither | None = None,\n        palette: Palette = Palette.WEB,\n        colors: int = 256,\n    ) -> Image:\n        \"\"\"\n        Returns a converted copy of this image. For the \"P\" mode, this\n        method translates pixels through the palette.  If mode is\n        omitted, a mode is chosen so that all information in the image\n        and the palette can be represented without a palette.\n    \n        This supports all possible conversions between \"L\", \"RGB\" and \"CMYK\". The\n        ``matrix`` argument only supports \"L\" and \"RGB\".\n    \n        When translating a color image to grayscale (mode \"L\"),\n        the library uses the ITU-R 601-2 luma transform::\n    \n            L = R * 299/1000 + G * 587/1000 + B * 114/1000\n    \n        The default method of converting a grayscale (\"L\") or \"RGB\"\n        image into a bilevel (mode \"1\") image uses Floyd-Steinberg\n        dither to approximate the original image luminosity levels. If\n        dither is ``None``, all values larger than 127 are set to 255 (white),\n        all other values to 0 (black). To use other thresholds, use the\n        :py:meth:`~PIL.Image.Image.point` method.\n    \n        When converting from \"RGBA\" to \"P\" without a ``matrix`` argument,\n        this passes the operation to :py:meth:`~PIL.Image.Image.quantize`,\n        and ``dither`` and ``palette`` are ignored.\n    \n        When converting from \"PA\", if an \"RGBA\" palette is present, the alpha\n        channel from the image will be used instead of the values from the palette.\n    \n        :param mode: The requested mode. See: :ref:`concept-modes`.\n        :param matrix: An optional conversion matrix.  If given, this\n           should be 4- or 12-tuple containing floating point values.\n        :param dither: Dithering method, used when converting from\n           mode \"RGB\" to \"P\" or from \"RGB\" or \"L\" to \"1\".\n           Available methods are :data:`Dither.NONE` or :data:`Dither.FLOYDSTEINBERG`\n           (default). Note that this is not used when ``matrix`` is supplied.\n        :param palette: Palette to use when converting from mode \"RGB\"\n           to \"P\".  Available palettes are :data:`Palette.WEB` or\n           :data:`Palette.ADAPTIVE`.\n        :param colors: Number of colors to use for the :data:`Palette.ADAPTIVE`\n           palette. Defaults to 256.\n        :rtype: :py:class:`~PIL.Image.Image`\n        :returns: An :py:class:`~PIL.Image.Image` object.\n        \"\"\"\n    \n        if mode in (\"BGR;15\", \"BGR;16\", \"BGR;24\"):\n            deprecate(mode, 12)\n    \n        self.load()\n    \n        has_transparency = \"transparency\" in self.info\n        if not mode and self.mode == \"P\":\n            # determine default mode\n            if self.palette:\n                mode = self.palette.mode\n            else:\n                mode = \"RGB\"\n            if mode == \"RGB\" and has_transparency:\n                mode = \"RGBA\"\n        if not mode or (mode == self.mode and not matrix):\n            return self.copy()\n    \n        if matrix:\n            # matrix conversion\n            if mode not in (\"L\", \"RGB\"):\n                msg = \"illegal conversion\"\n                raise ValueError(msg)\n            im = self.im.convert_matrix(mode, matrix)\n            new_im = self._new(im)\n            if has_transparency and self.im.bands == 3:\n                transparency = new_im.info[\"transparency\"]\n    \n                def convert_transparency(m, v):\n                    v = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3] * 0.5\n                    return max(0, min(255, int(v)))\n    \n                if mode == \"L\":\n                    transparency = convert_transparency(matrix, transparency)\n                elif len(mode) == 3:\n                    transparency = tuple(\n                        convert_transparency(matrix[i * 4 : i * 4 + 4], transparency)\n                        for i in range(0, len(transparency))\n                    )\n                new_im.info[\"transparency\"] = transparency\n            return new_im\n    \n        if mode == \"P\" and self.mode == \"RGBA\":\n            return self.quantize(colors)\n    \n        trns = None\n        delete_trns = False\n        # transparency handling\n        if has_transparency:\n            if (self.mode in (\"1\", \"L\", \"I\", \"I;16\") and mode in (\"LA\", \"RGBA\")) or (\n                self.mode == \"RGB\" and mode in (\"La\", \"LA\", \"RGBa\", \"RGBA\")\n            ):\n                # Use transparent conversion to promote from transparent\n                # color to an alpha channel.\n                new_im = self._new(\n                    self.im.convert_transparent(mode, self.info[\"transparency\"])\n                )\n                del new_im.info[\"transparency\"]\n                return new_im\n            elif self.mode in (\"L\", \"RGB\", \"P\") and mode in (\"L\", \"RGB\", \"P\"):\n                t = self.info[\"transparency\"]\n                if isinstance(t, bytes):\n                    # Dragons. This can't be represented by a single color\n                    warnings.warn(\n                        \"Palette images with Transparency expressed in bytes should be \"\n                        \"converted to RGBA images\"\n                    )\n                    delete_trns = True\n                else:\n                    # get the new transparency color.\n                    # use existing conversions\n                    trns_im = new(self.mode, (1, 1))\n                    if self.mode == \"P\":\n                        trns_im.putpalette(self.palette)\n                        if isinstance(t, tuple):\n                            err = \"Couldn't allocate a palette color for transparency\"\n                            try:\n                                t = trns_im.palette.getcolor(t, self)\n                            except ValueError as e:\n                                if str(e) == \"cannot allocate more than 256 colors\":\n                                    # If all 256 colors are in use,\n                                    # then there is no need for transparency\n                                    t = None\n                                else:\n                                    raise ValueError(err) from e\n                    if t is None:\n                        trns = None\n                    else:\n                        trns_im.putpixel((0, 0), t)\n    \n                        if mode in (\"L\", \"RGB\"):\n                            trns_im = trns_im.convert(mode)\n                        else:\n                            # can't just retrieve the palette number, got to do it\n                            # after quantization.\n                            trns_im = trns_im.convert(\"RGB\")\n                        trns = trns_im.getpixel((0, 0))\n    \n            elif self.mode == \"P\" and mode in (\"LA\", \"PA\", \"RGBA\"):\n                t = self.info[\"transparency\"]\n                delete_trns = True\n    \n                if isinstance(t, bytes):\n                    self.im.putpalettealphas(t)\n                elif isinstance(t, int):\n                    self.im.putpalettealpha(t, 0)\n                else:\n                    msg = \"Transparency for P mode should be bytes or int\"\n                    raise ValueError(msg)\n    \n        if mode == \"P\" and palette == Palette.ADAPTIVE:\n            im = self.im.quantize(colors)\n            new_im = self._new(im)\n            from . import ImagePalette\n    \n            new_im.palette = ImagePalette.ImagePalette(\n                \"RGB\", new_im.im.getpalette(\"RGB\")\n            )\n            if delete_trns:\n                # This could possibly happen if we requantize to fewer colors.\n                # The transparency would be totally off in that case.\n                del new_im.info[\"transparency\"]\n            if trns is not None:\n                try:\n                    new_im.info[\"transparency\"] = new_im.palette.getcolor(trns, new_im)\n                except Exception:\n                    # if we can't make a transparent color, don't leave the old\n                    # transparency hanging around to mess us up.\n                    del new_im.info[\"transparency\"]\n                    warnings.warn(\"Couldn't allocate palette entry for transparency\")\n            return new_im\n    \n        if \"LAB\" in (self.mode, mode):\n            other_mode = mode if self.mode == \"LAB\" else self.mode\n            if other_mode in (\"RGB\", \"RGBA\", \"RGBX\"):\n                from . import ImageCms\n    \n                srgb = ImageCms.createProfile(\"sRGB\")\n                lab = ImageCms.createProfile(\"LAB\")\n                profiles = [lab, srgb] if self.mode == \"LAB\" else [srgb, lab]\n                transform = ImageCms.buildTransform(\n                    profiles[0], profiles[1], self.mode, mode\n                )\n                return transform.apply(self)\n    \n        # colorspace conversion\n        if dither is None:\n            dither = Dither.FLOYDSTEINBERG\n    \n        try:\n>           im = self.im.convert(mode, dither)\n\u001b[1m\u001b[31mE           ValueError: conversion not supported\u001b[0m\n\n\u001b[1m\u001b[31msrc/PIL/Image.py\u001b[0m:1113: ValueError\n==================================== PASSES ====================================\n\u001b[36m\u001b[1m=========================== short test summary info ============================\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_sanity\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_invalid_file\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_broken\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_ztxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_bad_itxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_interlace\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_transparent_p\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_transparent_rgb\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_transparent_palette\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_single_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_p_transparent_black\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_grayscale_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_rgb_single_transparency\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_verify\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_struct_error\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_ignores_crc_error\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_verify_not_ignores_crc_error_in_required_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_dpi\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_load_float_dpi\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_itxt\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_nonunicode_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_unicode_text\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_scary\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_rgb\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_p\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_trns_null\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_discard_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_no_icc_profile\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_repr_png\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_repr_png_error_returns_none\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_chunk_order\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_getchunks\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_read_private_chunks\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_roundtrip_private_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_textual_chunks_after_idat\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_unknown_compression_method\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_padded_idat\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[IHDR]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[sRGB]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[pHYs]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[acTL]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[fcTL]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_chunks[fdAT]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_specify_bits[False]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_plte_length\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_getxmp\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_save\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_from_jpg\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_exif_argument\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_tell\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_seek\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_stdout[True]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_save_stdout[False]\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_truncated_end_chunk\u001b[0m\n\u001b[32mPASSED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestTruncatedPngPLeaks::test_leak_load\u001b[0m\n\u001b[31mFAILED\u001b[0m r2e_tests/test_1.py::\u001b[1mTestFilePng::test_specify_bits[True]\u001b[0m - ValueError: conversion not supported\n\u001b[31m========================= \u001b[31m\u001b[1m1 failed\u001b[0m, \u001b[32m60 passed\u001b[0m\u001b[31m in 0.56s\u001b[0m\u001b[31m =========================\u001b[0m\n",
    "old_commit_res_stderr": ""
}
Tests/test_file_png.py src/PIL/PngImagePlugin.py
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
### Error When Saving Multiple PNG Frames with Specified Bit Depth

**Description**

When attempting to save multiple frames in PNG format with a specified bit depth, a `ValueError` is raised, preventing the save operation from completing successfully.

**Example Code**

```python
from PIL import Image, ImageSequence

# Create a palette-based image
im = Image.new("P", (128, 128))
out = "temp.png"

# Attempt to save multiple frames with bits=4
im.save(out, bits=4, save_all=True, append_images=[im.copy()])
```

**Expected Behavior**

The image should be saved successfully as a PNG file with the specified bit depth of 4 bits per channel, including all appended frames.

**Actual Behavior**

A `ValueError` is raised with the message "conversion not supported", indicating that the image mode conversion failed during the save operation.

**Error Message**

```
ValueError: conversion not supported
```

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### src/PIL/PngImagePlugin.py
#
# The Python Imaging Library.
# $Id$
#
# PNG support code
#
# See "PNG (Portable Network Graphics) Specification, version 1.0;
# W3C Recommendation", 1996-10-01, Thomas Boutell (ed.).
#
# history:
# 1996-05-06 fl   Created (couldn't resist it)
# 1996-12-14 fl   Upgraded, added read and verify support (0.2)
# 1996-12-15 fl   Separate PNG stream parser
# 1996-12-29 fl   Added write support, added getchunks
# 1996-12-30 fl   Eliminated circular references in decoder (0.3)
# 1998-07-12 fl   Read/write 16-bit images as mode I (0.4)
# 2001-02-08 fl   Added transparency support (from Zircon) (0.5)
# 2001-04-16 fl   Don't close data source in "open" method (0.6)
# 2004-02-24 fl   Don't even pretend to support interlaced files (0.7)
# 2004-08-31 fl   Do basic sanity check on chunk identifiers (0.8)
# 2004-09-20 fl   Added PngInfo chunk container
# 2004-12-18 fl   Added DPI read support (based on code by Niki Spahiev)
# 2008-08-13 fl   Added tRNS support for RGB images
# 2009-03-06 fl   Support for preserving ICC profiles (by Florian Hoech)
# 2009-03-08 fl   Added zTXT support (from Lowell Alleman)
# 2009-03-29 fl   Read interlaced PNG files (from Conrado Porto Lopes Gouvua)
#
# Copyright (c) 1997-2009 by Secret Labs AB
# Copyright (c) 1996 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations

import itertools
import logging
import re
import struct
import warnings
import zlib
from enum import IntEnum
from typing import IO

from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
from ._binary import i16be as i16
from ._binary import i32be as i32
from ._binary import o8
from ._binary import o16be as o16
from ._binary import o32be as o32

logger = logging.getLogger(__name__)

is_cid = re.compile(rb"\w\w\w\w").match


_MAGIC = b"\211PNG\r\n\032\n"


_MODES = {
    # supported bits/color combinations, and corresponding modes/rawmodes
    # Grayscale
    (1, 0): ("1", "1"),
    (2, 0): ("L", "L;2"),
    (4, 0): ("L", "L;4"),
    (8, 0): ("L", "L"),
    (16, 0): ("I;16", "I;16B"),
    # Truecolour
    (8, 2): ("RGB", "RGB"),
    (16, 2): ("RGB", "RGB;16B"),
    # Indexed-colour
    (1, 3): ("P", "P;1"),
    (2, 3): ("P", "P;2"),
    (4, 3): ("P", "P;4"),
    (8, 3): ("P", "P"),
    # Grayscale with alpha
    (8, 4): ("LA", "LA"),
    (16, 4): ("RGBA", "LA;16B"),  # LA;16B->LA not yet available
    # Truecolour with alpha
    (8, 6): ("RGBA", "RGBA"),
    (16, 6): ("RGBA", "RGBA;16B"),
}


_simple_palette = re.compile(b"^\xff*\x00\xff*$")

MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
"""
Maximum decompressed size for a iTXt or zTXt chunk.
Eliminates decompression bombs where compressed chunks can expand 1000x.
See :ref:`Text in PNG File Format<png-text>`.
"""
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
"""
Set the maximum total text chunk size.
See :ref:`Text in PNG File Format<png-text>`.
"""


# APNG frame disposal modes
class Disposal(IntEnum):
    OP_NONE = 0
    """
    No disposal is done on this frame before rendering the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_BACKGROUND = 1
    """
    This frame’s modified region is cleared to fully transparent black before rendering
    the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_PREVIOUS = 2
    """
    This frame’s modified region is reverted to the previous frame’s contents before
    rendering the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """


# APNG frame blend modes
class Blend(IntEnum):
    OP_SOURCE = 0
    """
    All color components of this frame, including alpha, overwrite the previous output
    image contents.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_OVER = 1
    """
    This frame should be alpha composited with the previous output image contents.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """


def _safe_zlib_decompress(s):
    dobj = zlib.decompressobj()
    plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)
    if dobj.unconsumed_tail:
        msg = "Decompressed Data Too Large"
        raise ValueError(msg)
    return plaintext


def _crc32(data, seed=0):
    return zlib.crc32(data, seed) & 0xFFFFFFFF


# --------------------------------------------------------------------
# Support classes.  Suitable for PNG and related formats like MNG etc.


class ChunkStream:
    def __init__(self, fp: IO[bytes]) -> None:
        self.fp: IO[bytes] | None = fp
        self.queue: list[tuple[bytes, int, int]] | None = []

    def read(self) -> tuple[bytes, int, int]:
        """Fetch a new chunk. Returns header information."""
        cid = None

        assert self.fp is not None
        if self.queue:
            cid, pos, length = self.queue.pop()
            self.fp.seek(pos)
        else:
            s = self.fp.read(8)
            cid = s[4:]
            pos = self.fp.tell()
            length = i32(s)

        if not is_cid(cid):
            if not ImageFile.LOAD_TRUNCATED_IMAGES:
                msg = f"broken PNG file (chunk {repr(cid)})"
                raise SyntaxError(msg)

        return cid, pos, length

    def __enter__(self) -> ChunkStream:
        return self

    def __exit__(self, *args):
        self.close()

    def close(self) -> None:
        self.queue = self.fp = None

    def push(self, cid: bytes, pos: int, length: int) -> None:
        assert self.queue is not None
        self.queue.append((cid, pos, length))

    def call(self, cid, pos, length):
        """Call the appropriate chunk handler"""

        logger.debug("STREAM %r %s %s", cid, pos, length)
        return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)

    def crc(self, cid: bytes, data: bytes) -> None:
        """Read and verify checksum"""

        # Skip CRC checks for ancillary chunks if allowed to load truncated
        # images
        # 5th byte of first char is 1 [specs, section 5.4]
        if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):
            self.crc_skip(cid, data)
            return

        assert self.fp is not None
        try:
            crc1 = _crc32(data, _crc32(cid))
            crc2 = i32(self.fp.read(4))
            if crc1 != crc2:
                msg = f"broken PNG file (bad header checksum in {repr(cid)})"
                raise SyntaxError(msg)
        except struct.error as e:
            msg = f"broken PNG file (incomplete checksum in {repr(cid)})"
            raise SyntaxError(msg) from e

    def crc_skip(self, cid: bytes, data: bytes) -> None:
        """Read checksum"""

        assert self.fp is not None
        self.fp.read(4)

    def verify(self, endchunk: bytes = b"IEND") -> list[bytes]:
        # Simple approach; just calculate checksum for all remaining
        # blocks.  Must be called directly after open.

        cids = []

        while True:
            try:
                cid, pos, length = self.read()
            except struct.error as e:
                msg = "truncated PNG file"
                raise OSError(msg) from e

            if cid == endchunk:
                break
            self.crc(cid, ImageFile._safe_read(self.fp, length))
            cids.append(cid)

        return cids


class iTXt(str):
    """
    Subclass of string to allow iTXt chunks to look like strings while
    keeping their extra information

    """

    @staticmethod
    def __new__(cls, text, lang=None, tkey=None):
        """
        :param cls: the class to use when creating the instance
        :param text: value for this key
        :param lang: language code
        :param tkey: UTF-8 version of the key name
        """

        self = str.__new__(cls, text)
        self.lang = lang
        self.tkey = tkey
        return self


class PngInfo:
    """
    PNG chunk container (for use with save(pnginfo=))

    """

    def __init__(self):
        self.chunks = []

    def add(self, cid, data, after_idat=False):
        """Appends an arbitrary chunk. Use with caution.

        :param cid: a byte string, 4 bytes long.
        :param data: a byte string of the encoded data
        :param after_idat: for use with private chunks. Whether the chunk
                           should be written after IDAT

        """

        chunk = [cid, data]
        if after_idat:
            chunk.append(True)
        self.chunks.append(tuple(chunk))

    def add_itxt(self, key, value, lang="", tkey="", zip=False):
        """Appends an iTXt chunk.

        :param key: latin-1 encodable text key name
        :param value: value for this key
        :param lang: language code
        :param tkey: UTF-8 version of the key name
        :param zip: compression flag

        """

        if not isinstance(key, bytes):
            key = key.encode("latin-1", "strict")
        if not isinstance(value, bytes):
            value = value.encode("utf-8", "strict")
        if not isinstance(lang, bytes):
            lang = lang.encode("utf-8", "strict")
        if not isinstance(tkey, bytes):
            tkey = tkey.encode("utf-8", "strict")

        if zip:
            self.add(
                b"iTXt",
                key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" + zlib.compress(value),
            )
        else:
            self.add(b"iTXt", key + b"\0\0\0" + lang + b"\0" + tkey + b"\0" + value)

    def add_text(self, key, value, zip=False):
        """Appends a text chunk.

        :param key: latin-1 encodable text key name
        :param value: value for this key, text or an
           :py:class:`PIL.PngImagePlugin.iTXt` instance
        :param zip: compression flag

        """
        if isinstance(value, iTXt):
            return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)

        # The tEXt chunk stores latin-1 text
        if not isinstance(value, bytes):
            try:
                value = value.encode("latin-1", "strict")
            except UnicodeError:
                return self.add_itxt(key, value, zip=zip)

        if not isinstance(key, bytes):
            key = key.encode("latin-1", "strict")

        if zip:
            self.add(b"zTXt", key + b"\0\0" + zlib.compress(value))
        else:
            self.add(b"tEXt", key + b"\0" + value)


# --------------------------------------------------------------------
# PNG image stream (IHDR/IEND)


class PngStream(ChunkStream):
    def __init__(self, fp):
        super().__init__(fp)

        # local copies of Image attributes
        self.im_info = {}
        self.im_text = {}
        self.im_size = (0, 0)
        self.im_mode = None
        self.im_tile = None
        self.im_palette = None
        self.im_custom_mimetype = None
        self.im_n_frames = None
        self._seq_num = None
        self.rewind_state = None

        self.text_memory = 0

    def check_text_memory(self, chunklen: int) -> None:
        self.text_memory += chunklen
        if self.text_memory > MAX_TEXT_MEMORY:
            msg = (
                "Too much memory used in text chunks: "
                f"{self.text_memory}>MAX_TEXT_MEMORY"
            )
            raise ValueError(msg)

    def save_rewind(self) -> None:
        self.rewind_state = {
            "info": self.im_info.copy(),
            "tile": self.im_tile,
            "seq_num": self._seq_num,
        }

    def rewind(self) -> None:
        self.im_info = self.rewind_state["info"].copy()
        self.im_tile = self.rewind_state["tile"]
        self._seq_num = self.rewind_state["seq_num"]

    def chunk_iCCP(self, pos: int, length: int) -> bytes:
        # ICC profile
        s = ImageFile._safe_read(self.fp, length)
        # according to PNG spec, the iCCP chunk contains:
        # Profile name  1-79 bytes (character string)
        # Null separator        1 byte (null character)
        # Compression method    1 byte (0)
        # Compressed profile    n bytes (zlib with deflate compression)
        i = s.find(b"\0")
        logger.debug("iCCP profile name %r", s[:i])
        comp_method = s[i + 1]
        logger.debug("Compression method %s", comp_method)
        if comp_method != 0:
            msg = f"Unknown compression method {comp_method} in iCCP chunk"
            raise SyntaxError(msg)
        try:
            icc_profile = _safe_zlib_decompress(s[i + 2 :])
        except ValueError:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                icc_profile = None
            else:
                raise
        except zlib.error:
            icc_profile = None  # FIXME
        self.im_info["icc_profile"] = icc_profile
        return s

    def chunk_IHDR(self, pos: int, length: int) -> bytes:
        # image header
        s = ImageFile._safe_read(self.fp, length)
        if length < 13:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated IHDR chunk"
            raise ValueError(msg)
        self.im_size = i32(s, 0), i32(s, 4)
        try:
            self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
        except Exception:
            pass
        if s[12]:
            self.im_info["interlace"] = 1
        if s[11]:
            msg = "unknown filter category"
            raise SyntaxError(msg)
        return s

    def chunk_IDAT(self, pos, length):
        # image data
        if "bbox" in self.im_info:
            tile = [("zip", self.im_info["bbox"], pos, self.im_rawmode)]
        else:
            if self.im_n_frames is not None:
                self.im_info["default_image"] = True
            tile = [("zip", (0, 0) + self.im_size, pos, self.im_rawmode)]
        self.im_tile = tile
        self.im_idat = length
        msg = "image data found"
        raise EOFError(msg)

    def chunk_IEND(self, pos, length):
        msg = "end of PNG image"
        raise EOFError(msg)

    def chunk_PLTE(self, pos: int, length: int) -> bytes:
        # palette
        s = ImageFile._safe_read(self.fp, length)
        if self.im_mode == "P":
            self.im_palette = "RGB", s
        return s

    def chunk_tRNS(self, pos: int, length: int) -> bytes:
        # transparency
        s = ImageFile._safe_read(self.fp, length)
        if self.im_mode == "P":
            if _simple_palette.match(s):
                # tRNS contains only one full-transparent entry,
                # other entries are full opaque
                i = s.find(b"\0")
                if i >= 0:
                    self.im_info["transparency"] = i
            else:
                # otherwise, we have a byte string with one alpha value
                # for each palette entry
                self.im_info["transparency"] = s
        elif self.im_mode in ("1", "L", "I;16"):
            self.im_info["transparency"] = i16(s)
        elif self.im_mode == "RGB":
            self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
        return s

    def chunk_gAMA(self, pos: int, length: int) -> bytes:
        # gamma setting
        s = ImageFile._safe_read(self.fp, length)
        self.im_info["gamma"] = i32(s) / 100000.0
        return s

    def chunk_cHRM(self, pos: int, length: int) -> bytes:
        # chromaticity, 8 unsigned ints, actual value is scaled by 100,000
        # WP x,y, Red x,y, Green x,y Blue x,y

        s = ImageFile._safe_read(self.fp, length)
        raw_vals = struct.unpack(">%dI" % (len(s) // 4), s)
        self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals)
        return s

    def chunk_sRGB(self, pos: int, length: int) -> bytes:
        # srgb rendering intent, 1 byte
        # 0 perceptual
        # 1 relative colorimetric
        # 2 saturation
        # 3 absolute colorimetric

        s = ImageFile._safe_read(self.fp, length)
        if length < 1:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated sRGB chunk"
            raise ValueError(msg)
        self.im_info["srgb"] = s[0]
        return s

    def chunk_pHYs(self, pos: int, length: int) -> bytes:
        # pixels per unit
        s = ImageFile._safe_read(self.fp, length)
        if length < 9:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated pHYs chunk"
            raise ValueError(msg)
        px, py = i32(s, 0), i32(s, 4)
        unit = s[8]
        if unit == 1:  # meter
            dpi = px * 0.0254, py * 0.0254
            self.im_info["dpi"] = dpi
        elif unit == 0:
            self.im_info["aspect"] = px, py
        return s

    def chunk_tEXt(self, pos: int, length: int) -> bytes:
        # text
        s = ImageFile._safe_read(self.fp, length)
        try:
            k, v = s.split(b"\0", 1)
        except ValueError:
            # fallback for broken tEXt tags
            k = s
            v = b""
        if k:
            k = k.decode("latin-1", "strict")
            v_str = v.decode("latin-1", "replace")

            self.im_info[k] = v if k == "exif" else v_str
            self.im_text[k] = v_str
            self.check_text_memory(len(v_str))

        return s

    def chunk_zTXt(self, pos: int, length: int) -> bytes:
        # compressed text
        s = ImageFile._safe_read(self.fp, length)
        try:
            k, v = s.split(b"\0", 1)
        except ValueError:
            k = s
            v = b""
        if v:
            comp_method = v[0]
        else:
            comp_method = 0
        if comp_method != 0:
            msg = f"Unknown compression method {comp_method} in zTXt chunk"
            raise SyntaxError(msg)
        try:
            v = _safe_zlib_decompress(v[1:])
        except ValueError:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                v = b""
            else:
                raise
        except zlib.error:
            v = b""

        if k:
            k = k.decode("latin-1", "strict")
            v = v.decode("latin-1", "replace")

            self.im_info[k] = self.im_text[k] = v
            self.check_text_memory(len(v))

        return s

    def chunk_iTXt(self, pos: int, length: int) -> bytes:
        # international text
        r = s = ImageFile._safe_read(self.fp, length)
        try:
            k, r = r.split(b"\0", 1)
        except ValueError:
            return s
        if len(r) < 2:
            return s
        cf, cm, r = r[0], r[1], r[2:]
        try:
            lang, tk, v = r.split(b"\0", 2)
        except ValueError:
            return s
        if cf != 0:
            if cm == 0:
                try:
                    v = _safe_zlib_decompress(v)
                except ValueError:
                    if ImageFile.LOAD_TRUNCATED_IMAGES:
                        return s
                    else:
                        raise
                except zlib.error:
                    return s
            else:
                return s
        try:
            k = k.decode("latin-1", "strict")
            lang = lang.decode("utf-8", "strict")
            tk = tk.decode("utf-8", "strict")
            v = v.decode("utf-8", "strict")
        except UnicodeError:
            return s

        self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)
        self.check_text_memory(len(v))

        return s

    def chunk_eXIf(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        self.im_info["exif"] = b"Exif\x00\x00" + s
        return s

    # APNG chunks
    def chunk_acTL(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        if length < 8:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "APNG contains truncated acTL chunk"
            raise ValueError(msg)
        if self.im_n_frames is not None:
            self.im_n_frames = None
            warnings.warn("Invalid APNG, will use default PNG image if possible")
            return s
        n_frames = i32(s)
        if n_frames == 0 or n_frames > 0x80000000:
            warnings.warn("Invalid APNG, will use default PNG image if possible")
            return s
        self.im_n_frames = n_frames
        self.im_info["loop"] = i32(s, 4)
        self.im_custom_mimetype = "image/apng"
        return s

    def chunk_fcTL(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        if length < 26:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "APNG contains truncated fcTL chunk"
            raise ValueError(msg)
        seq = i32(s)
        if (self._seq_num is None and seq != 0) or (
            self._seq_num is not None and self._seq_num != seq - 1
        ):
            msg = "APNG contains frame sequence errors"
            raise SyntaxError(msg)
        self._seq_num = seq
        width, height = i32(s, 4), i32(s, 8)
        px, py = i32(s, 12), i32(s, 16)
        im_w, im_h = self.im_size
        if px + width > im_w or py + height > im_h:
            msg = "APNG contains invalid frames"
            raise SyntaxError(msg)
        self.im_info["bbox"] = (px, py, px + width, py + height)
        delay_num, delay_den = i16(s, 20), i16(s, 22)
        if delay_den == 0:
            delay_den = 100
        self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000
        self.im_info["disposal"] = s[24]
        self.im_info["blend"] = s[25]
        return s

    def chunk_fdAT(self, pos: int, length: int) -> bytes:
        if length < 4:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                s = ImageFile._safe_read(self.fp, length)
                return s
            msg = "APNG contains truncated fDAT chunk"
            raise ValueError(msg)
        s = ImageFile._safe_read(self.fp, 4)
        seq = i32(s)
        if self._seq_num != seq - 1:
            msg = "APNG contains frame sequence errors"
            raise SyntaxError(msg)
        self._seq_num = seq
        return self.chunk_IDAT(pos + 4, length - 4)


# --------------------------------------------------------------------
# PNG reader


def _accept(prefix: bytes) -> bool:
    return prefix[:8] == _MAGIC


##
# Image plugin for PNG images.


class PngImageFile(ImageFile.ImageFile):
    format = "PNG"
    format_description = "Portable network graphics"

    def _open(self) -> None:
        if not _accept(self.fp.read(8)):
            msg = "not a PNG file"
            raise SyntaxError(msg)
        self._fp = self.fp
        self.__frame = 0

        #
        # Parse headers up to the first IDAT or fDAT chunk

        self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []
        self.png: PngStream | None = PngStream(self.fp)

        while True:
            #
            # get next chunk

            cid, pos, length = self.png.read()

            try:
                s = self.png.call(cid, pos, length)
            except EOFError:
                break
            except AttributeError:
                logger.debug("%r %s %s (unknown)", cid, pos, length)
                s = ImageFile._safe_read(self.fp, length)
                if cid[1:2].islower():
                    self.private_chunks.append((cid, s))

            self.png.crc(cid, s)

        #
        # Copy relevant attributes from the PngStream.  An alternative
        # would be to let the PngStream class modify these attributes
        # directly, but that introduces circular references which are
        # difficult to break if things go wrong in the decoder...
        # (believe me, I've tried ;-)

        self._mode = self.png.im_mode
        self._size = self.png.im_size
        self.info = self.png.im_info
        self._text = None
        self.tile = self.png.im_tile
        self.custom_mimetype = self.png.im_custom_mimetype
        self.n_frames = self.png.im_n_frames or 1
        self.default_image = self.info.get("default_image", False)

        if self.png.im_palette:
            rawmode, data = self.png.im_palette
            self.palette = ImagePalette.raw(rawmode, data)

        if cid == b"fdAT":
            self.__prepare_idat = length - 4
        else:
            self.__prepare_idat = length  # used by load_prepare()

        if self.png.im_n_frames is not None:
            self._close_exclusive_fp_after_loading = False
            self.png.save_rewind()
            self.__rewind_idat = self.__prepare_idat
            self.__rewind = self._fp.tell()
            if self.default_image:
                # IDAT chunk contains default image and not first animation frame
                self.n_frames += 1
            self._seek(0)
        self.is_animated = self.n_frames > 1

    @property
    def text(self):
        # experimental
        if self._text is None:
            # iTxt, tEXt and zTXt chunks may appear at the end of the file
            # So load the file to ensure that they are read
            if self.is_animated:
                frame = self.__frame
                # for APNG, seek to the final frame before loading
                self.seek(self.n_frames - 1)
            self.load()
            if self.is_animated:
                self.seek(frame)
        return self._text

    def verify(self) -> None:
        """Verify PNG file"""

        if self.fp is None:
            msg = "verify must be called directly after open"
            raise RuntimeError(msg)

        # back up to beginning of IDAT block
        self.fp.seek(self.tile[0][2] - 8)

        assert self.png is not None
        self.png.verify()
        self.png.close()

        if self._exclusive_fp:
            self.fp.close()
        self.fp = None

    def seek(self, frame: int) -> None:
        if not self._seek_check(frame):
            return
        if frame < self.__frame:
            self._seek(0, True)

        last_frame = self.__frame
        for f in range(self.__frame + 1, frame + 1):
            try:
                self._seek(f)
            except EOFError as e:
                self.seek(last_frame)
                msg = "no more images in APNG file"
                raise EOFError(msg) from e

    def _seek(self, frame, rewind=False):
        if frame == 0:
            if rewind:
                self._fp.seek(self.__rewind)
                self.png.rewind()
                self.__prepare_idat = self.__rewind_idat
                self.im = None
                if self.pyaccess:
                    self.pyaccess = None
                self.info = self.png.im_info
                self.tile = self.png.im_tile
                self.fp = self._fp
            self._prev_im = None
            self.dispose = None
            self.default_image = self.info.get("default_image", False)
            self.dispose_op = self.info.get("disposal")
            self.blend_op = self.info.get("blend")
            self.dispose_extent = self.info.get("bbox")
            self.__frame = 0
        else:
            if frame != self.__frame + 1:
                msg = f"cannot seek to frame {frame}"
                raise ValueError(msg)

            # ensure previous frame was loaded
            self.load()

            if self.dispose:
                self.im.paste(self.dispose, self.dispose_extent)
            self._prev_im = self.im.copy()

            self.fp = self._fp

            # advance to the next frame
            if self.__prepare_idat:
                ImageFile._safe_read(self.fp, self.__prepare_idat)
                self.__prepare_idat = 0
            frame_start = False
            while True:
                self.fp.read(4)  # CRC

                try:
                    cid, pos, length = self.png.read()
                except (struct.error, SyntaxError):
                    break

                if cid == b"IEND":
                    msg = "No more images in APNG file"
                    raise EOFError(msg)
                if cid == b"fcTL":
                    if frame_start:
                        # there must be at least one fdAT chunk between fcTL chunks
                        msg = "APNG missing frame data"
                        raise SyntaxError(msg)
                    frame_start = True

                try:
                    self.png.call(cid, pos, length)
                except UnicodeDecodeError:
                    break
                except EOFError:
                    if cid == b"fdAT":
                        length -= 4
                        if frame_start:
                            self.__prepare_idat = length
                            break
                    ImageFile._safe_read(self.fp, length)
                except AttributeError:
                    logger.debug("%r %s %s (unknown)", cid, pos, length)
                    ImageFile._safe_read(self.fp, length)

            self.__frame = frame
            self.tile = self.png.im_tile
            self.dispose_op = self.info.get("disposal")
            self.blend_op = self.info.get("blend")
            self.dispose_extent = self.info.get("bbox")

            if not self.tile:
                msg = "image not found in APNG frame"
                raise EOFError(msg)

        # setup frame disposal (actual disposal done when needed in the next _seek())
        if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:
            self.dispose_op = Disposal.OP_BACKGROUND

        if self.dispose_op == Disposal.OP_PREVIOUS:
            self.dispose = self._prev_im.copy()
            self.dispose = self._crop(self.dispose, self.dispose_extent)
        elif self.dispose_op == Disposal.OP_BACKGROUND:
            self.dispose = Image.core.fill(self.mode, self.size)
            self.dispose = self._crop(self.dispose, self.dispose_extent)
        else:
            self.dispose = None

    def tell(self) -> int:
        return self.__frame

    def load_prepare(self) -> None:
        """internal: prepare to read PNG file"""

        if self.info.get("interlace"):
            self.decoderconfig = self.decoderconfig + (1,)

        self.__idat = self.__prepare_idat  # used by load_read()
        ImageFile.ImageFile.load_prepare(self)

    def load_read(self, read_bytes: int) -> bytes:
        """internal: read more image data"""

        assert self.png is not None
        while self.__idat == 0:
            # end of chunk, skip forward to next one

            self.fp.read(4)  # CRC

            cid, pos, length = self.png.read()

            if cid not in [b"IDAT", b"DDAT", b"fdAT"]:
                self.png.push(cid, pos, length)
                return b""

            if cid == b"fdAT":
                try:
                    self.png.call(cid, pos, length)
                except EOFError:
                    pass
                self.__idat = length - 4  # sequence_num has already been read
            else:
                self.__idat = length  # empty chunks are allowed

        # read more data from this chunk
        if read_bytes <= 0:
            read_bytes = self.__idat
        else:
            read_bytes = min(read_bytes, self.__idat)

        self.__idat = self.__idat - read_bytes

        return self.fp.read(read_bytes)

    def load_end(self) -> None:
        """internal: finished reading image data"""
        assert self.png is not None
        if self.__idat != 0:
            self.fp.read(self.__idat)
        while True:
            self.fp.read(4)  # CRC

            try:
                cid, pos, length = self.png.read()
            except (struct.error, SyntaxError):
                break

            if cid == b"IEND":
                break
            elif cid == b"fcTL" and self.is_animated:
                # start of the next frame, stop reading
                self.__prepare_idat = 0
                self.png.push(cid, pos, length)
                break

            try:
                self.png.call(cid, pos, length)
            except UnicodeDecodeError:
                break
            except EOFError:
                if cid == b"fdAT":
                    length -= 4
                try:
                    ImageFile._safe_read(self.fp, length)
                except OSError as e:
                    if ImageFile.LOAD_TRUNCATED_IMAGES:
                        break
                    else:
                        raise e
            except AttributeError:
                logger.debug("%r %s %s (unknown)", cid, pos, length)
                s = ImageFile._safe_read(self.fp, length)
                if cid[1:2].islower():
                    self.private_chunks.append((cid, s, True))
        self._text = self.png.im_text
        if not self.is_animated:
            self.png.close()
            self.png = None
        else:
            if self._prev_im and self.blend_op == Blend.OP_OVER:
                updated = self._crop(self.im, self.dispose_extent)
                if self.im.mode == "RGB" and "transparency" in self.info:
                    mask = updated.convert_transparent(
                        "RGBA", self.info["transparency"]
                    )
                else:
                    mask = updated.convert("RGBA")
                self._prev_im.paste(updated, self.dispose_extent, mask)
                self.im = self._prev_im
                if self.pyaccess:
                    self.pyaccess = None

    def _getexif(self):
        if "exif" not in self.info:
            self.load()
        if "exif" not in self.info and "Raw profile type exif" not in self.info:
            return None
        return self.getexif()._get_merged_dict()

    def getexif(self):
        if "exif" not in self.info:
            self.load()

        return super().getexif()

    def getxmp(self):
        """
        Returns a dictionary containing the XMP tags.
        Requires defusedxml to be installed.

        :returns: XMP tags in a dictionary.
        """
        return (
            self._getxmp(self.info["XML:com.adobe.xmp"])
            if "XML:com.adobe.xmp" in self.info
            else {}
        )


# --------------------------------------------------------------------
# PNG writer

_OUTMODES = {
    # supported PIL modes, and corresponding rawmode, bit depth and color type
    "1": ("1", b"\x01", b"\x00"),
    "L;1": ("L;1", b"\x01", b"\x00"),
    "L;2": ("L;2", b"\x02", b"\x00"),
    "L;4": ("L;4", b"\x04", b"\x00"),
    "L": ("L", b"\x08", b"\x00"),
    "LA": ("LA", b"\x08", b"\x04"),
    "I": ("I;16B", b"\x10", b"\x00"),
    "I;16": ("I;16B", b"\x10", b"\x00"),
    "I;16B": ("I;16B", b"\x10", b"\x00"),
    "P;1": ("P;1", b"\x01", b"\x03"),
    "P;2": ("P;2", b"\x02", b"\x03"),
    "P;4": ("P;4", b"\x04", b"\x03"),
    "P": ("P", b"\x08", b"\x03"),
    "RGB": ("RGB", b"\x08", b"\x02"),
    "RGBA": ("RGBA", b"\x08", b"\x06"),
}


def putchunk(fp, cid, *data):
    """Write a PNG chunk (including CRC field)"""

    data = b"".join(data)

    fp.write(o32(len(data)) + cid)
    fp.write(data)
    crc = _crc32(data, _crc32(cid))
    fp.write(o32(crc))


class _idat:
    # wrap output from the encoder in IDAT chunks

    def __init__(self, fp, chunk):
        self.fp = fp
        self.chunk = chunk

    def write(self, data: bytes) -> None:
        self.chunk(self.fp, b"IDAT", data)


class _fdat:
    # wrap encoder output in fdAT chunks

    def __init__(self, fp, chunk, seq_num):
        self.fp = fp
        self.chunk = chunk
        self.seq_num = seq_num

    def write(self, data: bytes) -> None:
        self.chunk(self.fp, b"fdAT", o32(self.seq_num), data)
        self.seq_num += 1


def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):
    duration = im.encoderinfo.get("duration", im.info.get("duration", 0))
    loop = im.encoderinfo.get("loop", im.info.get("loop", 0))
    disposal = im.encoderinfo.get("disposal", im.info.get("disposal", Disposal.OP_NONE))
    blend = im.encoderinfo.get("blend", im.info.get("blend", Blend.OP_SOURCE))

    if default_image:
        chain = itertools.chain(append_images)
    else:
        chain = itertools.chain([im], append_images)

    im_frames = []
    frame_count = 0
    for im_seq in chain:
        for im_frame in ImageSequence.Iterator(im_seq):
            if im_frame.mode == rawmode:
                im_frame = im_frame.copy()
            else:
                im_frame = im_frame.convert(rawmode)
            encoderinfo = im.encoderinfo.copy()
            if isinstance(duration, (list, tuple)):
                encoderinfo["duration"] = duration[frame_count]
            if isinstance(disposal, (list, tuple)):
                encoderinfo["disposal"] = disposal[frame_count]
            if isinstance(blend, (list, tuple)):
                encoderinfo["blend"] = blend[frame_count]
            frame_count += 1

            if im_frames:
                previous = im_frames[-1]
                prev_disposal = previous["encoderinfo"].get("disposal")
                prev_blend = previous["encoderinfo"].get("blend")
                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:
                    prev_disposal = Disposal.OP_BACKGROUND

                if prev_disposal == Disposal.OP_BACKGROUND:
                    base_im = previous["im"].copy()
                    dispose = Image.core.fill("RGBA", im.size, (0, 0, 0, 0))
                    bbox = previous["bbox"]
                    if bbox:
                        dispose = dispose.crop(bbox)
                    else:
                        bbox = (0, 0) + im.size
                    base_im.paste(dispose, bbox)
                elif prev_disposal == Disposal.OP_PREVIOUS:
                    base_im = im_frames[-2]["im"]
                else:
                    base_im = previous["im"]
                delta = ImageChops.subtract_modulo(
                    im_frame.convert("RGBA"), base_im.convert("RGBA")
                )
                bbox = delta.getbbox(alpha_only=False)
                if (
                    not bbox
                    and prev_disposal == encoderinfo.get("disposal")
                    and prev_blend == encoderinfo.get("blend")
                ):
                    previous["encoderinfo"]["duration"] += encoderinfo.get(
                        "duration", duration
                    )
                    continue
            else:
                bbox = None
            if "duration" not in encoderinfo:
                encoderinfo["duration"] = duration
            im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})

    if len(im_frames) == 1 and not default_image:
        return im_frames[0]["im"]

    # animation control
    chunk(
        fp,
        b"acTL",
        o32(len(im_frames)),  # 0: num_frames
        o32(loop),  # 4: num_plays
    )

    # default image IDAT (if it exists)
    if default_image:
        if im.mode != rawmode:
            im = im.convert(rawmode)
        ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])

    seq_num = 0
    for frame, frame_data in enumerate(im_frames):
        im_frame = frame_data["im"]
        if not frame_data["bbox"]:
            bbox = (0, 0) + im_frame.size
        else:
            bbox = frame_data["bbox"]
            im_frame = im_frame.crop(bbox)
        size = im_frame.size
        encoderinfo = frame_data["encoderinfo"]
        frame_duration = int(round(encoderinfo["duration"]))
        frame_disposal = encoderinfo.get("disposal", disposal)
        frame_blend = encoderinfo.get("blend", blend)
        # frame control
        chunk(
            fp,
            b"fcTL",
            o32(seq_num),  # sequence_number
            o32(size[0]),  # width
            o32(size[1]),  # height
            o32(bbox[0]),  # x_offset
            o32(bbox[1]),  # y_offset
            o16(frame_duration),  # delay_numerator
            o16(1000),  # delay_denominator
            o8(frame_disposal),  # dispose_op
            o8(frame_blend),  # blend_op
        )
        seq_num += 1
        # frame data
        if frame == 0 and not default_image:
            # first frame must be in IDAT chunks for backwards compatibility
            ImageFile._save(
                im_frame,
                _idat(fp, chunk),
                [("zip", (0, 0) + im_frame.size, 0, rawmode)],
            )
        else:
            fdat_chunks = _fdat(fp, chunk, seq_num)
            ImageFile._save(
                im_frame,
                fdat_chunks,
                [("zip", (0, 0) + im_frame.size, 0, rawmode)],
            )
            seq_num = fdat_chunks.seq_num


def _save_all(im, fp, filename):
    _save(im, fp, filename, save_all=True)


def _save(im, fp, filename, chunk=putchunk, save_all=False):
    # save an image to disk (called by the save method)

    if save_all:
        default_image = im.encoderinfo.get(
            "default_image", im.info.get("default_image")
        )
        modes = set()
        sizes = set()
        append_images = im.encoderinfo.get("append_images", [])
        for im_seq in itertools.chain([im], append_images):
            for im_frame in ImageSequence.Iterator(im_seq):
                modes.add(im_frame.mode)
                sizes.add(im_frame.size)
        for mode in ("RGBA", "RGB", "P"):
            if mode in modes:
                break
        else:
            mode = modes.pop()
        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))
    else:
        size = im.size
        mode = im.mode

    if mode == "P":
        #
        # attempt to minimize storage requirements for palette images
        if "bits" in im.encoderinfo:
            # number of bits specified by user
            colors = min(1 << im.encoderinfo["bits"], 256)
        else:
            # check palette contents
            if im.palette:
                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)
            else:
                colors = 256

        if colors <= 16:
            if colors <= 2:
                bits = 1
            elif colors <= 4:
                bits = 2
            else:
                bits = 4
            mode = f"{mode};{bits}"

    # encoder options
    im.encoderconfig = (
        im.encoderinfo.get("optimize", False),
        im.encoderinfo.get("compress_level", -1),
        im.encoderinfo.get("compress_type", -1),
        im.encoderinfo.get("dictionary", b""),
    )

    # get the corresponding PNG mode
    try:
        rawmode, bit_depth, color_type = _OUTMODES[mode]
    except KeyError as e:
        msg = f"cannot write mode {mode} as PNG"
        raise OSError(msg) from e

    #
    # write minimal PNG file

    fp.write(_MAGIC)

    chunk(
        fp,
        b"IHDR",
        o32(size[0]),  # 0: size
        o32(size[1]),
        bit_depth,
        color_type,
        b"\0",  # 10: compression
        b"\0",  # 11: filter category
        b"\0",  # 12: interlace flag
    )

    chunks = [b"cHRM", b"gAMA", b"sBIT", b"sRGB", b"tIME"]

    icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
    if icc:
        # ICC profile
        # according to PNG spec, the iCCP chunk contains:
        # Profile name  1-79 bytes (character string)
        # Null separator        1 byte (null character)
        # Compression method    1 byte (0)
        # Compressed profile    n bytes (zlib with deflate compression)
        name = b"ICC Profile"
        data = name + b"\0\0" + zlib.compress(icc)
        chunk(fp, b"iCCP", data)

        # You must either have sRGB or iCCP.
        # Disallow sRGB chunks when an iCCP-chunk has been emitted.
        chunks.remove(b"sRGB")

    info = im.encoderinfo.get("pnginfo")
    if info:
        chunks_multiple_allowed = [b"sPLT", b"iTXt", b"tEXt", b"zTXt"]
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid in chunks:
                chunks.remove(cid)
                chunk(fp, cid, data)
            elif cid in chunks_multiple_allowed:
                chunk(fp, cid, data)
            elif cid[1:2].islower():
                # Private chunk
                after_idat = info_chunk[2:3]
                if not after_idat:
                    chunk(fp, cid, data)

    if im.mode == "P":
        palette_byte_number = colors * 3
        palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
        while len(palette_bytes) < palette_byte_number:
            palette_bytes += b"\0"
        chunk(fp, b"PLTE", palette_bytes)

    transparency = im.encoderinfo.get("transparency", im.info.get("transparency", None))

    if transparency or transparency == 0:
        if im.mode == "P":
            # limit to actual palette size
            alpha_bytes = colors
            if isinstance(transparency, bytes):
                chunk(fp, b"tRNS", transparency[:alpha_bytes])
            else:
                transparency = max(0, min(255, transparency))
                alpha = b"\xFF" * transparency + b"\0"
                chunk(fp, b"tRNS", alpha[:alpha_bytes])
        elif im.mode in ("1", "L", "I", "I;16"):
            transparency = max(0, min(65535, transparency))
            chunk(fp, b"tRNS", o16(transparency))
        elif im.mode == "RGB":
            red, green, blue = transparency
            chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue))
        else:
            if "transparency" in im.encoderinfo:
                # don't bother with transparency if it's an RGBA
                # and it's in the info dict. It's probably just stale.
                msg = "cannot use transparency for this mode"
                raise OSError(msg)
    else:
        if im.mode == "P" and im.im.getpalettemode() == "RGBA":
            alpha = im.im.getpalette("RGBA", "A")
            alpha_bytes = colors
            chunk(fp, b"tRNS", alpha[:alpha_bytes])

    dpi = im.encoderinfo.get("dpi")
    if dpi:
        chunk(
            fp,
            b"pHYs",
            o32(int(dpi[0] / 0.0254 + 0.5)),
            o32(int(dpi[1] / 0.0254 + 0.5)),
            b"\x01",
        )

    if info:
        chunks = [b"bKGD", b"hIST"]
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid in chunks:
                chunks.remove(cid)
                chunk(fp, cid, data)

    exif = im.encoderinfo.get("exif")
    if exif:
        if isinstance(exif, Image.Exif):
            exif = exif.tobytes(8)
        if exif.startswith(b"Exif\x00\x00"):
            exif = exif[6:]
        chunk(fp, b"eXIf", exif)

    if save_all:
        im = _write_multiple_frames(
            im, fp, chunk, rawmode, default_image, append_images
        )
    if im:
        ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])

    if info:
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid[1:2].islower():
                # Private chunk
                after_idat = info_chunk[2:3]
                if after_idat:
                    chunk(fp, cid, data)

    chunk(fp, b"IEND", b"")

    if hasattr(fp, "flush"):
        fp.flush()


# --------------------------------------------------------------------
# PNG chunk converter


def getchunks(im, **params):
    """Return a list of PNG chunks representing this image."""

    class collector:
        data = []

        def write(self, data: bytes) -> None:
            pass

        def append(self, chunk: bytes) -> None:
            self.data.append(chunk)

    def append(fp, cid, *data):
        data = b"".join(data)
        crc = o32(_crc32(data, _crc32(cid)))
        fp.append((cid, data, crc))

    fp = collector()

    try:
        im.encoderinfo = params
        _save(im, fp, None, append)
    finally:
        del im.encoderinfo

    return fp.data


# --------------------------------------------------------------------
# Registry

Image.register_open(PngImageFile.format, PngImageFile, _accept)
Image.register_save(PngImageFile.format, _save)
Image.register_save_all(PngImageFile.format, _save_all)

Image.register_extensions(PngImageFile.format, [".png", ".apng"])

Image.register_mime(PngImageFile.format, "image/png")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
[ISSUE]
### Error When Saving Multiple PNG Frames with Specified Bit Depth

**Description**

When attempting to save multiple frames in PNG format with a specified bit depth, a `ValueError` is raised, preventing the save operation from completing successfully.

**Example Code**

```python
from PIL import Image, ImageSequence

# Create a palette-based image
im = Image.new("P", (128, 128))
out = "temp.png"

# Attempt to save multiple frames with bits=4
im.save(out, bits=4, save_all=True, append_images=[im.copy()])
```

**Expected Behavior**

The image should be saved successfully as a PNG file with the specified bit depth of 4 bits per channel, including all appended frames.

**Actual Behavior**

A `ValueError` is raised with the message "conversion not supported", indicating that the image mode conversion failed during the save operation.

**Error Message**

```
ValueError: conversion not supported
```

[/ISSUE]
{
    "\u001b[1mTestFilePng.test_sanity\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_invalid_file\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_broken\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_bad_text\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_bad_ztxt\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_bad_itxt\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_interlace\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_load_transparent_p\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_load_transparent_rgb\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_p_transparent_palette\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_p_single_transparency\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_p_transparent_black\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_grayscale_transparency\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_rgb_single_transparency\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_load_verify\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_verify_struct_error\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_verify_ignores_crc_error\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_verify_not_ignores_crc_error_in_required_chunk\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_dpi\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_load_float_dpi\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_text\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_itxt\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_nonunicode_text\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_unicode_text\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_scary\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_trns_rgb\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_trns_p\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_trns_null\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_icc_profile\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_discard_icc_profile\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_icc_profile\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_no_icc_profile\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_repr_png\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_repr_png_error_returns_none\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_chunk_order\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_getchunks\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_read_private_chunks\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_roundtrip_private_chunk\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_textual_chunks_after_idat\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_unknown_compression_method\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_padded_idat\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[IHDR]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[sRGB]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[pHYs]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[acTL]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[fcTL]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_chunks[fdAT]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_specify_bits[True]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_specify_bits[False]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_plte_length\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_getxmp\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_exif\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_exif_save\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_exif_from_jpg\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_exif_argument\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_tell\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_seek\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_stdout[True]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_save_stdout[False]\u001b[0m": "PASSED",
    "\u001b[1mTestFilePng.test_truncated_end_chunk\u001b[0m": "PASSED",
    "\u001b[1mTestTruncatedPngPLeaks.test_leak_load\u001b[0m": "PASSED"
}
diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py
index 0d5751962..6b6c01d1b 100644
--- a/src/PIL/PngImagePlugin.py
+++ b/src/PIL/PngImagePlugin.py
@@ -1104,7 +1104,7 @@ class _fdat:
         self.seq_num += 1
 
 
-def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):
+def _write_multiple_frames(im, fp, chunk, mode, rawmode, default_image, append_images):
     duration = im.encoderinfo.get("duration", im.info.get("duration", 0))
     loop = im.encoderinfo.get("loop", im.info.get("loop", 0))
     disposal = im.encoderinfo.get("disposal", im.info.get("disposal", Disposal.OP_NONE))
@@ -1119,10 +1119,10 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
     frame_count = 0
     for im_seq in chain:
         for im_frame in ImageSequence.Iterator(im_seq):
-            if im_frame.mode == rawmode:
+            if im_frame.mode == mode:
                 im_frame = im_frame.copy()
             else:
-                im_frame = im_frame.convert(rawmode)
+                im_frame = im_frame.convert(mode)
             encoderinfo = im.encoderinfo.copy()
             if isinstance(duration, (list, tuple)):
                 encoderinfo["duration"] = duration[frame_count]
@@ -1184,8 +1184,8 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images)
 
     # default image IDAT (if it exists)
     if default_image:
-        if im.mode != rawmode:
-            im = im.convert(rawmode)
+        if im.mode != mode:
+            im = im.convert(mode)
         ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
 
     seq_num = 0
@@ -1262,6 +1262,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
         size = im.size
         mode = im.mode
 
+    outmode = mode
     if mode == "P":
         #
         # attempt to minimize storage requirements for palette images
@@ -1282,7 +1283,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
                 bits = 2
             else:
                 bits = 4
-            mode = f"{mode};{bits}"
+            outmode += f";{bits}"
 
     # encoder options
     im.encoderconfig = (
@@ -1294,7 +1295,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
 
     # get the corresponding PNG mode
     try:
-        rawmode, bit_depth, color_type = _OUTMODES[mode]
+        rawmode, bit_depth, color_type = _OUTMODES[outmode]
     except KeyError as e:
         msg = f"cannot write mode {mode} as PNG"
         raise OSError(msg) from e
@@ -1415,7 +1416,7 @@ def _save(im, fp, filename, chunk=putchunk, save_all=False):
 
     if save_all:
         im = _write_multiple_frames(
-            im, fp, chunk, rawmode, default_image, append_images
+            im, fp, chunk, mode, rawmode, default_image, append_images
         )
     if im:
         ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
 

{"src/PIL/PngImagePlugin.py": "#\n# The Python Imaging Library.\n# $Id$\n#\n# PNG support code\n#\n# See \"PNG (Portable Network Graphics) Specification, version 1.0;\n# W3C Recommendation\", 1996-10-01, Thomas Boutell (ed.).\n#\n# history:\n# 1996-05-06 fl   Created (couldn't resist it)\n# 1996-12-14 fl   Upgraded, added read and verify support (0.2)\n# 1996-12-15 fl   Separate PNG stream parser\n# 1996-12-29 fl   Added write support, added getchunks\n# 1996-12-30 fl   Eliminated circular references in decoder (0.3)\n# 1998-07-12 fl   Read/write 16-bit images as mode I (0.4)\n# 2001-02-08 fl   Added transparency support (from Zircon) (0.5)\n# 2001-04-16 fl   Don't close data source in \"open\" method (0.6)\n# 2004-02-24 fl   Don't even pretend to support interlaced files (0.7)\n# 2004-08-31 fl   Do basic sanity check on chunk identifiers (0.8)\n# 2004-09-20 fl   Added PngInfo chunk container\n# 2004-12-18 fl   Added DPI read support (based on code by Niki Spahiev)\n# 2008-08-13 fl   Added tRNS support for RGB images\n# 2009-03-06 fl   Support for preserving ICC profiles (by Florian Hoech)\n# 2009-03-08 fl   Added zTXT support (from Lowell Alleman)\n# 2009-03-29 fl   Read interlaced PNG files (from Conrado Porto Lopes Gouvua)\n#\n# Copyright (c) 1997-2009 by Secret Labs AB\n# Copyright (c) 1996 by Fredrik Lundh\n#\n# See the README file for information on usage and redistribution.\n#\nfrom __future__ import annotations\n\nimport itertools\nimport logging\nimport re\nimport struct\nimport warnings\nimport zlib\nfrom enum import IntEnum\nfrom typing import IO\n\nfrom . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence\nfrom ._binary import i16be as i16\nfrom ._binary import i32be as i32\nfrom ._binary import o8\nfrom ._binary import o16be as o16\nfrom ._binary import o32be as o32\n\nlogger = logging.getLogger(__name__)\n\nis_cid = re.compile(rb\"\\w\\w\\w\\w\").match\n\n\n_MAGIC = b\"\\211PNG\\r\\n\\032\\n\"\n\n\n_MODES = {\n    # supported bits/color combinations, and corresponding modes/rawmodes\n    # Grayscale\n    (1, 0): (\"1\", \"1\"),\n    (2, 0): (\"L\", \"L;2\"),\n    (4, 0): (\"L\", \"L;4\"),\n    (8, 0): (\"L\", \"L\"),\n    (16, 0): (\"I;16\", \"I;16B\"),\n    # Truecolour\n    (8, 2): (\"RGB\", \"RGB\"),\n    (16, 2): (\"RGB\", \"RGB;16B\"),\n    # Indexed-colour\n    (1, 3): (\"P\", \"P;1\"),\n    (2, 3): (\"P\", \"P;2\"),\n    (4, 3): (\"P\", \"P;4\"),\n    (8, 3): (\"P\", \"P\"),\n    # Grayscale with alpha\n    (8, 4): (\"LA\", \"LA\"),\n    (16, 4): (\"RGBA\", \"LA;16B\"),  # LA;16B->LA not yet available\n    # Truecolour with alpha\n    (8, 6): (\"RGBA\", \"RGBA\"),\n    (16, 6): (\"RGBA\", \"RGBA;16B\"),\n}\n\n\n_simple_palette = re.compile(b\"^\\xff*\\x00\\xff*$\")\n\nMAX_TEXT_CHUNK = ImageFile.SAFEBLOCK\n\"\"\"\nMaximum decompressed size for a iTXt or zTXt chunk.\nEliminates decompression bombs where compressed chunks can expand 1000x.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\nMAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK\n\"\"\"\nSet the maximum total text chunk size.\nSee :ref:`Text in PNG File Format<png-text>`.\n\"\"\"\n\n\n# APNG frame disposal modes\nclass Disposal(IntEnum):\n    OP_NONE = 0\n    \"\"\"\n    No disposal is done on this frame before rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_BACKGROUND = 1\n    \"\"\"\n    This frame\u2019s modified region is cleared to fully transparent black before rendering\n    the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_PREVIOUS = 2\n    \"\"\"\n    This frame\u2019s modified region is reverted to the previous frame\u2019s contents before\n    rendering the next frame.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\n# APNG frame blend modes\nclass Blend(IntEnum):\n    OP_SOURCE = 0\n    \"\"\"\n    All color components of this frame, including alpha, overwrite the previous output\n    image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n    OP_OVER = 1\n    \"\"\"\n    This frame should be alpha composited with the previous output image contents.\n    See :ref:`Saving APNG sequences<apng-saving>`.\n    \"\"\"\n\n\ndef _safe_zlib_decompress(s):\n    dobj = zlib.decompressobj()\n    plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)\n    if dobj.unconsumed_tail:\n        msg = \"Decompressed Data Too Large\"\n        raise ValueError(msg)\n    return plaintext\n\n\ndef _crc32(data, seed=0):\n    return zlib.crc32(data, seed) & 0xFFFFFFFF\n\n\n# --------------------------------------------------------------------\n# Support classes.  Suitable for PNG and related formats like MNG etc.\n\n\nclass ChunkStream:\n    def __init__(self, fp: IO[bytes]) -> None:\n        self.fp: IO[bytes] | None = fp\n        self.queue: list[tuple[bytes, int, int]] | None = []\n\n    def read(self) -> tuple[bytes, int, int]:\n        \"\"\"Fetch a new chunk. Returns header information.\"\"\"\n        cid = None\n\n        assert self.fp is not None\n        if self.queue:\n            cid, pos, length = self.queue.pop()\n            self.fp.seek(pos)\n        else:\n            s = self.fp.read(8)\n            cid = s[4:]\n            pos = self.fp.tell()\n            length = i32(s)\n\n        if not is_cid(cid):\n            if not ImageFile.LOAD_TRUNCATED_IMAGES:\n                msg = f\"broken PNG file (chunk {repr(cid)})\"\n                raise SyntaxError(msg)\n\n        return cid, pos, length\n\n    def __enter__(self) -> ChunkStream:\n        return self\n\n    def __exit__(self, *args):\n        self.close()\n\n    def close(self) -> None:\n        self.queue = self.fp = None\n\n    def push(self, cid: bytes, pos: int, length: int) -> None:\n        assert self.queue is not None\n        self.queue.append((cid, pos, length))\n\n    def call(self, cid, pos, length):\n        \"\"\"Call the appropriate chunk handler\"\"\"\n\n        logger.debug(\"STREAM %r %s %s\", cid, pos, length)\n        return getattr(self, f\"chunk_{cid.decode('ascii')}\")(pos, length)\n\n    def crc(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read and verify checksum\"\"\"\n\n        # Skip CRC checks for ancillary chunks if allowed to load truncated\n        # images\n        # 5th byte of first char is 1 [specs, section 5.4]\n        if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):\n            self.crc_skip(cid, data)\n            return\n\n        assert self.fp is not None\n        try:\n            crc1 = _crc32(data, _crc32(cid))\n            crc2 = i32(self.fp.read(4))\n            if crc1 != crc2:\n                msg = f\"broken PNG file (bad header checksum in {repr(cid)})\"\n                raise SyntaxError(msg)\n        except struct.error as e:\n            msg = f\"broken PNG file (incomplete checksum in {repr(cid)})\"\n            raise SyntaxError(msg) from e\n\n    def crc_skip(self, cid: bytes, data: bytes) -> None:\n        \"\"\"Read checksum\"\"\"\n\n        assert self.fp is not None\n        self.fp.read(4)\n\n    def verify(self, endchunk: bytes = b\"IEND\") -> list[bytes]:\n        # Simple approach; just calculate checksum for all remaining\n        # blocks.  Must be called directly after open.\n\n        cids = []\n\n        while True:\n            try:\n                cid, pos, length = self.read()\n            except struct.error as e:\n                msg = \"truncated PNG file\"\n                raise OSError(msg) from e\n\n            if cid == endchunk:\n                break\n            self.crc(cid, ImageFile._safe_read(self.fp, length))\n            cids.append(cid)\n\n        return cids\n\n\nclass iTXt(str):\n    \"\"\"\n    Subclass of string to allow iTXt chunks to look like strings while\n    keeping their extra information\n\n    \"\"\"\n\n    @staticmethod\n    def __new__(cls, text, lang=None, tkey=None):\n        \"\"\"\n        :param cls: the class to use when creating the instance\n        :param text: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        \"\"\"\n\n        self = str.__new__(cls, text)\n        self.lang = lang\n        self.tkey = tkey\n        return self\n\n\nclass PngInfo:\n    \"\"\"\n    PNG chunk container (for use with save(pnginfo=))\n\n    \"\"\"\n\n    def __init__(self):\n        self.chunks = []\n\n    def add(self, cid, data, after_idat=False):\n        \"\"\"Appends an arbitrary chunk. Use with caution.\n\n        :param cid: a byte string, 4 bytes long.\n        :param data: a byte string of the encoded data\n        :param after_idat: for use with private chunks. Whether the chunk\n                           should be written after IDAT\n\n        \"\"\"\n\n        chunk = [cid, data]\n        if after_idat:\n            chunk.append(True)\n        self.chunks.append(tuple(chunk))\n\n    def add_itxt(self, key, value, lang=\"\", tkey=\"\", zip=False):\n        \"\"\"Appends an iTXt chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key\n        :param lang: language code\n        :param tkey: UTF-8 version of the key name\n        :param zip: compression flag\n\n        \"\"\"\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n        if not isinstance(value, bytes):\n            value = value.encode(\"utf-8\", \"strict\")\n        if not isinstance(lang, bytes):\n            lang = lang.encode(\"utf-8\", \"strict\")\n        if not isinstance(tkey, bytes):\n            tkey = tkey.encode(\"utf-8\", \"strict\")\n\n        if zip:\n            self.add(\n                b\"iTXt\",\n                key + b\"\\0\\x01\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + zlib.compress(value),\n            )\n        else:\n            self.add(b\"iTXt\", key + b\"\\0\\0\\0\" + lang + b\"\\0\" + tkey + b\"\\0\" + value)\n\n    def add_text(self, key, value, zip=False):\n        \"\"\"Appends a text chunk.\n\n        :param key: latin-1 encodable text key name\n        :param value: value for this key, text or an\n           :py:class:`PIL.PngImagePlugin.iTXt` instance\n        :param zip: compression flag\n\n        \"\"\"\n        if isinstance(value, iTXt):\n            return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)\n\n        # The tEXt chunk stores latin-1 text\n        if not isinstance(value, bytes):\n            try:\n                value = value.encode(\"latin-1\", \"strict\")\n            except UnicodeError:\n                return self.add_itxt(key, value, zip=zip)\n\n        if not isinstance(key, bytes):\n            key = key.encode(\"latin-1\", \"strict\")\n\n        if zip:\n            self.add(b\"zTXt\", key + b\"\\0\\0\" + zlib.compress(value))\n        else:\n            self.add(b\"tEXt\", key + b\"\\0\" + value)\n\n\n# --------------------------------------------------------------------\n# PNG image stream (IHDR/IEND)\n\n\nclass PngStream(ChunkStream):\n    def __init__(self, fp):\n        super().__init__(fp)\n\n        # local copies of Image attributes\n        self.im_info = {}\n        self.im_text = {}\n        self.im_size = (0, 0)\n        self.im_mode = None\n        self.im_tile = None\n        self.im_palette = None\n        self.im_custom_mimetype = None\n        self.im_n_frames = None\n        self._seq_num = None\n        self.rewind_state = None\n\n        self.text_memory = 0\n\n    def check_text_memory(self, chunklen: int) -> None:\n        self.text_memory += chunklen\n        if self.text_memory > MAX_TEXT_MEMORY:\n            msg = (\n                \"Too much memory used in text chunks: \"\n                f\"{self.text_memory}>MAX_TEXT_MEMORY\"\n            )\n            raise ValueError(msg)\n\n    def save_rewind(self) -> None:\n        self.rewind_state = {\n            \"info\": self.im_info.copy(),\n            \"tile\": self.im_tile,\n            \"seq_num\": self._seq_num,\n        }\n\n    def rewind(self) -> None:\n        self.im_info = self.rewind_state[\"info\"].copy()\n        self.im_tile = self.rewind_state[\"tile\"]\n        self._seq_num = self.rewind_state[\"seq_num\"]\n\n    def chunk_iCCP(self, pos: int, length: int) -> bytes:\n        # ICC profile\n        s = ImageFile._safe_read(self.fp, length)\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        i = s.find(b\"\\0\")\n        logger.debug(\"iCCP profile name %r\", s[:i])\n        comp_method = s[i + 1]\n        logger.debug(\"Compression method %s\", comp_method)\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in iCCP chunk\"\n            raise SyntaxError(msg)\n        try:\n            icc_profile = _safe_zlib_decompress(s[i + 2 :])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                icc_profile = None\n            else:\n                raise\n        except zlib.error:\n            icc_profile = None  # FIXME\n        self.im_info[\"icc_profile\"] = icc_profile\n        return s\n\n    def chunk_IHDR(self, pos: int, length: int) -> bytes:\n        # image header\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 13:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated IHDR chunk\"\n            raise ValueError(msg)\n        self.im_size = i32(s, 0), i32(s, 4)\n        try:\n            self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]\n        except Exception:\n            pass\n        if s[12]:\n            self.im_info[\"interlace\"] = 1\n        if s[11]:\n            msg = \"unknown filter category\"\n            raise SyntaxError(msg)\n        return s\n\n    def chunk_IDAT(self, pos, length):\n        # image data\n        if \"bbox\" in self.im_info:\n            tile = [(\"zip\", self.im_info[\"bbox\"], pos, self.im_rawmode)]\n        else:\n            if self.im_n_frames is not None:\n                self.im_info[\"default_image\"] = True\n            tile = [(\"zip\", (0, 0) + self.im_size, pos, self.im_rawmode)]\n        self.im_tile = tile\n        self.im_idat = length\n        msg = \"image data found\"\n        raise EOFError(msg)\n\n    def chunk_IEND(self, pos, length):\n        msg = \"end of PNG image\"\n        raise EOFError(msg)\n\n    def chunk_PLTE(self, pos: int, length: int) -> bytes:\n        # palette\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            self.im_palette = \"RGB\", s\n        return s\n\n    def chunk_tRNS(self, pos: int, length: int) -> bytes:\n        # transparency\n        s = ImageFile._safe_read(self.fp, length)\n        if self.im_mode == \"P\":\n            if _simple_palette.match(s):\n                # tRNS contains only one full-transparent entry,\n                # other entries are full opaque\n                i = s.find(b\"\\0\")\n                if i >= 0:\n                    self.im_info[\"transparency\"] = i\n            else:\n                # otherwise, we have a byte string with one alpha value\n                # for each palette entry\n                self.im_info[\"transparency\"] = s\n        elif self.im_mode in (\"1\", \"L\", \"I;16\"):\n            self.im_info[\"transparency\"] = i16(s)\n        elif self.im_mode == \"RGB\":\n            self.im_info[\"transparency\"] = i16(s), i16(s, 2), i16(s, 4)\n        return s\n\n    def chunk_gAMA(self, pos: int, length: int) -> bytes:\n        # gamma setting\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"gamma\"] = i32(s) / 100000.0\n        return s\n\n    def chunk_cHRM(self, pos: int, length: int) -> bytes:\n        # chromaticity, 8 unsigned ints, actual value is scaled by 100,000\n        # WP x,y, Red x,y, Green x,y Blue x,y\n\n        s = ImageFile._safe_read(self.fp, length)\n        raw_vals = struct.unpack(\">%dI\" % (len(s) // 4), s)\n        self.im_info[\"chromaticity\"] = tuple(elt / 100000.0 for elt in raw_vals)\n        return s\n\n    def chunk_sRGB(self, pos: int, length: int) -> bytes:\n        # srgb rendering intent, 1 byte\n        # 0 perceptual\n        # 1 relative colorimetric\n        # 2 saturation\n        # 3 absolute colorimetric\n\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 1:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated sRGB chunk\"\n            raise ValueError(msg)\n        self.im_info[\"srgb\"] = s[0]\n        return s\n\n    def chunk_pHYs(self, pos: int, length: int) -> bytes:\n        # pixels per unit\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 9:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"Truncated pHYs chunk\"\n            raise ValueError(msg)\n        px, py = i32(s, 0), i32(s, 4)\n        unit = s[8]\n        if unit == 1:  # meter\n            dpi = px * 0.0254, py * 0.0254\n            self.im_info[\"dpi\"] = dpi\n        elif unit == 0:\n            self.im_info[\"aspect\"] = px, py\n        return s\n\n    def chunk_tEXt(self, pos: int, length: int) -> bytes:\n        # text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            # fallback for broken tEXt tags\n            k = s\n            v = b\"\"\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v_str = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = v if k == \"exif\" else v_str\n            self.im_text[k] = v_str\n            self.check_text_memory(len(v_str))\n\n        return s\n\n    def chunk_zTXt(self, pos: int, length: int) -> bytes:\n        # compressed text\n        s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, v = s.split(b\"\\0\", 1)\n        except ValueError:\n            k = s\n            v = b\"\"\n        if v:\n            comp_method = v[0]\n        else:\n            comp_method = 0\n        if comp_method != 0:\n            msg = f\"Unknown compression method {comp_method} in zTXt chunk\"\n            raise SyntaxError(msg)\n        try:\n            v = _safe_zlib_decompress(v[1:])\n        except ValueError:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                v = b\"\"\n            else:\n                raise\n        except zlib.error:\n            v = b\"\"\n\n        if k:\n            k = k.decode(\"latin-1\", \"strict\")\n            v = v.decode(\"latin-1\", \"replace\")\n\n            self.im_info[k] = self.im_text[k] = v\n            self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_iTXt(self, pos: int, length: int) -> bytes:\n        # international text\n        r = s = ImageFile._safe_read(self.fp, length)\n        try:\n            k, r = r.split(b\"\\0\", 1)\n        except ValueError:\n            return s\n        if len(r) < 2:\n            return s\n        cf, cm, r = r[0], r[1], r[2:]\n        try:\n            lang, tk, v = r.split(b\"\\0\", 2)\n        except ValueError:\n            return s\n        if cf != 0:\n            if cm == 0:\n                try:\n                    v = _safe_zlib_decompress(v)\n                except ValueError:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        return s\n                    else:\n                        raise\n                except zlib.error:\n                    return s\n            else:\n                return s\n        try:\n            k = k.decode(\"latin-1\", \"strict\")\n            lang = lang.decode(\"utf-8\", \"strict\")\n            tk = tk.decode(\"utf-8\", \"strict\")\n            v = v.decode(\"utf-8\", \"strict\")\n        except UnicodeError:\n            return s\n\n        self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)\n        self.check_text_memory(len(v))\n\n        return s\n\n    def chunk_eXIf(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        self.im_info[\"exif\"] = b\"Exif\\x00\\x00\" + s\n        return s\n\n    # APNG chunks\n    def chunk_acTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 8:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated acTL chunk\"\n            raise ValueError(msg)\n        if self.im_n_frames is not None:\n            self.im_n_frames = None\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        n_frames = i32(s)\n        if n_frames == 0 or n_frames > 0x80000000:\n            warnings.warn(\"Invalid APNG, will use default PNG image if possible\")\n            return s\n        self.im_n_frames = n_frames\n        self.im_info[\"loop\"] = i32(s, 4)\n        self.im_custom_mimetype = \"image/apng\"\n        return s\n\n    def chunk_fcTL(self, pos: int, length: int) -> bytes:\n        s = ImageFile._safe_read(self.fp, length)\n        if length < 26:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                return s\n            msg = \"APNG contains truncated fcTL chunk\"\n            raise ValueError(msg)\n        seq = i32(s)\n        if (self._seq_num is None and seq != 0) or (\n            self._seq_num is not None and self._seq_num != seq - 1\n        ):\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        width, height = i32(s, 4), i32(s, 8)\n        px, py = i32(s, 12), i32(s, 16)\n        im_w, im_h = self.im_size\n        if px + width > im_w or py + height > im_h:\n            msg = \"APNG contains invalid frames\"\n            raise SyntaxError(msg)\n        self.im_info[\"bbox\"] = (px, py, px + width, py + height)\n        delay_num, delay_den = i16(s, 20), i16(s, 22)\n        if delay_den == 0:\n            delay_den = 100\n        self.im_info[\"duration\"] = float(delay_num) / float(delay_den) * 1000\n        self.im_info[\"disposal\"] = s[24]\n        self.im_info[\"blend\"] = s[25]\n        return s\n\n    def chunk_fdAT(self, pos: int, length: int) -> bytes:\n        if length < 4:\n            if ImageFile.LOAD_TRUNCATED_IMAGES:\n                s = ImageFile._safe_read(self.fp, length)\n                return s\n            msg = \"APNG contains truncated fDAT chunk\"\n            raise ValueError(msg)\n        s = ImageFile._safe_read(self.fp, 4)\n        seq = i32(s)\n        if self._seq_num != seq - 1:\n            msg = \"APNG contains frame sequence errors\"\n            raise SyntaxError(msg)\n        self._seq_num = seq\n        return self.chunk_IDAT(pos + 4, length - 4)\n\n\n# --------------------------------------------------------------------\n# PNG reader\n\n\ndef _accept(prefix: bytes) -> bool:\n    return prefix[:8] == _MAGIC\n\n\n##\n# Image plugin for PNG images.\n\n\nclass PngImageFile(ImageFile.ImageFile):\n    format = \"PNG\"\n    format_description = \"Portable network graphics\"\n\n    def _open(self) -> None:\n        if not _accept(self.fp.read(8)):\n            msg = \"not a PNG file\"\n            raise SyntaxError(msg)\n        self._fp = self.fp\n        self.__frame = 0\n\n        #\n        # Parse headers up to the first IDAT or fDAT chunk\n\n        self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []\n        self.png: PngStream | None = PngStream(self.fp)\n\n        while True:\n            #\n            # get next chunk\n\n            cid, pos, length = self.png.read()\n\n            try:\n                s = self.png.call(cid, pos, length)\n            except EOFError:\n                break\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s))\n\n            self.png.crc(cid, s)\n\n        #\n        # Copy relevant attributes from the PngStream.  An alternative\n        # would be to let the PngStream class modify these attributes\n        # directly, but that introduces circular references which are\n        # difficult to break if things go wrong in the decoder...\n        # (believe me, I've tried ;-)\n\n        self._mode = self.png.im_mode\n        self._size = self.png.im_size\n        self.info = self.png.im_info\n        self._text = None\n        self.tile = self.png.im_tile\n        self.custom_mimetype = self.png.im_custom_mimetype\n        self.n_frames = self.png.im_n_frames or 1\n        self.default_image = self.info.get(\"default_image\", False)\n\n        if self.png.im_palette:\n            rawmode, data = self.png.im_palette\n            self.palette = ImagePalette.raw(rawmode, data)\n\n        if cid == b\"fdAT\":\n            self.__prepare_idat = length - 4\n        else:\n            self.__prepare_idat = length  # used by load_prepare()\n\n        if self.png.im_n_frames is not None:\n            self._close_exclusive_fp_after_loading = False\n            self.png.save_rewind()\n            self.__rewind_idat = self.__prepare_idat\n            self.__rewind = self._fp.tell()\n            if self.default_image:\n                # IDAT chunk contains default image and not first animation frame\n                self.n_frames += 1\n            self._seek(0)\n        self.is_animated = self.n_frames > 1\n\n    @property\n    def text(self):\n        # experimental\n        if self._text is None:\n            # iTxt, tEXt and zTXt chunks may appear at the end of the file\n            # So load the file to ensure that they are read\n            if self.is_animated:\n                frame = self.__frame\n                # for APNG, seek to the final frame before loading\n                self.seek(self.n_frames - 1)\n            self.load()\n            if self.is_animated:\n                self.seek(frame)\n        return self._text\n\n    def verify(self) -> None:\n        \"\"\"Verify PNG file\"\"\"\n\n        if self.fp is None:\n            msg = \"verify must be called directly after open\"\n            raise RuntimeError(msg)\n\n        # back up to beginning of IDAT block\n        self.fp.seek(self.tile[0][2] - 8)\n\n        assert self.png is not None\n        self.png.verify()\n        self.png.close()\n\n        if self._exclusive_fp:\n            self.fp.close()\n        self.fp = None\n\n    def seek(self, frame: int) -> None:\n        if not self._seek_check(frame):\n            return\n        if frame < self.__frame:\n            self._seek(0, True)\n\n        last_frame = self.__frame\n        for f in range(self.__frame + 1, frame + 1):\n            try:\n                self._seek(f)\n            except EOFError as e:\n                self.seek(last_frame)\n                msg = \"no more images in APNG file\"\n                raise EOFError(msg) from e\n\n    def _seek(self, frame, rewind=False):\n        if frame == 0:\n            if rewind:\n                self._fp.seek(self.__rewind)\n                self.png.rewind()\n                self.__prepare_idat = self.__rewind_idat\n                self.im = None\n                if self.pyaccess:\n                    self.pyaccess = None\n                self.info = self.png.im_info\n                self.tile = self.png.im_tile\n                self.fp = self._fp\n            self._prev_im = None\n            self.dispose = None\n            self.default_image = self.info.get(\"default_image\", False)\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n            self.__frame = 0\n        else:\n            if frame != self.__frame + 1:\n                msg = f\"cannot seek to frame {frame}\"\n                raise ValueError(msg)\n\n            # ensure previous frame was loaded\n            self.load()\n\n            if self.dispose:\n                self.im.paste(self.dispose, self.dispose_extent)\n            self._prev_im = self.im.copy()\n\n            self.fp = self._fp\n\n            # advance to the next frame\n            if self.__prepare_idat:\n                ImageFile._safe_read(self.fp, self.__prepare_idat)\n                self.__prepare_idat = 0\n            frame_start = False\n            while True:\n                self.fp.read(4)  # CRC\n\n                try:\n                    cid, pos, length = self.png.read()\n                except (struct.error, SyntaxError):\n                    break\n\n                if cid == b\"IEND\":\n                    msg = \"No more images in APNG file\"\n                    raise EOFError(msg)\n                if cid == b\"fcTL\":\n                    if frame_start:\n                        # there must be at least one fdAT chunk between fcTL chunks\n                        msg = \"APNG missing frame data\"\n                        raise SyntaxError(msg)\n                    frame_start = True\n\n                try:\n                    self.png.call(cid, pos, length)\n                except UnicodeDecodeError:\n                    break\n                except EOFError:\n                    if cid == b\"fdAT\":\n                        length -= 4\n                        if frame_start:\n                            self.__prepare_idat = length\n                            break\n                    ImageFile._safe_read(self.fp, length)\n                except AttributeError:\n                    logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                    ImageFile._safe_read(self.fp, length)\n\n            self.__frame = frame\n            self.tile = self.png.im_tile\n            self.dispose_op = self.info.get(\"disposal\")\n            self.blend_op = self.info.get(\"blend\")\n            self.dispose_extent = self.info.get(\"bbox\")\n\n            if not self.tile:\n                msg = \"image not found in APNG frame\"\n                raise EOFError(msg)\n\n        # setup frame disposal (actual disposal done when needed in the next _seek())\n        if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose_op = Disposal.OP_BACKGROUND\n\n        if self.dispose_op == Disposal.OP_PREVIOUS:\n            self.dispose = self._prev_im.copy()\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        elif self.dispose_op == Disposal.OP_BACKGROUND:\n            self.dispose = Image.core.fill(self.mode, self.size)\n            self.dispose = self._crop(self.dispose, self.dispose_extent)\n        else:\n            self.dispose = None\n\n    def tell(self) -> int:\n        return self.__frame\n\n    def load_prepare(self) -> None:\n        \"\"\"internal: prepare to read PNG file\"\"\"\n\n        if self.info.get(\"interlace\"):\n            self.decoderconfig = self.decoderconfig + (1,)\n\n        self.__idat = self.__prepare_idat  # used by load_read()\n        ImageFile.ImageFile.load_prepare(self)\n\n    def load_read(self, read_bytes: int) -> bytes:\n        \"\"\"internal: read more image data\"\"\"\n\n        assert self.png is not None\n        while self.__idat == 0:\n            # end of chunk, skip forward to next one\n\n            self.fp.read(4)  # CRC\n\n            cid, pos, length = self.png.read()\n\n            if cid not in [b\"IDAT\", b\"DDAT\", b\"fdAT\"]:\n                self.png.push(cid, pos, length)\n                return b\"\"\n\n            if cid == b\"fdAT\":\n                try:\n                    self.png.call(cid, pos, length)\n                except EOFError:\n                    pass\n                self.__idat = length - 4  # sequence_num has already been read\n            else:\n                self.__idat = length  # empty chunks are allowed\n\n        # read more data from this chunk\n        if read_bytes <= 0:\n            read_bytes = self.__idat\n        else:\n            read_bytes = min(read_bytes, self.__idat)\n\n        self.__idat = self.__idat - read_bytes\n\n        return self.fp.read(read_bytes)\n\n    def load_end(self) -> None:\n        \"\"\"internal: finished reading image data\"\"\"\n        assert self.png is not None\n        if self.__idat != 0:\n            self.fp.read(self.__idat)\n        while True:\n            self.fp.read(4)  # CRC\n\n            try:\n                cid, pos, length = self.png.read()\n            except (struct.error, SyntaxError):\n                break\n\n            if cid == b\"IEND\":\n                break\n            elif cid == b\"fcTL\" and self.is_animated:\n                # start of the next frame, stop reading\n                self.__prepare_idat = 0\n                self.png.push(cid, pos, length)\n                break\n\n            try:\n                self.png.call(cid, pos, length)\n            except UnicodeDecodeError:\n                break\n            except EOFError:\n                if cid == b\"fdAT\":\n                    length -= 4\n                try:\n                    ImageFile._safe_read(self.fp, length)\n                except OSError as e:\n                    if ImageFile.LOAD_TRUNCATED_IMAGES:\n                        break\n                    else:\n                        raise e\n            except AttributeError:\n                logger.debug(\"%r %s %s (unknown)\", cid, pos, length)\n                s = ImageFile._safe_read(self.fp, length)\n                if cid[1:2].islower():\n                    self.private_chunks.append((cid, s, True))\n        self._text = self.png.im_text\n        if not self.is_animated:\n            self.png.close()\n            self.png = None\n        else:\n            if self._prev_im and self.blend_op == Blend.OP_OVER:\n                updated = self._crop(self.im, self.dispose_extent)\n                if self.im.mode == \"RGB\" and \"transparency\" in self.info:\n                    mask = updated.convert_transparent(\n                        \"RGBA\", self.info[\"transparency\"]\n                    )\n                else:\n                    mask = updated.convert(\"RGBA\")\n                self._prev_im.paste(updated, self.dispose_extent, mask)\n                self.im = self._prev_im\n                if self.pyaccess:\n                    self.pyaccess = None\n\n    def _getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n        if \"exif\" not in self.info and \"Raw profile type exif\" not in self.info:\n            return None\n        return self.getexif()._get_merged_dict()\n\n    def getexif(self):\n        if \"exif\" not in self.info:\n            self.load()\n\n        return super().getexif()\n\n    def getxmp(self):\n        \"\"\"\n        Returns a dictionary containing the XMP tags.\n        Requires defusedxml to be installed.\n\n        :returns: XMP tags in a dictionary.\n        \"\"\"\n        return (\n            self._getxmp(self.info[\"XML:com.adobe.xmp\"])\n            if \"XML:com.adobe.xmp\" in self.info\n            else {}\n        )\n\n\n# --------------------------------------------------------------------\n# PNG writer\n\n_OUTMODES = {\n    # supported PIL modes, and corresponding rawmode, bit depth and color type\n    \"1\": (\"1\", b\"\\x01\", b\"\\x00\"),\n    \"L;1\": (\"L;1\", b\"\\x01\", b\"\\x00\"),\n    \"L;2\": (\"L;2\", b\"\\x02\", b\"\\x00\"),\n    \"L;4\": (\"L;4\", b\"\\x04\", b\"\\x00\"),\n    \"L\": (\"L\", b\"\\x08\", b\"\\x00\"),\n    \"LA\": (\"LA\", b\"\\x08\", b\"\\x04\"),\n    \"I\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"I;16B\": (\"I;16B\", b\"\\x10\", b\"\\x00\"),\n    \"P;1\": (\"P;1\", b\"\\x01\", b\"\\x03\"),\n    \"P;2\": (\"P;2\", b\"\\x02\", b\"\\x03\"),\n    \"P;4\": (\"P;4\", b\"\\x04\", b\"\\x03\"),\n    \"P\": (\"P\", b\"\\x08\", b\"\\x03\"),\n    \"RGB\": (\"RGB\", b\"\\x08\", b\"\\x02\"),\n    \"RGBA\": (\"RGBA\", b\"\\x08\", b\"\\x06\"),\n}\n\n\ndef putchunk(fp, cid, *data):\n    \"\"\"Write a PNG chunk (including CRC field)\"\"\"\n\n    data = b\"\".join(data)\n\n    fp.write(o32(len(data)) + cid)\n    fp.write(data)\n    crc = _crc32(data, _crc32(cid))\n    fp.write(o32(crc))\n\n\nclass _idat:\n    # wrap output from the encoder in IDAT chunks\n\n    def __init__(self, fp, chunk):\n        self.fp = fp\n        self.chunk = chunk\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"IDAT\", data)\n\n\nclass _fdat:\n    # wrap encoder output in fdAT chunks\n\n    def __init__(self, fp, chunk, seq_num):\n        self.fp = fp\n        self.chunk = chunk\n        self.seq_num = seq_num\n\n    def write(self, data: bytes) -> None:\n        self.chunk(self.fp, b\"fdAT\", o32(self.seq_num), data)\n        self.seq_num += 1\n\n\ndef _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):\n    duration = im.encoderinfo.get(\"duration\", im.info.get(\"duration\", 0))\n    loop = im.encoderinfo.get(\"loop\", im.info.get(\"loop\", 0))\n    disposal = im.encoderinfo.get(\"disposal\", im.info.get(\"disposal\", Disposal.OP_NONE))\n    blend = im.encoderinfo.get(\"blend\", im.info.get(\"blend\", Blend.OP_SOURCE))\n\n    if default_image:\n        chain = itertools.chain(append_images)\n    else:\n        chain = itertools.chain([im], append_images)\n\n    im_frames = []\n    frame_count = 0\n    for im_seq in chain:\n        for im_frame in ImageSequence.Iterator(im_seq):\n            if im_frame.mode == rawmode:\n                im_frame = im_frame.copy()\n            else:\n                im_frame = im_frame.convert(rawmode)\n            encoderinfo = im.encoderinfo.copy()\n            if isinstance(duration, (list, tuple)):\n                encoderinfo[\"duration\"] = duration[frame_count]\n            if isinstance(disposal, (list, tuple)):\n                encoderinfo[\"disposal\"] = disposal[frame_count]\n            if isinstance(blend, (list, tuple)):\n                encoderinfo[\"blend\"] = blend[frame_count]\n            frame_count += 1\n\n            if im_frames:\n                previous = im_frames[-1]\n                prev_disposal = previous[\"encoderinfo\"].get(\"disposal\")\n                prev_blend = previous[\"encoderinfo\"].get(\"blend\")\n                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:\n                    prev_disposal = Disposal.OP_BACKGROUND\n\n                if prev_disposal == Disposal.OP_BACKGROUND:\n                    base_im = previous[\"im\"].copy()\n                    dispose = Image.core.fill(\"RGBA\", im.size, (0, 0, 0, 0))\n                    bbox = previous[\"bbox\"]\n                    if bbox:\n                        dispose = dispose.crop(bbox)\n                    else:\n                        bbox = (0, 0) + im.size\n                    base_im.paste(dispose, bbox)\n                elif prev_disposal == Disposal.OP_PREVIOUS:\n                    base_im = im_frames[-2][\"im\"]\n                else:\n                    base_im = previous[\"im\"]\n                delta = ImageChops.subtract_modulo(\n                    im_frame.convert(\"RGBA\"), base_im.convert(\"RGBA\")\n                )\n                bbox = delta.getbbox(alpha_only=False)\n                if (\n                    not bbox\n                    and prev_disposal == encoderinfo.get(\"disposal\")\n                    and prev_blend == encoderinfo.get(\"blend\")\n                ):\n                    previous[\"encoderinfo\"][\"duration\"] += encoderinfo.get(\n                        \"duration\", duration\n                    )\n                    continue\n            else:\n                bbox = None\n            if \"duration\" not in encoderinfo:\n                encoderinfo[\"duration\"] = duration\n            im_frames.append({\"im\": im_frame, \"bbox\": bbox, \"encoderinfo\": encoderinfo})\n\n    if len(im_frames) == 1 and not default_image:\n        return im_frames[0][\"im\"]\n\n    # animation control\n    chunk(\n        fp,\n        b\"acTL\",\n        o32(len(im_frames)),  # 0: num_frames\n        o32(loop),  # 4: num_plays\n    )\n\n    # default image IDAT (if it exists)\n    if default_image:\n        if im.mode != rawmode:\n            im = im.convert(rawmode)\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    seq_num = 0\n    for frame, frame_data in enumerate(im_frames):\n        im_frame = frame_data[\"im\"]\n        if not frame_data[\"bbox\"]:\n            bbox = (0, 0) + im_frame.size\n        else:\n            bbox = frame_data[\"bbox\"]\n            im_frame = im_frame.crop(bbox)\n        size = im_frame.size\n        encoderinfo = frame_data[\"encoderinfo\"]\n        frame_duration = int(round(encoderinfo[\"duration\"]))\n        frame_disposal = encoderinfo.get(\"disposal\", disposal)\n        frame_blend = encoderinfo.get(\"blend\", blend)\n        # frame control\n        chunk(\n            fp,\n            b\"fcTL\",\n            o32(seq_num),  # sequence_number\n            o32(size[0]),  # width\n            o32(size[1]),  # height\n            o32(bbox[0]),  # x_offset\n            o32(bbox[1]),  # y_offset\n            o16(frame_duration),  # delay_numerator\n            o16(1000),  # delay_denominator\n            o8(frame_disposal),  # dispose_op\n            o8(frame_blend),  # blend_op\n        )\n        seq_num += 1\n        # frame data\n        if frame == 0 and not default_image:\n            # first frame must be in IDAT chunks for backwards compatibility\n            ImageFile._save(\n                im_frame,\n                _idat(fp, chunk),\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n        else:\n            fdat_chunks = _fdat(fp, chunk, seq_num)\n            ImageFile._save(\n                im_frame,\n                fdat_chunks,\n                [(\"zip\", (0, 0) + im_frame.size, 0, rawmode)],\n            )\n            seq_num = fdat_chunks.seq_num\n\n\ndef _save_all(im, fp, filename):\n    _save(im, fp, filename, save_all=True)\n\n\ndef _save(im, fp, filename, chunk=putchunk, save_all=False):\n    # save an image to disk (called by the save method)\n\n    if save_all:\n        default_image = im.encoderinfo.get(\n            \"default_image\", im.info.get(\"default_image\")\n        )\n        modes = set()\n        sizes = set()\n        append_images = im.encoderinfo.get(\"append_images\", [])\n        for im_seq in itertools.chain([im], append_images):\n            for im_frame in ImageSequence.Iterator(im_seq):\n                modes.add(im_frame.mode)\n                sizes.add(im_frame.size)\n        for mode in (\"RGBA\", \"RGB\", \"P\"):\n            if mode in modes:\n                break\n        else:\n            mode = modes.pop()\n        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))\n    else:\n        size = im.size\n        mode = im.mode\n\n    if mode == \"P\":\n        #\n        # attempt to minimize storage requirements for palette images\n        if \"bits\" in im.encoderinfo:\n            # number of bits specified by user\n            colors = min(1 << im.encoderinfo[\"bits\"], 256)\n        else:\n            # check palette contents\n            if im.palette:\n                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)\n            else:\n                colors = 256\n\n        if colors <= 16:\n            if colors <= 2:\n                bits = 1\n            elif colors <= 4:\n                bits = 2\n            else:\n                bits = 4\n            mode = f\"{mode};{bits}\"\n\n    # encoder options\n    im.encoderconfig = (\n        im.encoderinfo.get(\"optimize\", False),\n        im.encoderinfo.get(\"compress_level\", -1),\n        im.encoderinfo.get(\"compress_type\", -1),\n        im.encoderinfo.get(\"dictionary\", b\"\"),\n    )\n\n    # get the corresponding PNG mode\n    try:\n        rawmode, bit_depth, color_type = _OUTMODES[mode]\n    except KeyError as e:\n        msg = f\"cannot write mode {mode} as PNG\"\n        raise OSError(msg) from e\n\n    #\n    # write minimal PNG file\n\n    fp.write(_MAGIC)\n\n    chunk(\n        fp,\n        b\"IHDR\",\n        o32(size[0]),  # 0: size\n        o32(size[1]),\n        bit_depth,\n        color_type,\n        b\"\\0\",  # 10: compression\n        b\"\\0\",  # 11: filter category\n        b\"\\0\",  # 12: interlace flag\n    )\n\n    chunks = [b\"cHRM\", b\"gAMA\", b\"sBIT\", b\"sRGB\", b\"tIME\"]\n\n    icc = im.encoderinfo.get(\"icc_profile\", im.info.get(\"icc_profile\"))\n    if icc:\n        # ICC profile\n        # according to PNG spec, the iCCP chunk contains:\n        # Profile name  1-79 bytes (character string)\n        # Null separator        1 byte (null character)\n        # Compression method    1 byte (0)\n        # Compressed profile    n bytes (zlib with deflate compression)\n        name = b\"ICC Profile\"\n        data = name + b\"\\0\\0\" + zlib.compress(icc)\n        chunk(fp, b\"iCCP\", data)\n\n        # You must either have sRGB or iCCP.\n        # Disallow sRGB chunks when an iCCP-chunk has been emitted.\n        chunks.remove(b\"sRGB\")\n\n    info = im.encoderinfo.get(\"pnginfo\")\n    if info:\n        chunks_multiple_allowed = [b\"sPLT\", b\"iTXt\", b\"tEXt\", b\"zTXt\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n            elif cid in chunks_multiple_allowed:\n                chunk(fp, cid, data)\n            elif cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if not after_idat:\n                    chunk(fp, cid, data)\n\n    if im.mode == \"P\":\n        palette_byte_number = colors * 3\n        palette_bytes = im.im.getpalette(\"RGB\")[:palette_byte_number]\n        while len(palette_bytes) < palette_byte_number:\n            palette_bytes += b\"\\0\"\n        chunk(fp, b\"PLTE\", palette_bytes)\n\n    transparency = im.encoderinfo.get(\"transparency\", im.info.get(\"transparency\", None))\n\n    if transparency or transparency == 0:\n        if im.mode == \"P\":\n            # limit to actual palette size\n            alpha_bytes = colors\n            if isinstance(transparency, bytes):\n                chunk(fp, b\"tRNS\", transparency[:alpha_bytes])\n            else:\n                transparency = max(0, min(255, transparency))\n                alpha = b\"\\xFF\" * transparency + b\"\\0\"\n                chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n        elif im.mode in (\"1\", \"L\", \"I\", \"I;16\"):\n            transparency = max(0, min(65535, transparency))\n            chunk(fp, b\"tRNS\", o16(transparency))\n        elif im.mode == \"RGB\":\n            red, green, blue = transparency\n            chunk(fp, b\"tRNS\", o16(red) + o16(green) + o16(blue))\n        else:\n            if \"transparency\" in im.encoderinfo:\n                # don't bother with transparency if it's an RGBA\n                # and it's in the info dict. It's probably just stale.\n                msg = \"cannot use transparency for this mode\"\n                raise OSError(msg)\n    else:\n        if im.mode == \"P\" and im.im.getpalettemode() == \"RGBA\":\n            alpha = im.im.getpalette(\"RGBA\", \"A\")\n            alpha_bytes = colors\n            chunk(fp, b\"tRNS\", alpha[:alpha_bytes])\n\n    dpi = im.encoderinfo.get(\"dpi\")\n    if dpi:\n        chunk(\n            fp,\n            b\"pHYs\",\n            o32(int(dpi[0] / 0.0254 + 0.5)),\n            o32(int(dpi[1] / 0.0254 + 0.5)),\n            b\"\\x01\",\n        )\n\n    if info:\n        chunks = [b\"bKGD\", b\"hIST\"]\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid in chunks:\n                chunks.remove(cid)\n                chunk(fp, cid, data)\n\n    exif = im.encoderinfo.get(\"exif\")\n    if exif:\n        if isinstance(exif, Image.Exif):\n            exif = exif.tobytes(8)\n        if exif.startswith(b\"Exif\\x00\\x00\"):\n            exif = exif[6:]\n        chunk(fp, b\"eXIf\", exif)\n\n    if save_all:\n        im = _write_multiple_frames(\n            im, fp, chunk, rawmode, default_image, append_images\n        )\n    if im:\n        ImageFile._save(im, _idat(fp, chunk), [(\"zip\", (0, 0) + im.size, 0, rawmode)])\n\n    if info:\n        for info_chunk in info.chunks:\n            cid, data = info_chunk[:2]\n            if cid[1:2].islower():\n                # Private chunk\n                after_idat = info_chunk[2:3]\n                if after_idat:\n                    chunk(fp, cid, data)\n\n    chunk(fp, b\"IEND\", b\"\")\n\n    if hasattr(fp, \"flush\"):\n        fp.flush()\n\n\n# --------------------------------------------------------------------\n# PNG chunk converter\n\n\ndef getchunks(im, **params):\n    \"\"\"Return a list of PNG chunks representing this image.\"\"\"\n\n    class collector:\n        data = []\n\n        def write(self, data: bytes) -> None:\n            pass\n\n        def append(self, chunk: bytes) -> None:\n            self.data.append(chunk)\n\n    def append(fp, cid, *data):\n        data = b\"\".join(data)\n        crc = o32(_crc32(data, _crc32(cid)))\n        fp.append((cid, data, crc))\n\n    fp = collector()\n\n    try:\n        im.encoderinfo = params\n        _save(im, fp, None, append)\n    finally:\n        del im.encoderinfo\n\n    return fp.data\n\n\n# --------------------------------------------------------------------\n# Registry\n\nImage.register_open(PngImageFile.format, PngImageFile, _accept)\nImage.register_save(PngImageFile.format, _save)\nImage.register_save_all(PngImageFile.format, _save_all)\n\nImage.register_extensions(PngImageFile.format, [\".png\", \".apng\"])\n\nImage.register_mime(PngImageFile.format, \"image/png\")\n"}
A user will ask you to solve a task. You should first draft your thinking process (inner monologue). Then, generate the solution.

Your response format must follow the template below:
<think>
Your thoughts or/and draft, like working through an exercise on scratch paper. Be as casual and as long as you want until you are confident to generate a correct solution.
</think>
<solution>
Final solution presented to the user.
</solution>
system
We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[ISSUE]
### Error When Saving Multiple PNG Frames with Specified Bit Depth

**Description**

When attempting to save multiple frames in PNG format with a specified bit depth, a `ValueError` is raised, preventing the save operation from completing successfully.

**Example Code**

```python
from PIL import Image, ImageSequence

# Create a palette-based image
im = Image.new("P", (128, 128))
out = "temp.png"

# Attempt to save multiple frames with bits=4
im.save(out, bits=4, save_all=True, append_images=[im.copy()])
```

**Expected Behavior**

The image should be saved successfully as a PNG file with the specified bit depth of 4 bits per channel, including all appended frames.

**Actual Behavior**

A `ValueError` is raised with the message "conversion not supported", indicating that the image mode conversion failed during the save operation.

**Error Message**

```
ValueError: conversion not supported
```

[/ISSUE]
--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.

--- BEGIN FILE ---
```
### src/PIL/PngImagePlugin.py
#
# The Python Imaging Library.
# $Id$
#
# PNG support code
#
# See "PNG (Portable Network Graphics) Specification, version 1.0;
# W3C Recommendation", 1996-10-01, Thomas Boutell (ed.).
#
# history:
# 1996-05-06 fl   Created (couldn't resist it)
# 1996-12-14 fl   Upgraded, added read and verify support (0.2)
# 1996-12-15 fl   Separate PNG stream parser
# 1996-12-29 fl   Added write support, added getchunks
# 1996-12-30 fl   Eliminated circular references in decoder (0.3)
# 1998-07-12 fl   Read/write 16-bit images as mode I (0.4)
# 2001-02-08 fl   Added transparency support (from Zircon) (0.5)
# 2001-04-16 fl   Don't close data source in "open" method (0.6)
# 2004-02-24 fl   Don't even pretend to support interlaced files (0.7)
# 2004-08-31 fl   Do basic sanity check on chunk identifiers (0.8)
# 2004-09-20 fl   Added PngInfo chunk container
# 2004-12-18 fl   Added DPI read support (based on code by Niki Spahiev)
# 2008-08-13 fl   Added tRNS support for RGB images
# 2009-03-06 fl   Support for preserving ICC profiles (by Florian Hoech)
# 2009-03-08 fl   Added zTXT support (from Lowell Alleman)
# 2009-03-29 fl   Read interlaced PNG files (from Conrado Porto Lopes Gouvua)
#
# Copyright (c) 1997-2009 by Secret Labs AB
# Copyright (c) 1996 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations

import itertools
import logging
import re
import struct
import warnings
import zlib
from enum import IntEnum
from typing import IO

from . import Image, ImageChops, ImageFile, ImagePalette, ImageSequence
from ._binary import i16be as i16
from ._binary import i32be as i32
from ._binary import o8
from ._binary import o16be as o16
from ._binary import o32be as o32

logger = logging.getLogger(__name__)

is_cid = re.compile(rb"\w\w\w\w").match


_MAGIC = b"\211PNG\r\n\032\n"


_MODES = {
    # supported bits/color combinations, and corresponding modes/rawmodes
    # Grayscale
    (1, 0): ("1", "1"),
    (2, 0): ("L", "L;2"),
    (4, 0): ("L", "L;4"),
    (8, 0): ("L", "L"),
    (16, 0): ("I;16", "I;16B"),
    # Truecolour
    (8, 2): ("RGB", "RGB"),
    (16, 2): ("RGB", "RGB;16B"),
    # Indexed-colour
    (1, 3): ("P", "P;1"),
    (2, 3): ("P", "P;2"),
    (4, 3): ("P", "P;4"),
    (8, 3): ("P", "P"),
    # Grayscale with alpha
    (8, 4): ("LA", "LA"),
    (16, 4): ("RGBA", "LA;16B"),  # LA;16B->LA not yet available
    # Truecolour with alpha
    (8, 6): ("RGBA", "RGBA"),
    (16, 6): ("RGBA", "RGBA;16B"),
}


_simple_palette = re.compile(b"^\xff*\x00\xff*$")

MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
"""
Maximum decompressed size for a iTXt or zTXt chunk.
Eliminates decompression bombs where compressed chunks can expand 1000x.
See :ref:`Text in PNG File Format<png-text>`.
"""
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
"""
Set the maximum total text chunk size.
See :ref:`Text in PNG File Format<png-text>`.
"""


# APNG frame disposal modes
class Disposal(IntEnum):
    OP_NONE = 0
    """
    No disposal is done on this frame before rendering the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_BACKGROUND = 1
    """
    This frame’s modified region is cleared to fully transparent black before rendering
    the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_PREVIOUS = 2
    """
    This frame’s modified region is reverted to the previous frame’s contents before
    rendering the next frame.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """


# APNG frame blend modes
class Blend(IntEnum):
    OP_SOURCE = 0
    """
    All color components of this frame, including alpha, overwrite the previous output
    image contents.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """
    OP_OVER = 1
    """
    This frame should be alpha composited with the previous output image contents.
    See :ref:`Saving APNG sequences<apng-saving>`.
    """


def _safe_zlib_decompress(s):
    dobj = zlib.decompressobj()
    plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)
    if dobj.unconsumed_tail:
        msg = "Decompressed Data Too Large"
        raise ValueError(msg)
    return plaintext


def _crc32(data, seed=0):
    return zlib.crc32(data, seed) & 0xFFFFFFFF


# --------------------------------------------------------------------
# Support classes.  Suitable for PNG and related formats like MNG etc.


class ChunkStream:
    def __init__(self, fp: IO[bytes]) -> None:
        self.fp: IO[bytes] | None = fp
        self.queue: list[tuple[bytes, int, int]] | None = []

    def read(self) -> tuple[bytes, int, int]:
        """Fetch a new chunk. Returns header information."""
        cid = None

        assert self.fp is not None
        if self.queue:
            cid, pos, length = self.queue.pop()
            self.fp.seek(pos)
        else:
            s = self.fp.read(8)
            cid = s[4:]
            pos = self.fp.tell()
            length = i32(s)

        if not is_cid(cid):
            if not ImageFile.LOAD_TRUNCATED_IMAGES:
                msg = f"broken PNG file (chunk {repr(cid)})"
                raise SyntaxError(msg)

        return cid, pos, length

    def __enter__(self) -> ChunkStream:
        return self

    def __exit__(self, *args):
        self.close()

    def close(self) -> None:
        self.queue = self.fp = None

    def push(self, cid: bytes, pos: int, length: int) -> None:
        assert self.queue is not None
        self.queue.append((cid, pos, length))

    def call(self, cid, pos, length):
        """Call the appropriate chunk handler"""

        logger.debug("STREAM %r %s %s", cid, pos, length)
        return getattr(self, f"chunk_{cid.decode('ascii')}")(pos, length)

    def crc(self, cid: bytes, data: bytes) -> None:
        """Read and verify checksum"""

        # Skip CRC checks for ancillary chunks if allowed to load truncated
        # images
        # 5th byte of first char is 1 [specs, section 5.4]
        if ImageFile.LOAD_TRUNCATED_IMAGES and (cid[0] >> 5 & 1):
            self.crc_skip(cid, data)
            return

        assert self.fp is not None
        try:
            crc1 = _crc32(data, _crc32(cid))
            crc2 = i32(self.fp.read(4))
            if crc1 != crc2:
                msg = f"broken PNG file (bad header checksum in {repr(cid)})"
                raise SyntaxError(msg)
        except struct.error as e:
            msg = f"broken PNG file (incomplete checksum in {repr(cid)})"
            raise SyntaxError(msg) from e

    def crc_skip(self, cid: bytes, data: bytes) -> None:
        """Read checksum"""

        assert self.fp is not None
        self.fp.read(4)

    def verify(self, endchunk: bytes = b"IEND") -> list[bytes]:
        # Simple approach; just calculate checksum for all remaining
        # blocks.  Must be called directly after open.

        cids = []

        while True:
            try:
                cid, pos, length = self.read()
            except struct.error as e:
                msg = "truncated PNG file"
                raise OSError(msg) from e

            if cid == endchunk:
                break
            self.crc(cid, ImageFile._safe_read(self.fp, length))
            cids.append(cid)

        return cids


class iTXt(str):
    """
    Subclass of string to allow iTXt chunks to look like strings while
    keeping their extra information

    """

    @staticmethod
    def __new__(cls, text, lang=None, tkey=None):
        """
        :param cls: the class to use when creating the instance
        :param text: value for this key
        :param lang: language code
        :param tkey: UTF-8 version of the key name
        """

        self = str.__new__(cls, text)
        self.lang = lang
        self.tkey = tkey
        return self


class PngInfo:
    """
    PNG chunk container (for use with save(pnginfo=))

    """

    def __init__(self):
        self.chunks = []

    def add(self, cid, data, after_idat=False):
        """Appends an arbitrary chunk. Use with caution.

        :param cid: a byte string, 4 bytes long.
        :param data: a byte string of the encoded data
        :param after_idat: for use with private chunks. Whether the chunk
                           should be written after IDAT

        """

        chunk = [cid, data]
        if after_idat:
            chunk.append(True)
        self.chunks.append(tuple(chunk))

    def add_itxt(self, key, value, lang="", tkey="", zip=False):
        """Appends an iTXt chunk.

        :param key: latin-1 encodable text key name
        :param value: value for this key
        :param lang: language code
        :param tkey: UTF-8 version of the key name
        :param zip: compression flag

        """

        if not isinstance(key, bytes):
            key = key.encode("latin-1", "strict")
        if not isinstance(value, bytes):
            value = value.encode("utf-8", "strict")
        if not isinstance(lang, bytes):
            lang = lang.encode("utf-8", "strict")
        if not isinstance(tkey, bytes):
            tkey = tkey.encode("utf-8", "strict")

        if zip:
            self.add(
                b"iTXt",
                key + b"\0\x01\0" + lang + b"\0" + tkey + b"\0" + zlib.compress(value),
            )
        else:
            self.add(b"iTXt", key + b"\0\0\0" + lang + b"\0" + tkey + b"\0" + value)

    def add_text(self, key, value, zip=False):
        """Appends a text chunk.

        :param key: latin-1 encodable text key name
        :param value: value for this key, text or an
           :py:class:`PIL.PngImagePlugin.iTXt` instance
        :param zip: compression flag

        """
        if isinstance(value, iTXt):
            return self.add_itxt(key, value, value.lang, value.tkey, zip=zip)

        # The tEXt chunk stores latin-1 text
        if not isinstance(value, bytes):
            try:
                value = value.encode("latin-1", "strict")
            except UnicodeError:
                return self.add_itxt(key, value, zip=zip)

        if not isinstance(key, bytes):
            key = key.encode("latin-1", "strict")

        if zip:
            self.add(b"zTXt", key + b"\0\0" + zlib.compress(value))
        else:
            self.add(b"tEXt", key + b"\0" + value)


# --------------------------------------------------------------------
# PNG image stream (IHDR/IEND)


class PngStream(ChunkStream):
    def __init__(self, fp):
        super().__init__(fp)

        # local copies of Image attributes
        self.im_info = {}
        self.im_text = {}
        self.im_size = (0, 0)
        self.im_mode = None
        self.im_tile = None
        self.im_palette = None
        self.im_custom_mimetype = None
        self.im_n_frames = None
        self._seq_num = None
        self.rewind_state = None

        self.text_memory = 0

    def check_text_memory(self, chunklen: int) -> None:
        self.text_memory += chunklen
        if self.text_memory > MAX_TEXT_MEMORY:
            msg = (
                "Too much memory used in text chunks: "
                f"{self.text_memory}>MAX_TEXT_MEMORY"
            )
            raise ValueError(msg)

    def save_rewind(self) -> None:
        self.rewind_state = {
            "info": self.im_info.copy(),
            "tile": self.im_tile,
            "seq_num": self._seq_num,
        }

    def rewind(self) -> None:
        self.im_info = self.rewind_state["info"].copy()
        self.im_tile = self.rewind_state["tile"]
        self._seq_num = self.rewind_state["seq_num"]

    def chunk_iCCP(self, pos: int, length: int) -> bytes:
        # ICC profile
        s = ImageFile._safe_read(self.fp, length)
        # according to PNG spec, the iCCP chunk contains:
        # Profile name  1-79 bytes (character string)
        # Null separator        1 byte (null character)
        # Compression method    1 byte (0)
        # Compressed profile    n bytes (zlib with deflate compression)
        i = s.find(b"\0")
        logger.debug("iCCP profile name %r", s[:i])
        comp_method = s[i + 1]
        logger.debug("Compression method %s", comp_method)
        if comp_method != 0:
            msg = f"Unknown compression method {comp_method} in iCCP chunk"
            raise SyntaxError(msg)
        try:
            icc_profile = _safe_zlib_decompress(s[i + 2 :])
        except ValueError:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                icc_profile = None
            else:
                raise
        except zlib.error:
            icc_profile = None  # FIXME
        self.im_info["icc_profile"] = icc_profile
        return s

    def chunk_IHDR(self, pos: int, length: int) -> bytes:
        # image header
        s = ImageFile._safe_read(self.fp, length)
        if length < 13:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated IHDR chunk"
            raise ValueError(msg)
        self.im_size = i32(s, 0), i32(s, 4)
        try:
            self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
        except Exception:
            pass
        if s[12]:
            self.im_info["interlace"] = 1
        if s[11]:
            msg = "unknown filter category"
            raise SyntaxError(msg)
        return s

    def chunk_IDAT(self, pos, length):
        # image data
        if "bbox" in self.im_info:
            tile = [("zip", self.im_info["bbox"], pos, self.im_rawmode)]
        else:
            if self.im_n_frames is not None:
                self.im_info["default_image"] = True
            tile = [("zip", (0, 0) + self.im_size, pos, self.im_rawmode)]
        self.im_tile = tile
        self.im_idat = length
        msg = "image data found"
        raise EOFError(msg)

    def chunk_IEND(self, pos, length):
        msg = "end of PNG image"
        raise EOFError(msg)

    def chunk_PLTE(self, pos: int, length: int) -> bytes:
        # palette
        s = ImageFile._safe_read(self.fp, length)
        if self.im_mode == "P":
            self.im_palette = "RGB", s
        return s

    def chunk_tRNS(self, pos: int, length: int) -> bytes:
        # transparency
        s = ImageFile._safe_read(self.fp, length)
        if self.im_mode == "P":
            if _simple_palette.match(s):
                # tRNS contains only one full-transparent entry,
                # other entries are full opaque
                i = s.find(b"\0")
                if i >= 0:
                    self.im_info["transparency"] = i
            else:
                # otherwise, we have a byte string with one alpha value
                # for each palette entry
                self.im_info["transparency"] = s
        elif self.im_mode in ("1", "L", "I;16"):
            self.im_info["transparency"] = i16(s)
        elif self.im_mode == "RGB":
            self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
        return s

    def chunk_gAMA(self, pos: int, length: int) -> bytes:
        # gamma setting
        s = ImageFile._safe_read(self.fp, length)
        self.im_info["gamma"] = i32(s) / 100000.0
        return s

    def chunk_cHRM(self, pos: int, length: int) -> bytes:
        # chromaticity, 8 unsigned ints, actual value is scaled by 100,000
        # WP x,y, Red x,y, Green x,y Blue x,y

        s = ImageFile._safe_read(self.fp, length)
        raw_vals = struct.unpack(">%dI" % (len(s) // 4), s)
        self.im_info["chromaticity"] = tuple(elt / 100000.0 for elt in raw_vals)
        return s

    def chunk_sRGB(self, pos: int, length: int) -> bytes:
        # srgb rendering intent, 1 byte
        # 0 perceptual
        # 1 relative colorimetric
        # 2 saturation
        # 3 absolute colorimetric

        s = ImageFile._safe_read(self.fp, length)
        if length < 1:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated sRGB chunk"
            raise ValueError(msg)
        self.im_info["srgb"] = s[0]
        return s

    def chunk_pHYs(self, pos: int, length: int) -> bytes:
        # pixels per unit
        s = ImageFile._safe_read(self.fp, length)
        if length < 9:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "Truncated pHYs chunk"
            raise ValueError(msg)
        px, py = i32(s, 0), i32(s, 4)
        unit = s[8]
        if unit == 1:  # meter
            dpi = px * 0.0254, py * 0.0254
            self.im_info["dpi"] = dpi
        elif unit == 0:
            self.im_info["aspect"] = px, py
        return s

    def chunk_tEXt(self, pos: int, length: int) -> bytes:
        # text
        s = ImageFile._safe_read(self.fp, length)
        try:
            k, v = s.split(b"\0", 1)
        except ValueError:
            # fallback for broken tEXt tags
            k = s
            v = b""
        if k:
            k = k.decode("latin-1", "strict")
            v_str = v.decode("latin-1", "replace")

            self.im_info[k] = v if k == "exif" else v_str
            self.im_text[k] = v_str
            self.check_text_memory(len(v_str))

        return s

    def chunk_zTXt(self, pos: int, length: int) -> bytes:
        # compressed text
        s = ImageFile._safe_read(self.fp, length)
        try:
            k, v = s.split(b"\0", 1)
        except ValueError:
            k = s
            v = b""
        if v:
            comp_method = v[0]
        else:
            comp_method = 0
        if comp_method != 0:
            msg = f"Unknown compression method {comp_method} in zTXt chunk"
            raise SyntaxError(msg)
        try:
            v = _safe_zlib_decompress(v[1:])
        except ValueError:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                v = b""
            else:
                raise
        except zlib.error:
            v = b""

        if k:
            k = k.decode("latin-1", "strict")
            v = v.decode("latin-1", "replace")

            self.im_info[k] = self.im_text[k] = v
            self.check_text_memory(len(v))

        return s

    def chunk_iTXt(self, pos: int, length: int) -> bytes:
        # international text
        r = s = ImageFile._safe_read(self.fp, length)
        try:
            k, r = r.split(b"\0", 1)
        except ValueError:
            return s
        if len(r) < 2:
            return s
        cf, cm, r = r[0], r[1], r[2:]
        try:
            lang, tk, v = r.split(b"\0", 2)
        except ValueError:
            return s
        if cf != 0:
            if cm == 0:
                try:
                    v = _safe_zlib_decompress(v)
                except ValueError:
                    if ImageFile.LOAD_TRUNCATED_IMAGES:
                        return s
                    else:
                        raise
                except zlib.error:
                    return s
            else:
                return s
        try:
            k = k.decode("latin-1", "strict")
            lang = lang.decode("utf-8", "strict")
            tk = tk.decode("utf-8", "strict")
            v = v.decode("utf-8", "strict")
        except UnicodeError:
            return s

        self.im_info[k] = self.im_text[k] = iTXt(v, lang, tk)
        self.check_text_memory(len(v))

        return s

    def chunk_eXIf(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        self.im_info["exif"] = b"Exif\x00\x00" + s
        return s

    # APNG chunks
    def chunk_acTL(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        if length < 8:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "APNG contains truncated acTL chunk"
            raise ValueError(msg)
        if self.im_n_frames is not None:
            self.im_n_frames = None
            warnings.warn("Invalid APNG, will use default PNG image if possible")
            return s
        n_frames = i32(s)
        if n_frames == 0 or n_frames > 0x80000000:
            warnings.warn("Invalid APNG, will use default PNG image if possible")
            return s
        self.im_n_frames = n_frames
        self.im_info["loop"] = i32(s, 4)
        self.im_custom_mimetype = "image/apng"
        return s

    def chunk_fcTL(self, pos: int, length: int) -> bytes:
        s = ImageFile._safe_read(self.fp, length)
        if length < 26:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                return s
            msg = "APNG contains truncated fcTL chunk"
            raise ValueError(msg)
        seq = i32(s)
        if (self._seq_num is None and seq != 0) or (
            self._seq_num is not None and self._seq_num != seq - 1
        ):
            msg = "APNG contains frame sequence errors"
            raise SyntaxError(msg)
        self._seq_num = seq
        width, height = i32(s, 4), i32(s, 8)
        px, py = i32(s, 12), i32(s, 16)
        im_w, im_h = self.im_size
        if px + width > im_w or py + height > im_h:
            msg = "APNG contains invalid frames"
            raise SyntaxError(msg)
        self.im_info["bbox"] = (px, py, px + width, py + height)
        delay_num, delay_den = i16(s, 20), i16(s, 22)
        if delay_den == 0:
            delay_den = 100
        self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000
        self.im_info["disposal"] = s[24]
        self.im_info["blend"] = s[25]
        return s

    def chunk_fdAT(self, pos: int, length: int) -> bytes:
        if length < 4:
            if ImageFile.LOAD_TRUNCATED_IMAGES:
                s = ImageFile._safe_read(self.fp, length)
                return s
            msg = "APNG contains truncated fDAT chunk"
            raise ValueError(msg)
        s = ImageFile._safe_read(self.fp, 4)
        seq = i32(s)
        if self._seq_num != seq - 1:
            msg = "APNG contains frame sequence errors"
            raise SyntaxError(msg)
        self._seq_num = seq
        return self.chunk_IDAT(pos + 4, length - 4)


# --------------------------------------------------------------------
# PNG reader


def _accept(prefix: bytes) -> bool:
    return prefix[:8] == _MAGIC


##
# Image plugin for PNG images.


class PngImageFile(ImageFile.ImageFile):
    format = "PNG"
    format_description = "Portable network graphics"

    def _open(self) -> None:
        if not _accept(self.fp.read(8)):
            msg = "not a PNG file"
            raise SyntaxError(msg)
        self._fp = self.fp
        self.__frame = 0

        #
        # Parse headers up to the first IDAT or fDAT chunk

        self.private_chunks: list[tuple[bytes, bytes] | tuple[bytes, bytes, bool]] = []
        self.png: PngStream | None = PngStream(self.fp)

        while True:
            #
            # get next chunk

            cid, pos, length = self.png.read()

            try:
                s = self.png.call(cid, pos, length)
            except EOFError:
                break
            except AttributeError:
                logger.debug("%r %s %s (unknown)", cid, pos, length)
                s = ImageFile._safe_read(self.fp, length)
                if cid[1:2].islower():
                    self.private_chunks.append((cid, s))

            self.png.crc(cid, s)

        #
        # Copy relevant attributes from the PngStream.  An alternative
        # would be to let the PngStream class modify these attributes
        # directly, but that introduces circular references which are
        # difficult to break if things go wrong in the decoder...
        # (believe me, I've tried ;-)

        self._mode = self.png.im_mode
        self._size = self.png.im_size
        self.info = self.png.im_info
        self._text = None
        self.tile = self.png.im_tile
        self.custom_mimetype = self.png.im_custom_mimetype
        self.n_frames = self.png.im_n_frames or 1
        self.default_image = self.info.get("default_image", False)

        if self.png.im_palette:
            rawmode, data = self.png.im_palette
            self.palette = ImagePalette.raw(rawmode, data)

        if cid == b"fdAT":
            self.__prepare_idat = length - 4
        else:
            self.__prepare_idat = length  # used by load_prepare()

        if self.png.im_n_frames is not None:
            self._close_exclusive_fp_after_loading = False
            self.png.save_rewind()
            self.__rewind_idat = self.__prepare_idat
            self.__rewind = self._fp.tell()
            if self.default_image:
                # IDAT chunk contains default image and not first animation frame
                self.n_frames += 1
            self._seek(0)
        self.is_animated = self.n_frames > 1

    @property
    def text(self):
        # experimental
        if self._text is None:
            # iTxt, tEXt and zTXt chunks may appear at the end of the file
            # So load the file to ensure that they are read
            if self.is_animated:
                frame = self.__frame
                # for APNG, seek to the final frame before loading
                self.seek(self.n_frames - 1)
            self.load()
            if self.is_animated:
                self.seek(frame)
        return self._text

    def verify(self) -> None:
        """Verify PNG file"""

        if self.fp is None:
            msg = "verify must be called directly after open"
            raise RuntimeError(msg)

        # back up to beginning of IDAT block
        self.fp.seek(self.tile[0][2] - 8)

        assert self.png is not None
        self.png.verify()
        self.png.close()

        if self._exclusive_fp:
            self.fp.close()
        self.fp = None

    def seek(self, frame: int) -> None:
        if not self._seek_check(frame):
            return
        if frame < self.__frame:
            self._seek(0, True)

        last_frame = self.__frame
        for f in range(self.__frame + 1, frame + 1):
            try:
                self._seek(f)
            except EOFError as e:
                self.seek(last_frame)
                msg = "no more images in APNG file"
                raise EOFError(msg) from e

    def _seek(self, frame, rewind=False):
        if frame == 0:
            if rewind:
                self._fp.seek(self.__rewind)
                self.png.rewind()
                self.__prepare_idat = self.__rewind_idat
                self.im = None
                if self.pyaccess:
                    self.pyaccess = None
                self.info = self.png.im_info
                self.tile = self.png.im_tile
                self.fp = self._fp
            self._prev_im = None
            self.dispose = None
            self.default_image = self.info.get("default_image", False)
            self.dispose_op = self.info.get("disposal")
            self.blend_op = self.info.get("blend")
            self.dispose_extent = self.info.get("bbox")
            self.__frame = 0
        else:
            if frame != self.__frame + 1:
                msg = f"cannot seek to frame {frame}"
                raise ValueError(msg)

            # ensure previous frame was loaded
            self.load()

            if self.dispose:
                self.im.paste(self.dispose, self.dispose_extent)
            self._prev_im = self.im.copy()

            self.fp = self._fp

            # advance to the next frame
            if self.__prepare_idat:
                ImageFile._safe_read(self.fp, self.__prepare_idat)
                self.__prepare_idat = 0
            frame_start = False
            while True:
                self.fp.read(4)  # CRC

                try:
                    cid, pos, length = self.png.read()
                except (struct.error, SyntaxError):
                    break

                if cid == b"IEND":
                    msg = "No more images in APNG file"
                    raise EOFError(msg)
                if cid == b"fcTL":
                    if frame_start:
                        # there must be at least one fdAT chunk between fcTL chunks
                        msg = "APNG missing frame data"
                        raise SyntaxError(msg)
                    frame_start = True

                try:
                    self.png.call(cid, pos, length)
                except UnicodeDecodeError:
                    break
                except EOFError:
                    if cid == b"fdAT":
                        length -= 4
                        if frame_start:
                            self.__prepare_idat = length
                            break
                    ImageFile._safe_read(self.fp, length)
                except AttributeError:
                    logger.debug("%r %s %s (unknown)", cid, pos, length)
                    ImageFile._safe_read(self.fp, length)

            self.__frame = frame
            self.tile = self.png.im_tile
            self.dispose_op = self.info.get("disposal")
            self.blend_op = self.info.get("blend")
            self.dispose_extent = self.info.get("bbox")

            if not self.tile:
                msg = "image not found in APNG frame"
                raise EOFError(msg)

        # setup frame disposal (actual disposal done when needed in the next _seek())
        if self._prev_im is None and self.dispose_op == Disposal.OP_PREVIOUS:
            self.dispose_op = Disposal.OP_BACKGROUND

        if self.dispose_op == Disposal.OP_PREVIOUS:
            self.dispose = self._prev_im.copy()
            self.dispose = self._crop(self.dispose, self.dispose_extent)
        elif self.dispose_op == Disposal.OP_BACKGROUND:
            self.dispose = Image.core.fill(self.mode, self.size)
            self.dispose = self._crop(self.dispose, self.dispose_extent)
        else:
            self.dispose = None

    def tell(self) -> int:
        return self.__frame

    def load_prepare(self) -> None:
        """internal: prepare to read PNG file"""

        if self.info.get("interlace"):
            self.decoderconfig = self.decoderconfig + (1,)

        self.__idat = self.__prepare_idat  # used by load_read()
        ImageFile.ImageFile.load_prepare(self)

    def load_read(self, read_bytes: int) -> bytes:
        """internal: read more image data"""

        assert self.png is not None
        while self.__idat == 0:
            # end of chunk, skip forward to next one

            self.fp.read(4)  # CRC

            cid, pos, length = self.png.read()

            if cid not in [b"IDAT", b"DDAT", b"fdAT"]:
                self.png.push(cid, pos, length)
                return b""

            if cid == b"fdAT":
                try:
                    self.png.call(cid, pos, length)
                except EOFError:
                    pass
                self.__idat = length - 4  # sequence_num has already been read
            else:
                self.__idat = length  # empty chunks are allowed

        # read more data from this chunk
        if read_bytes <= 0:
            read_bytes = self.__idat
        else:
            read_bytes = min(read_bytes, self.__idat)

        self.__idat = self.__idat - read_bytes

        return self.fp.read(read_bytes)

    def load_end(self) -> None:
        """internal: finished reading image data"""
        assert self.png is not None
        if self.__idat != 0:
            self.fp.read(self.__idat)
        while True:
            self.fp.read(4)  # CRC

            try:
                cid, pos, length = self.png.read()
            except (struct.error, SyntaxError):
                break

            if cid == b"IEND":
                break
            elif cid == b"fcTL" and self.is_animated:
                # start of the next frame, stop reading
                self.__prepare_idat = 0
                self.png.push(cid, pos, length)
                break

            try:
                self.png.call(cid, pos, length)
            except UnicodeDecodeError:
                break
            except EOFError:
                if cid == b"fdAT":
                    length -= 4
                try:
                    ImageFile._safe_read(self.fp, length)
                except OSError as e:
                    if ImageFile.LOAD_TRUNCATED_IMAGES:
                        break
                    else:
                        raise e
            except AttributeError:
                logger.debug("%r %s %s (unknown)", cid, pos, length)
                s = ImageFile._safe_read(self.fp, length)
                if cid[1:2].islower():
                    self.private_chunks.append((cid, s, True))
        self._text = self.png.im_text
        if not self.is_animated:
            self.png.close()
            self.png = None
        else:
            if self._prev_im and self.blend_op == Blend.OP_OVER:
                updated = self._crop(self.im, self.dispose_extent)
                if self.im.mode == "RGB" and "transparency" in self.info:
                    mask = updated.convert_transparent(
                        "RGBA", self.info["transparency"]
                    )
                else:
                    mask = updated.convert("RGBA")
                self._prev_im.paste(updated, self.dispose_extent, mask)
                self.im = self._prev_im
                if self.pyaccess:
                    self.pyaccess = None

    def _getexif(self):
        if "exif" not in self.info:
            self.load()
        if "exif" not in self.info and "Raw profile type exif" not in self.info:
            return None
        return self.getexif()._get_merged_dict()

    def getexif(self):
        if "exif" not in self.info:
            self.load()

        return super().getexif()

    def getxmp(self):
        """
        Returns a dictionary containing the XMP tags.
        Requires defusedxml to be installed.

        :returns: XMP tags in a dictionary.
        """
        return (
            self._getxmp(self.info["XML:com.adobe.xmp"])
            if "XML:com.adobe.xmp" in self.info
            else {}
        )


# --------------------------------------------------------------------
# PNG writer

_OUTMODES = {
    # supported PIL modes, and corresponding rawmode, bit depth and color type
    "1": ("1", b"\x01", b"\x00"),
    "L;1": ("L;1", b"\x01", b"\x00"),
    "L;2": ("L;2", b"\x02", b"\x00"),
    "L;4": ("L;4", b"\x04", b"\x00"),
    "L": ("L", b"\x08", b"\x00"),
    "LA": ("LA", b"\x08", b"\x04"),
    "I": ("I;16B", b"\x10", b"\x00"),
    "I;16": ("I;16B", b"\x10", b"\x00"),
    "I;16B": ("I;16B", b"\x10", b"\x00"),
    "P;1": ("P;1", b"\x01", b"\x03"),
    "P;2": ("P;2", b"\x02", b"\x03"),
    "P;4": ("P;4", b"\x04", b"\x03"),
    "P": ("P", b"\x08", b"\x03"),
    "RGB": ("RGB", b"\x08", b"\x02"),
    "RGBA": ("RGBA", b"\x08", b"\x06"),
}


def putchunk(fp, cid, *data):
    """Write a PNG chunk (including CRC field)"""

    data = b"".join(data)

    fp.write(o32(len(data)) + cid)
    fp.write(data)
    crc = _crc32(data, _crc32(cid))
    fp.write(o32(crc))


class _idat:
    # wrap output from the encoder in IDAT chunks

    def __init__(self, fp, chunk):
        self.fp = fp
        self.chunk = chunk

    def write(self, data: bytes) -> None:
        self.chunk(self.fp, b"IDAT", data)


class _fdat:
    # wrap encoder output in fdAT chunks

    def __init__(self, fp, chunk, seq_num):
        self.fp = fp
        self.chunk = chunk
        self.seq_num = seq_num

    def write(self, data: bytes) -> None:
        self.chunk(self.fp, b"fdAT", o32(self.seq_num), data)
        self.seq_num += 1


def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images):
    duration = im.encoderinfo.get("duration", im.info.get("duration", 0))
    loop = im.encoderinfo.get("loop", im.info.get("loop", 0))
    disposal = im.encoderinfo.get("disposal", im.info.get("disposal", Disposal.OP_NONE))
    blend = im.encoderinfo.get("blend", im.info.get("blend", Blend.OP_SOURCE))

    if default_image:
        chain = itertools.chain(append_images)
    else:
        chain = itertools.chain([im], append_images)

    im_frames = []
    frame_count = 0
    for im_seq in chain:
        for im_frame in ImageSequence.Iterator(im_seq):
            if im_frame.mode == rawmode:
                im_frame = im_frame.copy()
            else:
                im_frame = im_frame.convert(rawmode)
            encoderinfo = im.encoderinfo.copy()
            if isinstance(duration, (list, tuple)):
                encoderinfo["duration"] = duration[frame_count]
            if isinstance(disposal, (list, tuple)):
                encoderinfo["disposal"] = disposal[frame_count]
            if isinstance(blend, (list, tuple)):
                encoderinfo["blend"] = blend[frame_count]
            frame_count += 1

            if im_frames:
                previous = im_frames[-1]
                prev_disposal = previous["encoderinfo"].get("disposal")
                prev_blend = previous["encoderinfo"].get("blend")
                if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:
                    prev_disposal = Disposal.OP_BACKGROUND

                if prev_disposal == Disposal.OP_BACKGROUND:
                    base_im = previous["im"].copy()
                    dispose = Image.core.fill("RGBA", im.size, (0, 0, 0, 0))
                    bbox = previous["bbox"]
                    if bbox:
                        dispose = dispose.crop(bbox)
                    else:
                        bbox = (0, 0) + im.size
                    base_im.paste(dispose, bbox)
                elif prev_disposal == Disposal.OP_PREVIOUS:
                    base_im = im_frames[-2]["im"]
                else:
                    base_im = previous["im"]
                delta = ImageChops.subtract_modulo(
                    im_frame.convert("RGBA"), base_im.convert("RGBA")
                )
                bbox = delta.getbbox(alpha_only=False)
                if (
                    not bbox
                    and prev_disposal == encoderinfo.get("disposal")
                    and prev_blend == encoderinfo.get("blend")
                ):
                    previous["encoderinfo"]["duration"] += encoderinfo.get(
                        "duration", duration
                    )
                    continue
            else:
                bbox = None
            if "duration" not in encoderinfo:
                encoderinfo["duration"] = duration
            im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo})

    if len(im_frames) == 1 and not default_image:
        return im_frames[0]["im"]

    # animation control
    chunk(
        fp,
        b"acTL",
        o32(len(im_frames)),  # 0: num_frames
        o32(loop),  # 4: num_plays
    )

    # default image IDAT (if it exists)
    if default_image:
        if im.mode != rawmode:
            im = im.convert(rawmode)
        ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])

    seq_num = 0
    for frame, frame_data in enumerate(im_frames):
        im_frame = frame_data["im"]
        if not frame_data["bbox"]:
            bbox = (0, 0) + im_frame.size
        else:
            bbox = frame_data["bbox"]
            im_frame = im_frame.crop(bbox)
        size = im_frame.size
        encoderinfo = frame_data["encoderinfo"]
        frame_duration = int(round(encoderinfo["duration"]))
        frame_disposal = encoderinfo.get("disposal", disposal)
        frame_blend = encoderinfo.get("blend", blend)
        # frame control
        chunk(
            fp,
            b"fcTL",
            o32(seq_num),  # sequence_number
            o32(size[0]),  # width
            o32(size[1]),  # height
            o32(bbox[0]),  # x_offset
            o32(bbox[1]),  # y_offset
            o16(frame_duration),  # delay_numerator
            o16(1000),  # delay_denominator
            o8(frame_disposal),  # dispose_op
            o8(frame_blend),  # blend_op
        )
        seq_num += 1
        # frame data
        if frame == 0 and not default_image:
            # first frame must be in IDAT chunks for backwards compatibility
            ImageFile._save(
                im_frame,
                _idat(fp, chunk),
                [("zip", (0, 0) + im_frame.size, 0, rawmode)],
            )
        else:
            fdat_chunks = _fdat(fp, chunk, seq_num)
            ImageFile._save(
                im_frame,
                fdat_chunks,
                [("zip", (0, 0) + im_frame.size, 0, rawmode)],
            )
            seq_num = fdat_chunks.seq_num


def _save_all(im, fp, filename):
    _save(im, fp, filename, save_all=True)


def _save(im, fp, filename, chunk=putchunk, save_all=False):
    # save an image to disk (called by the save method)

    if save_all:
        default_image = im.encoderinfo.get(
            "default_image", im.info.get("default_image")
        )
        modes = set()
        sizes = set()
        append_images = im.encoderinfo.get("append_images", [])
        for im_seq in itertools.chain([im], append_images):
            for im_frame in ImageSequence.Iterator(im_seq):
                modes.add(im_frame.mode)
                sizes.add(im_frame.size)
        for mode in ("RGBA", "RGB", "P"):
            if mode in modes:
                break
        else:
            mode = modes.pop()
        size = tuple(max(frame_size[i] for frame_size in sizes) for i in range(2))
    else:
        size = im.size
        mode = im.mode

    if mode == "P":
        #
        # attempt to minimize storage requirements for palette images
        if "bits" in im.encoderinfo:
            # number of bits specified by user
            colors = min(1 << im.encoderinfo["bits"], 256)
        else:
            # check palette contents
            if im.palette:
                colors = max(min(len(im.palette.getdata()[1]) // 3, 256), 1)
            else:
                colors = 256

        if colors <= 16:
            if colors <= 2:
                bits = 1
            elif colors <= 4:
                bits = 2
            else:
                bits = 4
            mode = f"{mode};{bits}"

    # encoder options
    im.encoderconfig = (
        im.encoderinfo.get("optimize", False),
        im.encoderinfo.get("compress_level", -1),
        im.encoderinfo.get("compress_type", -1),
        im.encoderinfo.get("dictionary", b""),
    )

    # get the corresponding PNG mode
    try:
        rawmode, bit_depth, color_type = _OUTMODES[mode]
    except KeyError as e:
        msg = f"cannot write mode {mode} as PNG"
        raise OSError(msg) from e

    #
    # write minimal PNG file

    fp.write(_MAGIC)

    chunk(
        fp,
        b"IHDR",
        o32(size[0]),  # 0: size
        o32(size[1]),
        bit_depth,
        color_type,
        b"\0",  # 10: compression
        b"\0",  # 11: filter category
        b"\0",  # 12: interlace flag
    )

    chunks = [b"cHRM", b"gAMA", b"sBIT", b"sRGB", b"tIME"]

    icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
    if icc:
        # ICC profile
        # according to PNG spec, the iCCP chunk contains:
        # Profile name  1-79 bytes (character string)
        # Null separator        1 byte (null character)
        # Compression method    1 byte (0)
        # Compressed profile    n bytes (zlib with deflate compression)
        name = b"ICC Profile"
        data = name + b"\0\0" + zlib.compress(icc)
        chunk(fp, b"iCCP", data)

        # You must either have sRGB or iCCP.
        # Disallow sRGB chunks when an iCCP-chunk has been emitted.
        chunks.remove(b"sRGB")

    info = im.encoderinfo.get("pnginfo")
    if info:
        chunks_multiple_allowed = [b"sPLT", b"iTXt", b"tEXt", b"zTXt"]
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid in chunks:
                chunks.remove(cid)
                chunk(fp, cid, data)
            elif cid in chunks_multiple_allowed:
                chunk(fp, cid, data)
            elif cid[1:2].islower():
                # Private chunk
                after_idat = info_chunk[2:3]
                if not after_idat:
                    chunk(fp, cid, data)

    if im.mode == "P":
        palette_byte_number = colors * 3
        palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]
        while len(palette_bytes) < palette_byte_number:
            palette_bytes += b"\0"
        chunk(fp, b"PLTE", palette_bytes)

    transparency = im.encoderinfo.get("transparency", im.info.get("transparency", None))

    if transparency or transparency == 0:
        if im.mode == "P":
            # limit to actual palette size
            alpha_bytes = colors
            if isinstance(transparency, bytes):
                chunk(fp, b"tRNS", transparency[:alpha_bytes])
            else:
                transparency = max(0, min(255, transparency))
                alpha = b"\xFF" * transparency + b"\0"
                chunk(fp, b"tRNS", alpha[:alpha_bytes])
        elif im.mode in ("1", "L", "I", "I;16"):
            transparency = max(0, min(65535, transparency))
            chunk(fp, b"tRNS", o16(transparency))
        elif im.mode == "RGB":
            red, green, blue = transparency
            chunk(fp, b"tRNS", o16(red) + o16(green) + o16(blue))
        else:
            if "transparency" in im.encoderinfo:
                # don't bother with transparency if it's an RGBA
                # and it's in the info dict. It's probably just stale.
                msg = "cannot use transparency for this mode"
                raise OSError(msg)
    else:
        if im.mode == "P" and im.im.getpalettemode() == "RGBA":
            alpha = im.im.getpalette("RGBA", "A")
            alpha_bytes = colors
            chunk(fp, b"tRNS", alpha[:alpha_bytes])

    dpi = im.encoderinfo.get("dpi")
    if dpi:
        chunk(
            fp,
            b"pHYs",
            o32(int(dpi[0] / 0.0254 + 0.5)),
            o32(int(dpi[1] / 0.0254 + 0.5)),
            b"\x01",
        )

    if info:
        chunks = [b"bKGD", b"hIST"]
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid in chunks:
                chunks.remove(cid)
                chunk(fp, cid, data)

    exif = im.encoderinfo.get("exif")
    if exif:
        if isinstance(exif, Image.Exif):
            exif = exif.tobytes(8)
        if exif.startswith(b"Exif\x00\x00"):
            exif = exif[6:]
        chunk(fp, b"eXIf", exif)

    if save_all:
        im = _write_multiple_frames(
            im, fp, chunk, rawmode, default_image, append_images
        )
    if im:
        ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])

    if info:
        for info_chunk in info.chunks:
            cid, data = info_chunk[:2]
            if cid[1:2].islower():
                # Private chunk
                after_idat = info_chunk[2:3]
                if after_idat:
                    chunk(fp, cid, data)

    chunk(fp, b"IEND", b"")

    if hasattr(fp, "flush"):
        fp.flush()


# --------------------------------------------------------------------
# PNG chunk converter


def getchunks(im, **params):
    """Return a list of PNG chunks representing this image."""

    class collector:
        data = []

        def write(self, data: bytes) -> None:
            pass

        def append(self, chunk: bytes) -> None:
            self.data.append(chunk)

    def append(fp, cid, *data):
        data = b"".join(data)
        crc = o32(_crc32(data, _crc32(cid)))
        fp.append((cid, data, crc))

    fp = collector()

    try:
        im.encoderinfo = params
        _save(im, fp, None, append)
    finally:
        del im.encoderinfo

    return fp.data


# --------------------------------------------------------------------
# Registry

Image.register_open(PngImageFile.format, PngImageFile, _accept)
Image.register_save(PngImageFile.format, _save)
Image.register_save_all(PngImageFile.format, _save_all)

Image.register_extensions(PngImageFile.format, [".png", ".apng"])

Image.register_mime(PngImageFile.format, "image/png")

```
--- END FILE ---

Please first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.

Every *SEARCH/REPLACE* edit must use this format:
1. The file path
2. The start of search block: <<<<<<< SEARCH
3. A contiguous chunk of lines to search for in the existing source code
4. The dividing line: =======
5. The lines to replace into the source code
6. The end of the replace block: >>>>>>> REPLACE

Here is an example:

```python
### mathweb/flask/app.py
<<<<<<< SEARCH
from flask import Flask
=======
import math
from flask import Flask
>>>>>>> REPLACE
```
</solution>

Please note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!
Wrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.
user