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

Animated GIF image cropping #215

Open
urtzai opened this issue Jun 25, 2021 · 2 comments
Open

Animated GIF image cropping #215

urtzai opened this issue Jun 25, 2021 · 2 comments

Comments

@urtzai
Copy link
Contributor

urtzai commented Jun 25, 2021

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
Copy link
Owner

richardbarran commented Jun 27, 2021

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
Copy link
Contributor

erdnaxe commented Oct 15, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants