Categories

APNG to GIF script

As someone who frequently steal downloads stickers from the Line store and tries to convert them to .gif format to use as personal stickers on WhatsApp, this python script will be helpful for me.

				
					import os
from PIL import Image, ImageSequence

# Set the directory containing the APNG files
apng_dir = r"C:\Users\Valsze\Desktop\animation@2x"

# Loop through all files in the directory
for filename in os.listdir(apng_dir):
    if filename.endswith(".apng"):
        # Construct the full file path
        apng_path = os.path.join(apng_dir, filename)
        
        # Load the APNG file
        try:
            im = Image.open(apng_path)
        except:
            print(f"Error opening {filename}. Skipping.")
            continue
        
        # Get the frame durations
        durations = [frame.info.get("duration", 100) for frame in ImageSequence.Iterator(im)]
        
        # Save the frames as an animated GIF
        gif_path = os.path.join(apng_dir, os.path.splitext(filename)[0] + ".gif")
        frames = []
        try:
            for frame in ImageSequence.Iterator(im):
                frame = frame.copy()
                frames.append(frame)
        except EOFError:
            pass
        
        frames[0].save(gif_path, save_all=True, append_images=frames[1:], disposal=2, loop=0, duration=durations)
        
        print(f"Converted {filename} to {os.path.splitext(filename)[0]}.gif")

print("Conversion complete!")
				
			

Note: You might need ImageMagick installed in order to run this script.