Spaces:
Runtime error
Runtime error
ZachNagengast
commited on
Commit
•
d8c4344
1
Parent(s):
f647b33
Update logic and add font
Browse files- .gitignore +78 -0
- Lato-Regular.ttf +0 -0
- app.py +84 -38
.gitignore
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Python build
|
2 |
+
.eggs/
|
3 |
+
gradio.egg-info
|
4 |
+
dist/
|
5 |
+
*.pyc
|
6 |
+
__pycache__/
|
7 |
+
*.py[cod]
|
8 |
+
*$py.class
|
9 |
+
build/
|
10 |
+
__tmp/*
|
11 |
+
|
12 |
+
# JS build
|
13 |
+
gradio/templates/cdn
|
14 |
+
gradio/templates/frontend
|
15 |
+
|
16 |
+
# Secrets
|
17 |
+
.env
|
18 |
+
|
19 |
+
# Gradio run artifacts
|
20 |
+
*.db
|
21 |
+
*.sqlite3
|
22 |
+
gradio/launches.json
|
23 |
+
flagged/
|
24 |
+
gradio_cached_examples/
|
25 |
+
tmp.zip
|
26 |
+
|
27 |
+
# Tests
|
28 |
+
.coverage
|
29 |
+
coverage.xml
|
30 |
+
test.txt
|
31 |
+
**/snapshots/**/*.png
|
32 |
+
playwright-report/
|
33 |
+
|
34 |
+
# Demos
|
35 |
+
demo/tmp.zip
|
36 |
+
demo/files/*.avi
|
37 |
+
demo/files/*.mp4
|
38 |
+
demo/all_demos/demos/*
|
39 |
+
demo/all_demos/requirements.txt
|
40 |
+
demo/*/config.json
|
41 |
+
demo/annotatedimage_component/*.png
|
42 |
+
|
43 |
+
# Etc
|
44 |
+
.idea/*
|
45 |
+
.DS_Store
|
46 |
+
*.bak
|
47 |
+
workspace.code-workspace
|
48 |
+
*.h5
|
49 |
+
|
50 |
+
# dev containers
|
51 |
+
.pnpm-store/
|
52 |
+
|
53 |
+
# log files
|
54 |
+
.pnpm-debug.log
|
55 |
+
|
56 |
+
# Local virtualenv for devs
|
57 |
+
.venv*
|
58 |
+
|
59 |
+
# FRP
|
60 |
+
gradio/frpc_*
|
61 |
+
.vercel
|
62 |
+
|
63 |
+
# js
|
64 |
+
node_modules
|
65 |
+
public/build/
|
66 |
+
test-results
|
67 |
+
client/js/test.js
|
68 |
+
.config/test.py
|
69 |
+
|
70 |
+
# storybook
|
71 |
+
storybook-static
|
72 |
+
build-storybook.log
|
73 |
+
js/storybook/theme.css
|
74 |
+
|
75 |
+
# playwright
|
76 |
+
.config/playwright
|
77 |
+
!.config/playwright/index.html
|
78 |
+
!.config/playwright/index.ts
|
Lato-Regular.ttf
ADDED
Binary file (75.2 kB). View file
|
|
app.py
CHANGED
@@ -2,52 +2,98 @@ import gradio as gr
|
|
2 |
from PIL import Image, ImageDraw, ImageFont, ImageSequence
|
3 |
import numpy as np
|
4 |
|
5 |
-
def create_grid(
|
6 |
-
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
#
|
|
|
19 |
try:
|
20 |
-
font = ImageFont.truetype("
|
21 |
except IOError:
|
|
|
22 |
font = ImageFont.load_default()
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
grid_img.paste(frame, (x_offset, y_offset))
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
)
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from PIL import Image, ImageDraw, ImageFont, ImageSequence
|
3 |
import numpy as np
|
4 |
|
5 |
+
def create_grid(image_file, grid_x, grid_y, font_size, font_color, position, border_size, border_color):
|
6 |
+
image_file.file.seek(0) # Seek to the beginning of the file
|
7 |
|
8 |
+
# Open the image from bytes
|
9 |
+
img = Image.open(image_file.name)
|
10 |
|
11 |
+
total_frames = img.n_frames
|
12 |
+
selected_frames_count = grid_x * grid_y
|
13 |
|
14 |
+
# Select evenly spaced frames
|
15 |
+
selected_frames_indices = np.linspace(0, total_frames - 1, selected_frames_count).astype(int)
|
16 |
+
selected_frames = [img.seek(i) or img.copy() for i in selected_frames_indices]
|
17 |
|
18 |
+
# Modify frames by adding border and number
|
19 |
+
modified_frames = []
|
20 |
try:
|
21 |
+
font = ImageFont.truetype("Lato-Regular.ttf", font_size)
|
22 |
except IOError:
|
23 |
+
print("Font not found, using default font.")
|
24 |
font = ImageFont.load_default()
|
25 |
|
26 |
+
positions = {
|
27 |
+
"Top Left": (20, 20),
|
28 |
+
"Top Right": (img.width - 20 - font_size, 20),
|
29 |
+
"Bottom Left": (20, img.height - 20 - font_size),
|
30 |
+
"Bottom Right": (img.width - 20 - font_size, img.height - 20 - font_size)
|
31 |
+
}
|
32 |
+
|
33 |
+
for i, frame in enumerate(selected_frames):
|
34 |
+
# Add border
|
35 |
+
border_width = border_size
|
36 |
+
frame_with_border = Image.new('RGB', (frame.width + 2*border_width, frame.height + 2*border_width), border_color.lower())
|
37 |
+
frame_with_border.paste(frame, (border_width, border_width))
|
38 |
+
|
39 |
+
# Add number
|
40 |
+
draw = ImageDraw.Draw(frame_with_border)
|
41 |
+
text = str(i + 1)
|
42 |
+
text_position = (border_width + positions[position][0], border_width + positions[position][1])
|
43 |
+
draw.text(text_position, text, font=font, fill=font_color)
|
44 |
+
|
45 |
+
modified_frames.append(frame_with_border)
|
46 |
|
47 |
+
# Combine modified frames into a grid
|
48 |
+
grid_width = modified_frames[0].width * grid_x
|
49 |
+
grid_height = modified_frames[0].height * grid_y
|
50 |
+
grid_img = Image.new('RGB', (grid_width, grid_height), border_color.lower())
|
51 |
+
for i, frame in enumerate(modified_frames):
|
52 |
+
x_offset = (i % grid_x) * frame.width
|
53 |
+
y_offset = (i // grid_x) * frame.height
|
54 |
grid_img.paste(frame, (x_offset, y_offset))
|
55 |
|
56 |
+
output_info = f"Grid size: {grid_x} x {grid_y}\n\nSelected Frames: {selected_frames_count} / {total_frames} ({selected_frames_count / total_frames * 100:.2f}%)"
|
57 |
+
return grid_img, output_info
|
58 |
+
def gif_info(image_file, grid_x, grid_y, font_size, font_color, position, border_size, border_color):
|
59 |
+
image_file.file.seek(0)
|
60 |
+
img = Image.open(image_file.name)
|
61 |
+
total_frames = img.n_frames
|
62 |
+
frame_rate = img.info.get('duration', 100) / 1000.0 # Convert to seconds
|
63 |
+
|
64 |
+
# Generate grid using create_grid function
|
65 |
+
grid_img, output_info = create_grid(image_file, grid_x, grid_y, font_size, font_color, position, border_size, border_color)
|
66 |
+
|
67 |
+
gif_details = f"![image](/file={image_file.name})\n\n**Total Frames:** {total_frames}\n\n**Frame Rate:** {frame_rate} sec/frame\n\n{output_info}"
|
68 |
+
|
69 |
+
return grid_img, gif_details
|
70 |
+
|
71 |
+
with gr.Blocks() as app:
|
72 |
+
gr.Markdown('## GIF Grid Generator')
|
73 |
+
gr.Markdown('Upload a GIF to generate a grid from its frames. Use the sliders to adjust the grid size and text settings.')
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
control_image = gr.File(label="Upload a GIF", type="file", elem_id="file_upload", file_types=[".gif"])
|
77 |
+
gif_details = gr.Markdown("Upload a gif")
|
78 |
+
grid_x_slider = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Grid X Size")
|
79 |
+
grid_y_slider = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Grid Y Size")
|
80 |
+
font_color_dropdown = gr.Dropdown(choices=["Black", "White", "Red", "Green", "Blue"], value="White", label="Numbering Color")
|
81 |
+
position_radio = gr.Radio(choices=["Top Left", "Top Right", "Bottom Left", "Bottom Right"], value="Top Left", label="Numbering Position")
|
82 |
+
font_size_slider = gr.Slider(minimum=10, maximum=100, step=5, value=40, label="Font Size")
|
83 |
+
border_color_dropdown = gr.Dropdown(choices=["Black", "White", "Red", "Green", "Blue"], value="White", label="Border Color")
|
84 |
+
border_size_slider = gr.Slider(minimum=0, maximum=100, step=5, value=10, label="Border Size")
|
85 |
+
with gr.Column():
|
86 |
+
result_image = gr.Image(label="Generated Grid")
|
87 |
+
|
88 |
+
# Use .change() method to listen for changes in any of the controls
|
89 |
+
control_image.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
90 |
+
grid_x_slider.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
91 |
+
grid_y_slider.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
92 |
+
font_size_slider.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
93 |
+
font_color_dropdown.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
94 |
+
position_radio.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
95 |
+
border_size_slider.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
96 |
+
border_color_dropdown.change(gif_info, inputs=[control_image, grid_x_slider, grid_y_slider, font_size_slider, font_color_dropdown, position_radio, border_size_slider, border_color_dropdown], outputs=[result_image, gif_details])
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
app.launch()
|