I made a python script that takes a GIF, chromakeys each of its frames and saves them in a folder. Those frames now contain transparency and are PNGs. But i don’t know how to make a GIF from transparent frames using python, every time i try the framerate (which should be 30 FPS) is off. At least i think so because if i check the GIF metadata the duration and frame count is correct. But the GIF should be synced to music, but when i play the song under the GIF it doesn’t sync up at all. The frames were derived from a non transparent GIF which was derived from a video. Both the video and non transparent GIF do however sync up with the music.
I will attach a clip of the GIF below as the GIF is too large to upload.
Thank you in advance!
import os
from PIL import Image
def read_frames_from_folder(folder):
# Get a sorted list of frame files
frame_files = sorted([f for f in os.listdir(folder) if f.endswith('.png')])
frames = []
for frame_file in frame_files:
frame_path = os.path.join(folder, frame_file)
frame = Image.open(frame_path)
frames.append(frame)
return frames
def create_gif_from_frames(frames, output_gif_path, fps):
duration = int(1000 / fps)
frames[0].save(output_gif_path, save_all=True, append_images=frames[1:], duration=duration, loop=0, disposal=2)
output_folder = 'frames'
output_gif_path = 'output.gif'
fps = 30
frames = read_frames_from_folder(output_folder)
create_gif_from_frames(frames, output_gif_path, fps)
GIF:
https://streamable.com/vsaig0
Im relatively new to Python so I tried to ask ChatGPT-4o but that did not help as expected. I tried to search for answers online but found nothing, which is why I am asking this question.
user747381341243 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.