Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617182 unmodified lines21222324252627282930313233343536373839404142434445464748495051import streamlit as stfrom PIL import Imageimport numpy as npdef gif_to_c_array(gif_path, output_c_file):gif = Image.open(gif_path)frame_count = 0c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n"try:while True:frame = gif.convert("RGB")frame = frame.resize((240, 135))frame_array = np.array(frame)for y in range(frame_array.shape[0]):for x in range(frame_array.shape[1]):r, g, b = frame_array[y, x]2 unmodified linesc_code += "\n"frame_count += 1gif.seek(gif.tell() + 1)except EOFError:passc_code += "};\n"c_code += f"const uint16_t {output_c_file.split('.')[0]}_frame_count = {frame_count};\n"with open(output_c_file, "w") as f:f.write(c_code)return output_c_file# Streamlit Appst.title("GIF to C Converter for LilyGO T-Display S3")st.write("Upload a GIF to convert it to a .c file.")uploaded_file = st.file_uploader("Upload GIF", type=["gif"])if uploaded_file is not None:output_file = "output.c"c_file_path = gif_to_c_array(uploaded_file, output_file)with open(c_file_path, "rb") as f:st.download_button(label="Download .c File",data=f,file_name=output_file,mime="text/plain")123456789101112131415161718192021222324252627282 unmodified lines3132333435363738394041424344454647484950515253545556575859606162import gradio as grfrom PIL import Imageimport numpy as npdef gif_to_c_array(gif_path, output_c_file):# Open the GIF imagegif = Image.open(gif_path)# Initialize variablesframe_count = 0c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n"# Iterate through each frame in the GIFtry:while True:# Convert the frame to RGBframe = 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 arrayframe_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]2 unmodified linesc_code += "\n"frame_count += 1gif.seek(gif.tell() + 1) # Move to the next frameexcept EOFError:pass # End of GIFc_code += "};\n"c_code += f"const uint16_t {output_c_file.split('.')[0]}_frame_count = {frame_count};\n"# Write the C code to a filewith open(output_c_file, "w") as f:f.write(c_code)return output_c_filedef process_gif(gif_file):output_file = "output.c"c_file_path = gif_to_c_array(gif_file, output_file)return c_file_path# Gradio Interfaceinterface = 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 appinterface.launch()
app.py+29-18