File size: 1,565 Bytes
ce8ee9d
5d892f6
ce8ee9d
5d892f6
ce8ee9d
 
 
 
5d892f6
ce8ee9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d892f6
ce8ee9d
 
 
5d892f6
ce8ee9d
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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"
        )