import gradio as gr from PIL import Image import numpy as np def gif_to_c_array(gif_path, output_c_file): # Open the GIF image gif = Image.open(gif_path) # Initialize variables frame_count = 0 c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n" # Iterate through each frame in the GIF try: while True: # Convert the frame to RGB frame = gif.convert("RGB") # Resize the frame to match the display resolution (e.g., 240x135 for LilyGO T-Display S3) frame = frame.resize((240, 135)) # Convert the frame to a numpy array frame_array = np.array(frame) # Convert RGB to 16-bit color (RGB565) for y in range(frame_array.shape[0]): for x in range(frame_array.shape[1]): r, g, b = frame_array[y, x] rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3) c_code += f"0x{rgb565:04X}, " c_code += "\n" frame_count += 1 gif.seek(gif.tell() + 1) # Move to the next frame except EOFError: pass # End of GIF c_code += "};\n" c_code += f"const uint16_t {output_c_file.split('.')[0]}_frame_count = {frame_count};\n" # Write the C code to a file with open(output_c_file, "w") as f: f.write(c_code) return output_c_file def process_gif(gif_file): output_file = "output.c" c_file_path = gif_to_c_array(gif_file, output_file) return c_file_path # Gradio Interface interface = gr.Interface( fn=process_gif, inputs=gr.File(label="Upload GIF"), outputs=gr.File(label="Download .c File"), title="GIF to C Converter for LilyGO T-Display S3", description="Upload a GIF to convert it to a .c file for use on LilyGO T-Display S3." ) # Launch the app interface.launch()