Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import gradio as grimport numpy as npfrom PIL import Imageimport os# Convert 24-bit RGB to 16-bit RGB565def rgb_to_rgb565(r, g, b):return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)# Convert image to RGB565 formatdef convert_to_rgb565(image):image = image.convert("RGB") # Ensure image is in RGB modepixels = np.array(image)height, width, _ = pixels.shapergb565_data = []for y in range(height):for x in range(width):r, g, b = pixels[y, x]rgb565_data.append(rgb_to_rgb565(r, g, b))return rgb565_data, width, height# Generate a .c file with RGB565 image datadef generate_c_file(frames, width, height, output_filename="gif_frames.c"):with open(output_filename, "w") as f:f.write("#include <stdint.h>\n\n")f.write(f"const uint16_t gif_width = {width};\n")f.write(f"const uint16_t gif_height = {height};\n\n")for i, frame in enumerate(frames):f.write(f"const uint16_t frame_{i}[] = {{\n")for j, pixel in enumerate(frame):f.write(f"0x{pixel:04X}, ")if (j + 1) % 10 == 0:f.write("\n")f.write("};\n\n")f.write(f"const uint16_t* gif_frames[{len(frames)}] = {{\n")for i in range(len(frames)):f.write(f" frame_{i},\n")f.write("};\n")# Function to process GIF and return the .c filedef process_gif(gif_file):gif = Image.open(gif_file)frames = []frame_index = 0while True:rgb565_data, width, height = convert_to_rgb565(gif)frames.append(rgb565_data)frame_index += 1try:gif.seek(gif.tell() + 1) # Move to next frameexcept EOFError:breakoutput_filename = "/tmp/gif_frames.c"generate_c_file(frames, width, height, output_filename)return output_filename# Gradio Interfacedef gif_to_c_file(gif_file):output_file = process_gif(gif_file)with open(output_file, "r") as file:return file.read()iface = gr.Interface(fn=gif_to_c_file,inputs=gr.File(label="Upload GIF"),outputs="text",title="GIF to C File Converter",description="Upload a GIF to convert it into a C file with RGB565 format for use on LilyGO T-Display S3.")iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import 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]rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)c_code += f"0x{rgb565:04X}, "c_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")
app.py+46-71