mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-02 04:03:33 +03:00
Corrected P mode save
This commit is contained in:
parent
8878511476
commit
6f42cfeb14
|
@ -43,6 +43,11 @@ def roundtrip(tmp_path: Path, mode: str) -> None:
|
||||||
|
|
||||||
im.save(outfile)
|
im.save(outfile)
|
||||||
converted = open_with_magick(magick, tmp_path, outfile)
|
converted = open_with_magick(magick, tmp_path, outfile)
|
||||||
|
if mode == "P":
|
||||||
|
assert converted.mode == "P"
|
||||||
|
|
||||||
|
im = im.convert("RGB")
|
||||||
|
converted = converted.convert("RGB")
|
||||||
assert_image_equal(converted, im)
|
assert_image_equal(converted, im)
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,7 +60,6 @@ def test_monochrome(tmp_path: Path) -> None:
|
||||||
roundtrip(tmp_path, mode)
|
roundtrip(tmp_path, mode)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail(reason="Palm P image is wrong")
|
|
||||||
def test_p_mode(tmp_path: Path) -> None:
|
def test_p_mode(tmp_path: Path) -> None:
|
||||||
# Arrange
|
# Arrange
|
||||||
mode = "P"
|
mode = "P"
|
||||||
|
|
|
@ -116,9 +116,6 @@ _COMPRESSION_TYPES = {"none": 0xFF, "rle": 0x01, "scanline": 0x00}
|
||||||
|
|
||||||
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
if im.mode == "P":
|
if im.mode == "P":
|
||||||
# we assume this is a color Palm image with the standard colormap,
|
|
||||||
# unless the "info" dict has a "custom-colormap" field
|
|
||||||
|
|
||||||
rawmode = "P"
|
rawmode = "P"
|
||||||
bpp = 8
|
bpp = 8
|
||||||
version = 1
|
version = 1
|
||||||
|
@ -172,12 +169,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
compression_type = _COMPRESSION_TYPES["none"]
|
compression_type = _COMPRESSION_TYPES["none"]
|
||||||
|
|
||||||
flags = 0
|
flags = 0
|
||||||
if im.mode == "P" and "custom-colormap" in im.info:
|
if im.mode == "P":
|
||||||
assert im.palette is not None
|
flags |= _FLAGS["custom-colormap"]
|
||||||
flags = flags & _FLAGS["custom-colormap"]
|
colormap = im.im.getpalette()
|
||||||
colormapsize = 4 * 256 + 2
|
colors = len(colormap) // 3
|
||||||
colormapmode = im.palette.mode
|
colormapsize = 4 * colors + 2
|
||||||
colormap = im.getdata().getpalette()
|
|
||||||
else:
|
else:
|
||||||
colormapsize = 0
|
colormapsize = 0
|
||||||
|
|
||||||
|
@ -196,22 +192,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
|
||||||
# now write colormap if necessary
|
# now write colormap if necessary
|
||||||
|
|
||||||
if colormapsize > 0:
|
if colormapsize:
|
||||||
fp.write(o16b(256))
|
fp.write(o16b(colors))
|
||||||
for i in range(256):
|
for i in range(colors):
|
||||||
fp.write(o8(i))
|
fp.write(o8(i))
|
||||||
if colormapmode == "RGB":
|
fp.write(colormap[3 * i : 3 * i + 3])
|
||||||
fp.write(
|
|
||||||
o8(colormap[3 * i])
|
|
||||||
+ o8(colormap[3 * i + 1])
|
|
||||||
+ o8(colormap[3 * i + 2])
|
|
||||||
)
|
|
||||||
elif colormapmode == "RGBA":
|
|
||||||
fp.write(
|
|
||||||
o8(colormap[4 * i])
|
|
||||||
+ o8(colormap[4 * i + 1])
|
|
||||||
+ o8(colormap[4 * i + 2])
|
|
||||||
)
|
|
||||||
|
|
||||||
# now convert data to raw form
|
# now convert data to raw form
|
||||||
ImageFile._save(
|
ImageFile._save(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user