richardbarran / django-photologue

A customizable plug-in photo gallery management application for the Django web framework.
BSD 3-Clause "New" or "Revised" License
674 stars 239 forks source link

Animated GIF image cropping #215

Open urtzai opened 3 years ago

urtzai commented 3 years ago

When I try to crop animated GIFs with photologue, it seems that only tooks one frame of the entire animation, converting it in a static image. Cropping images is vital in order to keep layout stable but at the same time, users want to upload animated GIFs as banners, autopromotion purposes and other stuff.

Wikimedia commons can do this up to a certain size of image:

"Due to technical limitations, the limit on Wikimedia Commons is width × height × number of frames ≤ 100 million."

e.g. https://commons.wikimedia.org/wiki/File:Eguzki_eklipsea_timelapsea.gif

Implementing this feature would be awesome but if it is not possible, is there any workaround to avoid this behavior? Thank you!

richardbarran commented 3 years ago

I've never thought of using animated GIFs in Photologue so I'm afraid I am of no help... Maybe someone else has experience in this domain and can help?

erdnaxe commented 2 years ago

To crop animated GIF with PIL, you can crop each frame. I implemented something similar here: https://gitlab.crans.org/bde/nk20/-/blob/master/apps/member/forms.py#L100-127

im = Image.open(image)

# Crop each frame
frames = []
for frame in ImageSequence.Iterator(im):
    # processing such as:
    frame = frame.crop((0, 0, 10, 10))
    frames.append(frame)

# Save as GIF
om = frames.pop(0)  # Get first frame
om.info = im.info  # Copy metadata
image.file = io.BytesIO()
om.save(image.file, "GIF", save_all=True, append_images=frames, loop=0)