Skip to content

Conversation

@radarhere
Copy link
Member

Resolves #8921

When a GIF frame doesn't have a global palette, and an individual frame has a palette where the colours sequentially move from (0, 0, 0) to (255, 255, 255), or even if the palette is less than 256 colours in length and still follows that progression, Pillow reads that as an L mode frame.

def _is_palette_needed(self, p: bytes) -> bool:
for i in range(0, len(p), 3):
if not (i // 3 == p[i] == p[i + 1] == p[i + 2]):
return True
return False

if self._is_palette_needed(p):
palette = ImagePalette.raw("RGB", p)
else:
palette = False

self._frame_palette = palette if palette is not None else self.global_palette

temp_mode = "P" if self._frame_palette else "L"

GIF frames may have transparency though, and

if self._frame_transparency is not None:
self.im.putpalettealpha(self._frame_transparency, 0)

is not able to accomodate an L mode frame with transparency.

This fixes that.

@radarhere radarhere added the GIF label Apr 24, 2025
@hugovk hugovk merged commit e7a53ba into python-pillow:main Jun 30, 2025
55 checks passed
@radarhere radarhere deleted the l_mode_with_transparency branch June 30, 2025 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2 participants