Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PIL import Image | |
import numpy as np | |
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" | |
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 | |
# Streamlit App | |
st.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" | |
) |