Eun0 commited on
Commit
eaa33f1
·
1 Parent(s): 5a5f080

First init

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import gradio as gr
3
+
4
+ def preview_images(files):
5
+ return gr.Gallery(files)
6
+
7
+ def merge_images(image_paths, direction, b_resize):
8
+ images = [Image.open(p) for p in image_paths]
9
+ if b_resize:
10
+ w, h = images[0].size
11
+ images = [i.resize((w, h)) for i in images]
12
+
13
+ widths, heights = zip(*(i.size for i in images))
14
+ if direction == 'horizontal':
15
+ total_width = sum(widths)
16
+ max_height = max(heights)
17
+ new_im = Image.new('RGB', (total_width, max_height))
18
+ x_offset = 0
19
+ for im in images:
20
+ new_im.paste(im, (x_offset, 0))
21
+ x_offset += im.width
22
+ else: # vertical
23
+ total_height = sum(heights)
24
+ max_width = max(widths)
25
+ new_im = Image.new('RGB', (max_width, total_height))
26
+ y_offset = 0
27
+ for im in images:
28
+ new_im.paste(im, (0, y_offset))
29
+ y_offset += im.height
30
+
31
+ return [new_im]
32
+
33
+ def split_image(image, parts, direction='horizontal'):
34
+ width, height = image.size
35
+ if direction == 'horizontal':
36
+ part_width = width // parts
37
+ return [image.crop((i * part_width, 0, (i + 1) * part_width, height)) for i in range(parts)]
38
+ else: # vertical
39
+ part_height = height // parts
40
+ return [image.crop((0, i * part_height, width, (i + 1) * part_height)) for i in range(parts)]
41
+
42
+
43
+ css = "#file {height: 10vh;}"
44
+ result_image = gr.Gallery()
45
+ with gr.Blocks(css=css) as demo:
46
+ with gr.Tabs():
47
+ with gr.Tab(label="Split") as tab_split:
48
+ with gr.Row():
49
+ split_input = gr.Image(type="pil", height="40vh")
50
+ with gr.Column():
51
+ split_direction = gr.Radio(label="Direction", choices=["vertical", "horizontal"], value="vertical", interactive=True)
52
+ split_num = gr.Number(label="Number of parts", value=2, interactive=True)
53
+ split_btn = gr.Button(scale=1)
54
+ split_btn.click(fn=split_image, inputs=[split_input, split_num, split_direction], outputs=result_image)
55
+ with gr.Tab(label="Merge") as tab_split:
56
+ with gr.Row():
57
+ with gr.Column():
58
+ merge_files = gr.File(file_count="multiple", elem_id="file")
59
+ merge_preview = gr.Gallery(label="prevew", height="35vh")
60
+ with gr.Column():
61
+ merge_direction = gr.Radio(label="Direction", choices=["vertical", "horizontal"], value="vertical", interactive=True)
62
+ merge_b_resize = gr.Checkbox(label="Resize to first image", value=True, interactive=True)
63
+ merge_btn = gr.Button(scale=1)
64
+ merge_files.change(fn=preview_images, inputs=merge_files, outputs=merge_preview)
65
+ merge_btn.click(fn=merge_images, inputs=[merge_files, merge_direction, merge_b_resize], outputs=result_image)
66
+ result_image.render()
67
+
68
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio