Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GIF with P frame cannot be loaded #1036

Merged
merged 9 commits into from
Sep 19, 2023
9 changes: 8 additions & 1 deletion imageio/plugins/pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Union, cast

import numpy as np
from PIL import ExifTags, Image, ImageSequence, UnidentifiedImageError # type: ignore
from PIL import ExifTags, GifImagePlugin, Image, ImageSequence, UnidentifiedImageError # type: ignore

from ..core.request import URI_BYTES, InitializationError, IOMode, Request
from ..core.v3_plugin_api import ImageProperties, PluginV3
Expand Down Expand Up @@ -210,6 +210,13 @@ def read(
" `mode='L'` for an integer-valued result."
)

if self._image.format == "GIF":
# Converting GIF P frames to RGB
# https://github.com/python-pillow/Pillow/pull/6150
GifImagePlugin.LOADING_STRATEGY = (
GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY
)

if index is None:
if self._image.format == "GIF":
index = Ellipsis
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,17 @@ def test_gif_list_write(test_images, tmp_path):
assert im2.shape == (24, 30, 3)


@pytest.mark.needs_internet
def test_gif_first_p_frame():
# Bugfix: https://github.com/imageio/imageio/issues/1030
im = iio.imread(
"https://upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif",
plugin="pillow",
index=None,
)
assert im.shape == (36, 360, 480, 3)


def test_legacy_exif_orientation(test_images, tmp_path):
from PIL.Image import Exif

Expand Down