File size: 2,157 Bytes
38d647f
1eb15ec
 
7d9bafe
78489aa
1eb15ec
7c1408f
1eb15ec
 
 
7c1408f
 
1eb15ec
 
 
 
 
 
 
 
 
 
646fd2e
 
1eb15ec
 
 
 
 
646fd2e
 
7d9bafe
1eb15ec
646fd2e
 
 
066f283
646fd2e
 
 
1eb15ec
 
 
 
7c1408f
066f283
7c1408f
066f283
 
 
 
9dff307
 
25569a9
7c1408f
 
066f283
 
646fd2e
7c1408f
9dff307
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gradio as gr
import numpy as np
import zipfile
from io import BytesIO
from PIL import Image

def split_image_grid(image, grid_cols, grid_rows):
    # Convert the image to a NumPy array
    img = np.array(image)
    width, height = img.shape[1], img.shape[0]
    grid_width = grid_cols
    grid_height = grid_rows
    # Calculate the size of each grid cell
    cell_width = width // grid_width
    cell_height = height // grid_height

    # Split the image into individual frames
    frames = []
    for i in range(grid_height):
        for j in range(grid_width):
            left = j * cell_width
            upper = i * cell_height
            right = left + cell_width
            lower = upper + cell_height
            frame = img[upper:lower, left:right]
            frames.append(frame)

    return frames

def zip_images(images):
    # Create a BytesIO object to hold the zip file
    zip_buffer = BytesIO()
    with zipfile.ZipFile(zip_buffer, 'w') as zipf:
        for idx, img in enumerate(images):
            # Save each image to the zip file
            img_buffer = BytesIO()
            img = Image.fromarray(img)  # Convert NumPy array to PIL Image
            img.save(img_buffer, format='PNG')
            img_buffer.seek(0)
            zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())

    zip_buffer.seek(0)
    return zip_buffer

def process_image(image, grid_cols_input, grid_rows_input):
    # Split the image into a grid of frames
    frames = split_image_grid(image, grid_cols_input, grid_rows_input)
    # Zip the frames into a single zip file
    zip_file = zip_images(frames)
    return zip_file

with gr.Blocks() as demo:
    with gr.Row():
        image_input = gr.Image(label="Input Image", type="filepath")
        grid_cols_input = gr.Slider(1, 10, value=2, step=1, label="Grid Cols")
        grid_rows_input = gr.Slider(1, 10, value=2, step=1, label="Grid Rows")
    zip_output = gr.File(label="Output Zip File")
    process_button = gr.Button("Process Image")

    process_button.click(process_image, inputs=[image_input, grid_cols_input, grid_rows_input], outputs=zip_output)

demo.launch(show_error=True)