Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,28 @@
|
|
1 |
-
import
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
def gif_to_c_array(gif_path, output_c_file):
|
|
|
6 |
gif = Image.open(gif_path)
|
|
|
|
|
7 |
frame_count = 0
|
8 |
c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n"
|
9 |
|
|
|
10 |
try:
|
11 |
while True:
|
|
|
12 |
frame = gif.convert("RGB")
|
|
|
|
|
13 |
frame = frame.resize((240, 135))
|
|
|
|
|
14 |
frame_array = np.array(frame)
|
15 |
|
|
|
16 |
for y in range(frame_array.shape[0]):
|
17 |
for x in range(frame_array.shape[1]):
|
18 |
r, g, b = frame_array[y, x]
|
@@ -21,31 +31,32 @@ def gif_to_c_array(gif_path, output_c_file):
|
|
21 |
c_code += "\n"
|
22 |
|
23 |
frame_count += 1
|
24 |
-
gif.seek(gif.tell() + 1)
|
25 |
except EOFError:
|
26 |
-
pass
|
27 |
|
28 |
c_code += "};\n"
|
29 |
c_code += f"const uint16_t {output_c_file.split('.')[0]}_frame_count = {frame_count};\n"
|
30 |
|
|
|
31 |
with open(output_c_file, "w") as f:
|
32 |
f.write(c_code)
|
33 |
|
34 |
return output_c_file
|
35 |
|
36 |
-
|
37 |
-
st.title("GIF to C Converter for LilyGO T-Display S3")
|
38 |
-
st.write("Upload a GIF to convert it to a .c file.")
|
39 |
-
|
40 |
-
uploaded_file = st.file_uploader("Upload GIF", type=["gif"])
|
41 |
-
if uploaded_file is not None:
|
42 |
output_file = "output.c"
|
43 |
-
c_file_path = gif_to_c_array(
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
def gif_to_c_array(gif_path, output_c_file):
|
6 |
+
# Open the GIF image
|
7 |
gif = Image.open(gif_path)
|
8 |
+
|
9 |
+
# Initialize variables
|
10 |
frame_count = 0
|
11 |
c_code = f"const uint16_t {output_c_file.split('.')[0]}[] PROGMEM = {{\n"
|
12 |
|
13 |
+
# Iterate through each frame in the GIF
|
14 |
try:
|
15 |
while True:
|
16 |
+
# Convert the frame to RGB
|
17 |
frame = gif.convert("RGB")
|
18 |
+
|
19 |
+
# Resize the frame to match the display resolution (e.g., 240x135 for LilyGO T-Display S3)
|
20 |
frame = frame.resize((240, 135))
|
21 |
+
|
22 |
+
# Convert the frame to a numpy array
|
23 |
frame_array = np.array(frame)
|
24 |
|
25 |
+
# Convert RGB to 16-bit color (RGB565)
|
26 |
for y in range(frame_array.shape[0]):
|
27 |
for x in range(frame_array.shape[1]):
|
28 |
r, g, b = frame_array[y, x]
|
|
|
31 |
c_code += "\n"
|
32 |
|
33 |
frame_count += 1
|
34 |
+
gif.seek(gif.tell() + 1) # Move to the next frame
|
35 |
except EOFError:
|
36 |
+
pass # End of GIF
|
37 |
|
38 |
c_code += "};\n"
|
39 |
c_code += f"const uint16_t {output_c_file.split('.')[0]}_frame_count = {frame_count};\n"
|
40 |
|
41 |
+
# Write the C code to a file
|
42 |
with open(output_c_file, "w") as f:
|
43 |
f.write(c_code)
|
44 |
|
45 |
return output_c_file
|
46 |
|
47 |
+
def process_gif(gif_file):
|
|
|
|
|
|
|
|
|
|
|
48 |
output_file = "output.c"
|
49 |
+
c_file_path = gif_to_c_array(gif_file, output_file)
|
50 |
+
return c_file_path
|
51 |
+
|
52 |
+
# Gradio Interface
|
53 |
+
interface = gr.Interface(
|
54 |
+
fn=process_gif,
|
55 |
+
inputs=gr.File(label="Upload GIF"),
|
56 |
+
outputs=gr.File(label="Download .c File"),
|
57 |
+
title="GIF to C Converter for LilyGO T-Display S3",
|
58 |
+
description="Upload a GIF to convert it to a .c file for use on LilyGO T-Display S3."
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
interface.launch()
|