Besimplestudio commited on
Commit
5d892f6
·
verified ·
1 Parent(s): 88d34b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()