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

Support for animated formats (gif/webp/apng) #628

Merged
merged 11 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions easy_thumbnails/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ def _points_table():
yield j


def _call_pil_method(im, method, *args, **kwargs):
benzkji marked this conversation as resolved.
Show resolved Hide resolved
"""
call a method on the provided PIL image
if im.n_frames > 1 (image with multiple images, like GIF or WEBP)
call the method on all frames
"""
n_frames = getattr(im, "n_frames", 1)
method = getattr(im, method, None)
if not method:
return None
if n_frames <= 1:
return method(*args, **kwargs)
index = 0
while index < im.n_frames:
im.seek(index)
temp = method(*args, **kwargs)
im.paste(temp)
index += 1
return im


def colorspace(im, bw=False, replace_alpha=False, **kwargs):
"""
Convert images to the correct color space.
Expand All @@ -57,7 +78,8 @@ def colorspace(im, bw=False, replace_alpha=False, **kwargs):
if im.mode == 'I':
# PIL (and pillow) have can't convert 16 bit grayscale images to lower
# modes, so manually convert them to an 8 bit grayscale.
im = im.point(list(_points_table()), 'L')
# im = im.point(list(_points_table()), "L")
im = _call_pil_method(im, "point", list(_points_table()), "L")

is_transparent = utils.is_transparent(im)
is_grayscale = im.mode in ('L', 'LA')
Expand All @@ -78,8 +100,8 @@ def colorspace(im, bw=False, replace_alpha=False, **kwargs):
new_mode = new_mode + 'A'

if im.mode != new_mode:
im = im.convert(new_mode)

# im = im.convert(new_mode)
im = _call_pil_method(im, "convert", new_mode)
return im


Expand Down Expand Up @@ -108,7 +130,8 @@ def autocrop(im, autocrop=False, **kwargs):
bg = Image.new('L', im.size, 255)
bbox = ImageChops.difference(bw, bg).getbbox()
if bbox:
im = im.crop(bbox)
# im = im.crop(bbox)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove commented code

im = _call_pil_method(im, "crop", bbox)
return im


Expand Down Expand Up @@ -202,9 +225,15 @@ def scale_and_crop(im, size, crop=False, upscale=False, zoom=None, target=None,
if scale < 1.0 or (scale > 1.0 and upscale):
# Resize the image to the target size boundary. Round the scaled
# boundary sizes to avoid floating point errors.
im = im.resize((int(round(source_x * scale)),
int(round(source_y * scale))),
resample=Image__Resampling__LANCZOS)
# im = im.resize((int(round(source_x * scale)),
# int(round(source_y * scale))),
# resample=Image__Resampling__LANCZOS)
im = _call_pil_method(
im,
"resize",
(int(round(source_x * scale)), int(round(source_y * scale))),
resample=Image__Resampling__LANCZOS,
)

if crop:
# Use integer values now.
Expand Down Expand Up @@ -274,7 +303,8 @@ def scale_and_crop(im, size, crop=False, upscale=False, zoom=None, target=None,
diff_y = diff_y - add - remove
box = (left, top, right, bottom)
# Finally, crop the image!
im = im.crop(box)
# im = im.crop(box)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detto

im = _call_pil_method(im, "crop", box)
return im


Expand All @@ -291,9 +321,11 @@ def filters(im, detail=False, sharpen=False, **kwargs):

"""
if detail:
im = im.filter(ImageFilter.DETAIL)
# im = im.filter(ImageFilter.DETAIL)
benzkji marked this conversation as resolved.
Show resolved Hide resolved
im = _call_pil_method(im, "filter", ImageFilter.DETAIL)
if sharpen:
im = im.filter(ImageFilter.SHARPEN)
# im = im.filter(ImageFilter.SHARPEN)
im = _call_pil_method(im, "filter", ImageFilter.SHARPEN)
return im


Expand Down Expand Up @@ -321,5 +353,6 @@ def background(im, size, background=None, **kwargs):
if new_im.mode != im.mode:
new_im = new_im.convert(im.mode)
offset = (size[0]-x)//2, (size[1]-y)//2
# animated GIF support must manually be added, here.
new_im.paste(im, offset)
return new_im
Binary file added easy_thumbnails/tests/files/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions easy_thumbnails/tests/test_animated_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from PIL import Image, ImageChops, ImageDraw
from PIL.GifImagePlugin import GifImageFile
from easy_thumbnails import processors
from unittest import TestCase


class AnimatedGIFProcessorsTests(TestCase):

demo_gif = 'easy_thumbnails/tests/files/demo.gif'

def test_scale(self):
with Image.open(self.demo_gif) as im:
frames = im.n_frames
print(frames)
processed = processors.scale_and_crop(im, (100, 100))
processed_frames = processed.n_frames
self.assertEqual(frames, processed_frames)
print(processed.size)
self.assertEqual(processed.size, (100, 75))