Spaces:
Sleeping
Sleeping
Update app.py
Browse files12124567891011121314151617181920212223242526272829303132333435363738394041423444454647567894950515253545556575859606162636465666768101112131415161718192021222324252627282930313233343570717273743637383976404142434445464748495051import gradio as grimport numpy as npimport streamlit as stfrom 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 mode pixels = np.array(image) height, width, _ = pixels.shape rgb565_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")import numpy as np
# Function to process GIF and return the .c filedef process_gif(gif_file): gif = Image.open(gif_file) frames = []def gif_to_c_array(gif_path, output_c_file): gif = Image.open(gif_path) frame_count = 0 c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n" frame_index = 0 while True: rgb565_data, width, height = convert_to_rgb565(gif) frames.append(rgb565_data) frame_index += 1
try: gif.seek(gif.tell() + 1) # Move to next frame except EOFError: break
output_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() 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 += 1 gif.seek(gif.tell() + 1) except EOFError: pass c_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
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.")# Streamlit Appst.title("GIF to C Converter for LilyGO T-Display S3")st.write("Upload a GIF to convert it to a .c file.")
iface.launch()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