Spaces:
Sleeping
Sleeping
Update app.py
Browse files1123456789101112131415161718192021222324252627282 unmodified lines31323324343526363738394041424344454636373839404147484344454647484950514950515253545556575859606162import streamlit as stimport gradio as grfrom PIL import Imageimport 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]2 unmodified lines c_code += "\n" frame_count += 1 gif.seek(gif.tell() + 1) gif.seek(gif.tell() + 1) # Move to the next frame except EOFError: pass 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
# 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:def process_gif(gif_file): 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" ) 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