File size: 2,952 Bytes
eaa33f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2467a86
eaa33f1
 
 
 
 
 
 
 
 
 
 
 
 
 
2467a86
eaa33f1
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
from PIL import Image
import gradio as gr

def preview_images(files):
    return gr.Gallery(files)

def merge_images(image_paths, direction, b_resize):
    images = [Image.open(p) for p in image_paths]
    if b_resize:
        w, h = images[0].size
        images = [i.resize((w, h)) for i in images]

    widths, heights = zip(*(i.size for i in images))
    if direction == 'horizontal':
        total_width = sum(widths)
        max_height = max(heights)
        new_im = Image.new('RGB', (total_width, max_height))
        x_offset = 0
        for im in images:
            new_im.paste(im, (x_offset, 0))
            x_offset += im.width
    else:  # vertical
        total_height = sum(heights)
        max_width = max(widths)
        new_im = Image.new('RGB', (max_width, total_height))
        y_offset = 0
        for im in images:
            new_im.paste(im, (0, y_offset))
            y_offset += im.height

    return [new_im]

def split_image(image, parts, direction='horizontal'):
    width, height = image.size
    if direction == 'horizontal':
        part_width = width // parts
        return [image.crop((i * part_width, 0, (i + 1) * part_width, height)) for i in range(parts)]
    else:  # vertical
        part_height = height // parts
        return [image.crop((0, i * part_height, width, (i + 1) * part_height)) for i in range(parts)]


css = "#file {height: 10vh;}"
result_image = gr.Gallery(height="45vh", format="png")
with gr.Blocks(css=css) as demo: 
    with gr.Tabs():
        with gr.Tab(label="Split") as tab_split:
            with gr.Row():
                split_input = gr.Image(type="pil", height="40vh")
                with gr.Column():
                    split_direction = gr.Radio(label="Direction", choices=["vertical", "horizontal"], value="vertical", interactive=True) 
                    split_num = gr.Number(label="Number of parts", value=2, interactive=True)
                    split_btn = gr.Button(scale=1)
            split_btn.click(fn=split_image, inputs=[split_input, split_num, split_direction], outputs=result_image)
        with gr.Tab(label="Merge") as tab_split:
            with gr.Row():
                with gr.Column():
                    merge_files = gr.File(file_count="multiple", elem_id="file")
                    merge_preview = gr.Gallery(label="prevew", height="35vh", format="png")
                with gr.Column():
                    merge_direction = gr.Radio(label="Direction", choices=["vertical", "horizontal"], value="vertical", interactive=True) 
                    merge_b_resize = gr.Checkbox(label="Resize to first image", value=True, interactive=True)
                    merge_btn = gr.Button(scale=1)
            merge_files.change(fn=preview_images, inputs=merge_files, outputs=merge_preview)
            merge_btn.click(fn=merge_images, inputs=[merge_files, merge_direction, merge_b_resize], outputs=result_image)
    result_image.render()
    
demo.launch()