Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

`ValueError: image has no palette` when show GIF

Open quaeast opened this issue 9 months ago • 5 comments

What did you do?

from PIL import Image
import io


image_1 = '/Users/fangzidong/git_repo/mmwrastra/debug_data/debug_gif_img/no_palette.gif'


with open(image_1, 'rb') as f:
    image_bytes = f.read()
image = Image.open(io.BytesIO(image_bytes))
image.show()

What actually happened?

I got this error when i opening this image:

Image

Traceback (most recent call last):
  File "/Users/fangzidong/git_repo/mmwrastra/plg/image_io_debug.py", line 11, in <module>
    image.show()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/Image.py", line 2641, in show
    _show(self, title=title)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/Image.py", line 3811, in _show
    ImageShow.show(image, **options)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageShow.py", line 61, in show
    if viewer.show(image, title=title, **options):
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageShow.py", line 85, in show
    return self.show_image(image, **options)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageShow.py", line 112, in show_image
    return self.show_file(self.save_image(image), **options)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageShow.py", line 108, in save_image
    return image._dump(format=self.get_format(image), **self.options)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/Image.py", line 666, in _dump
    self.save(filename, format, **options)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/Image.py", line 2581, in save
    save_handler(self, fp, filename)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/PngImagePlugin.py", line 1301, in _save_all
    _save(im, fp, filename, save_all=True)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/PngImagePlugin.py", line 1321, in _save
    for im_frame in ImageSequence.Iterator(im_seq):
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageSequence.py", line 56, in __next__
    self.im.seek(self.position)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/GifImagePlugin.py", line 165, in seek
    self._seek(f)
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/GifImagePlugin.py", line 186, in _seek
    self.load()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageFile.py", line 400, in load
    self.load_end()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/GifImagePlugin.py", line 480, in load_end
    self.im.putpalettealpha(self._frame_transparency, 0)
ValueError: image has no palette

What are your OS, Python and Pillow versions?

  • OS: macOS 15.0.1 24A348 arm
  • Python: Python 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:35:25) [Clang 16.0.6 ]
  • Pillow: 11.2.1
--------------------------------------------------------------------
Pillow 11.2.1
Python 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:35:25) [Clang 16.0.6 ]
--------------------------------------------------------------------
Python executable is /Users/fangzidong/miniconda3/envs/torch/bin/python3
System Python files loaded from /Users/fangzidong/miniconda3/envs/torch
--------------------------------------------------------------------
Python Pillow modules loaded from /Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL
Binary Pillow modules loaded from /Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.2.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.3
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.5.0
*** AVIF support not installed
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.2.4
--- LIBTIFF support ok, loaded 4.7.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.10, harfbuzz 11.0.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------

quaeast avatar Apr 21 '25 12:04 quaeast

Hi, I would like to work on this issue!

luoxixiang23921 avatar Apr 22 '25 00:04 luoxixiang23921

@quaeast Why not just call Image.open ?

Python 3.12.9 (main, Mar 23 2025, 16:34:48) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> img = Image.open("435668030-8d16446a-4b29-4a0f-9249-52edfa1532c1.gif")
>>> img.palette
<PIL.ImagePalette.ImagePalette object at 0x10546f740>

aclark4life avatar Apr 22 '25 01:04 aclark4life

@aclark4life I tried to open it in this way, and it will return the same error. The key of this error is this image has a palette, but pillow tells me it does not have.

And I also tried to convert this image to ndarray frame by frame, but when it turns to the 9th frame, it will return ValueError: image has no palette. I checked the bytes of this gif, and i confirm this gif's every frame has a palette. I think may be it is because that some frames of this image is gray scale image and pillow convert it to L mode. This error may similar to this issue https://github.com/python-pillow/Pillow/issues/6084

from PIL import Image

image = Image.open('435668030-8d16446a-4b29-4a0f-9249-52edfa1532c1.gif')

frames = []
try:
    while True:
        print(len(frames))
        frame = image.copy()
        frames.append(frame)
        image.seek(image.tell() + 1)
except EOFError:
    pass
0
1
2
3
4
5
6
7
Traceback (most recent call last):
  File "/Users/fangzidong/git_repo/mmwrastra/plg/image_io_debug.py", line 13, in <module>
    frame = image.copy()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/Image.py", line 1271, in copy
    self.load()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/ImageFile.py", line 400, in load
    self.load_end()
  File "/Users/fangzidong/miniconda3/envs/torch/lib/python3.10/site-packages/PIL/GifImagePlugin.py", line 480, in load_end
    self.im.putpalettealpha(self._frame_transparency, 0)
ValueError: image has no palette

quaeast avatar Apr 22 '25 02:04 quaeast

I can reproduce, but not sure if this is a Pillow bug.

Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> 
>>> image = Image.open('435668030-8d16446a-4b29-4a0f-9249-52edfa1532c1.gif')
>>> 
>>> frames = []
>>> try:
...     while True:
...         print(len(frames))
...         frame = image.copy()
...         frames.append(frame)
...         image.seek(image.tell() + 1)
... except EOFError:
...     pass
... 
0
1
2
3
4
5
6
7
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pillow-11.2.0-py3.12-macosx-15.3-arm64.egg/PIL/Image.py", line 1271, in copy
    self.load()
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pillow-11.2.0-py3.12-macosx-15.3-arm64.egg/PIL/ImageFile.py", line 396, in load
    self.load_end()
  File "/Users/alex.clark/Developer/django-mongodb-cli/.venv/lib/python3.12/site-packages/pillow-11.2.0-py3.12-macosx-15.3-arm64.egg/PIL/GifImagePlugin.py", line 480, in load_end
    self.im.putpalettealpha(self._frame_transparency, 0)
ValueError: image has no palette

aclark4life avatar Apr 22 '25 03:04 aclark4life

I've created https://github.com/python-pillow/Pillow/pull/8924 to resolve this.

radarhere avatar Apr 25 '25 02:04 radarhere