Spaces:
Runtime error
Runtime error
Commit
·
f647b33
1
Parent(s):
aa85975
Initial setup for grid app
Browse files
app.py
CHANGED
|
@@ -1,7 +1,53 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont, ImageSequence
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
+
def create_grid(image, grid_size=3, frame_count=9):
|
| 6 |
+
img = Image.open(image.name)
|
| 7 |
+
|
| 8 |
+
# Get the total number of frames in the gif
|
| 9 |
+
total_frames = img.n_frames
|
| 10 |
+
|
| 11 |
+
# Calculate the frames to use for the grid
|
| 12 |
+
selected_frames_indices = np.linspace(0, total_frames - 1, frame_count, dtype=int)
|
| 13 |
+
|
| 14 |
+
# Create a blank image to hold the grid
|
| 15 |
+
grid_dim = (img.width * grid_size, img.height * grid_size)
|
| 16 |
+
grid_img = Image.new("RGBA", grid_dim, "white")
|
| 17 |
+
|
| 18 |
+
# Font setup for numbering frames
|
| 19 |
+
try:
|
| 20 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 40)
|
| 21 |
+
except IOError:
|
| 22 |
+
font = ImageFont.load_default()
|
| 23 |
+
|
| 24 |
+
draw = ImageDraw.Draw(grid_img)
|
| 25 |
+
|
| 26 |
+
# Extract frames and place them in the grid
|
| 27 |
+
for i, frame_index in enumerate(selected_frames_indices):
|
| 28 |
+
img.seek(frame_index)
|
| 29 |
+
frame = img.copy()
|
| 30 |
+
x_offset = (i % grid_size) * img.width
|
| 31 |
+
y_offset = (i // grid_size) * img.height
|
| 32 |
+
grid_img.paste(frame, (x_offset, y_offset))
|
| 33 |
+
|
| 34 |
+
# Draw frame number
|
| 35 |
+
text = str(i + 1)
|
| 36 |
+
text_position = (x_offset + 20, y_offset + 20)
|
| 37 |
+
text_color = "black" if np.mean(np.array(frame)[:,:3]) > 128 else "white"
|
| 38 |
+
draw.text(text_position, text, fill=text_color, font=font)
|
| 39 |
+
|
| 40 |
+
return grid_img
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=create_grid,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.inputs.File(label="Upload a GIF"),
|
| 46 |
+
gr.inputs.Number(label="Grid Size", default=3, min_value=1, max_value=10),
|
| 47 |
+
gr.inputs.Number(label="Frame Count", default=9, min_value=1, max_value=50)
|
| 48 |
+
],
|
| 49 |
+
outputs=gr.outputs.Image(label="Generated Grid"),
|
| 50 |
+
live=False
|
| 51 |
+
)
|
| 52 |
|
|
|
|
| 53 |
iface.launch()
|