Besimplestudio commited on
Commit
ce8ee9d
·
verified ·
1 Parent(s): b41f351

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -71
app.py CHANGED
@@ -1,76 +1,51 @@
1
- import gradio as gr
2
- import numpy as np
3
  from PIL import Image
4
- import os
5
-
6
- # Convert 24-bit RGB to 16-bit RGB565
7
- def rgb_to_rgb565(r, g, b):
8
- return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
9
-
10
- # Convert image to RGB565 format
11
- def convert_to_rgb565(image):
12
- image = image.convert("RGB") # Ensure image is in RGB mode
13
- pixels = np.array(image)
14
- height, width, _ = pixels.shape
15
- rgb565_data = []
16
-
17
- for y in range(height):
18
- for x in range(width):
19
- r, g, b = pixels[y, x]
20
- rgb565_data.append(rgb_to_rgb565(r, g, b))
21
-
22
- return rgb565_data, width, height
23
-
24
- # Generate a .c file with RGB565 image data
25
- def generate_c_file(frames, width, height, output_filename="gif_frames.c"):
26
- with open(output_filename, "w") as f:
27
- f.write("#include <stdint.h>\n\n")
28
- f.write(f"const uint16_t gif_width = {width};\n")
29
- f.write(f"const uint16_t gif_height = {height};\n\n")
30
-
31
- for i, frame in enumerate(frames):
32
- f.write(f"const uint16_t frame_{i}[] = {{\n")
33
- for j, pixel in enumerate(frame):
34
- f.write(f"0x{pixel:04X}, ")
35
- if (j + 1) % 10 == 0:
36
- f.write("\n")
37
- f.write("};\n\n")
38
-
39
- f.write(f"const uint16_t* gif_frames[{len(frames)}] = {{\n")
40
- for i in range(len(frames)):
41
- f.write(f" frame_{i},\n")
42
- f.write("};\n")
43
 
44
- # Function to process GIF and return the .c file
45
- def process_gif(gif_file):
46
- gif = Image.open(gif_file)
47
- frames = []
48
 
49
- frame_index = 0
50
- while True:
51
- rgb565_data, width, height = convert_to_rgb565(gif)
52
- frames.append(rgb565_data)
53
- frame_index += 1
54
-
55
- try:
56
- gif.seek(gif.tell() + 1) # Move to next frame
57
- except EOFError:
58
- break
59
-
60
- output_filename = "/tmp/gif_frames.c"
61
- generate_c_file(frames, width, height, output_filename)
62
- return output_filename
63
-
64
- # Gradio Interface
65
- def gif_to_c_file(gif_file):
66
- output_file = process_gif(gif_file)
67
- with open(output_file, "r") as file:
68
- return file.read()
 
 
 
 
 
69
 
70
- iface = gr.Interface(fn=gif_to_c_file,
71
- inputs=gr.File(label="Upload GIF"),
72
- outputs="text",
73
- title="GIF to C File Converter",
74
- description="Upload a GIF to convert it into a C file with RGB565 format for use on LilyGO T-Display S3.")
75
 
76
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
 
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]
19
+ rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
20
+ c_code += f"0x{rgb565:04X}, "
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
+ # Streamlit App
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(uploaded_file, output_file)
44
+
45
+ with open(c_file_path, "rb") as f:
46
+ st.download_button(
47
+ label="Download .c File",
48
+ data=f,
49
+ file_name=output_file,
50
+ mime="text/plain"
51
+ )