Spaces:
Sleeping
Sleeping
File size: 1,966 Bytes
43ddd0c 5d892f6 ce8ee9d 5d892f6 ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 5d892f6 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 43ddd0c ce8ee9d 5d892f6 43ddd0c ce8ee9d 43ddd0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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() |