Skip to content

How to pixelate GIFs using python? #6208

Answered by radarhere
Lypheal asked this question in Q&A
Discussion options

You must be logged in to vote

Running this code over your original image,

from PIL import Image, ImageSequence

def pixelate(frame):
    if frame.mode == "P":
        frame = frame.convert("RGB")
    original_size = frame.size
    # To create the pixelation effect,
    # shrink the image by a factor of 5
    frame = frame.reduce(5)
    # and then resize it back to normal size
    return frame.resize(original_size, Image.Resampling.NEAREST)

# Open the image
with Image.open("in.gif") as im:
    # Run the "pixelate" function over every frame
    frames = ImageSequence.all_frames(im, pixelate)
    # Save it to file
    frames[0].save("out.gif", save_all=True, append_images=frames[1:])

gives

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@Lypheal
Comment options

@radarhere
Comment options

@Lypheal
Comment options

@radarhere
Comment options

@Lypheal
Comment options

Answer selected by Lypheal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants