Fixed reloading palette

This commit is contained in:
Andrew Murray 2021-06-28 22:24:43 +10:00
parent 461628845d
commit 1ee30ded0b
4 changed files with 14 additions and 11 deletions

View File

@ -93,7 +93,7 @@ def test_trns_p(tmp_path):
im_l.save(f) im_l.save(f)
im_rgb = im.convert("RGB") im_rgb = im.convert("RGB")
assert im_rgb.info["transparency"] == (0, 0, 0) # undone assert im_rgb.info["transparency"] == (0, 1, 2) # undone
im_rgb.save(f) im_rgb.save(f)

View File

@ -2,7 +2,7 @@ import pytest
from PIL import Image, ImagePalette from PIL import Image, ImagePalette
from .helper import assert_image_equal_tofile from .helper import assert_image_equal, assert_image_equal_tofile
def test_sanity(): def test_sanity():
@ -14,6 +14,13 @@ def test_sanity():
ImagePalette.ImagePalette("RGB", list(range(256)) * 3, 10) ImagePalette.ImagePalette("RGB", list(range(256)) * 3, 10)
def test_reload():
im = Image.open("Tests/images/hopper.gif")
original = im.copy()
im.palette.dirty = 1
assert_image_equal(im.convert("RGB"), original.convert("RGB"))
def test_getcolor(): def test_getcolor():
palette = ImagePalette.ImagePalette() palette = ImagePalette.ImagePalette()
@ -129,7 +136,7 @@ def test_getdata():
mode, data_out = palette.getdata() mode, data_out = palette.getdata()
# Assert # Assert
assert mode == "RGB;L" assert mode == "RGB"
def test_rawmode_getdata(): def test_rawmode_getdata():

View File

@ -1834,23 +1834,19 @@ class Image:
m_im = self.copy() m_im = self.copy()
m_im.mode = "P" m_im.mode = "P"
m_im.palette = ImagePalette.ImagePalette( m_im.palette = ImagePalette.ImagePalette("RGB", palette=mapping_palette * 3)
"RGB", palette=mapping_palette * 3, size=768
)
# possibly set palette dirty, then # possibly set palette dirty, then
# m_im.putpalette(mapping_palette, 'L') # converts to 'P' # m_im.putpalette(mapping_palette, 'L') # converts to 'P'
# or just force it. # or just force it.
# UNDONE -- this is part of the general issue with palettes # UNDONE -- this is part of the general issue with palettes
m_im.im.putpalette(*m_im.palette.getdata()) m_im.im.putpalette("RGB;L", m_im.palette.tobytes())
m_im = m_im.convert("L") m_im = m_im.convert("L")
# Internally, we require 768 bytes for a palette. # Internally, we require 768 bytes for a palette.
new_palette_bytes = palette_bytes + (768 - len(palette_bytes)) * b"\x00" new_palette_bytes = palette_bytes + (768 - len(palette_bytes)) * b"\x00"
m_im.putpalette(new_palette_bytes) m_im.putpalette(new_palette_bytes)
m_im.palette = ImagePalette.ImagePalette( m_im.palette = ImagePalette.ImagePalette("RGB", palette=palette_bytes)
"RGB", palette=palette_bytes, size=len(palette_bytes)
)
return m_im return m_im

View File

@ -80,7 +80,7 @@ class ImagePalette:
""" """
if self.rawmode: if self.rawmode:
return self.rawmode, self.palette return self.rawmode, self.palette
return self.mode + ";L", self.tobytes() return self.mode, self.tobytes()
def tobytes(self): def tobytes(self):
"""Convert palette to bytes. """Convert palette to bytes.