dramp77 commited on
Commit
6015ee6
·
verified ·
1 Parent(s): 3973a71

Update app.py

Browse files

change 1 to 0 on top offset

Files changed (1) hide show
  1. app.py +82 -82
app.py CHANGED
@@ -1,83 +1,83 @@
1
- import gradio as gr
2
- import PIL
3
- from PIL import Image, ImageOps
4
- from collections import Counter
5
- import numpy as np
6
-
7
- size_options ={"1200 x 1500": (1200, 1500),
8
- "1200 x 1200": (1200, 1200),
9
- "1200 x 628": (1200, 628),
10
- "Meta - Square (Feed)": (1200, 1200),
11
- "Meta - Story": (1080, 1920),
12
- "Tiktok Pangle - 1280 x 720": (1280, 720),
13
- "Tiktok Pangle - 1200 x 628": (1200, 628),
14
- "Tiktok Pangle - 640 x 640": (640, 640),
15
- "Tiktok Pangle - 300 x 250": (300, 250),
16
- "Tiktok Pangle - 720 x 1280": (720, 1280),
17
- "Tiktok Pangle - 300 x 50": (300, 50),
18
- "Tiktok Pangle - 320 x 50": (320, 50),
19
- "Tiktok Pangle - 320 x 480": (320, 480),
20
- "Tiktok Pangle - 250 x 250": (250, 250),
21
- "Tiktok Pangle - 600 x 770": (600, 770),
22
- "Tiktok Pangle - 720 x 1067": (720, 1067),
23
- "Tiktok Pangle - 480 x 320": (480, 320)}
24
-
25
- def get_dominant_color(image):
26
- image = image.resize((50, 50)) # Resize to speed up
27
- pixels = np.array(image)
28
- pixels = pixels.reshape((-1, 3))
29
- counter = Counter(map(tuple, pixels))
30
- dominant_color = counter.most_common(1)[0][0]
31
- return dominant_color
32
-
33
- def change_size(image, selected_size, size_align):
34
- target_size = size_options[selected_size]
35
-
36
- # Maintain aspect ratio without stretching
37
- image.thumbnail(target_size, PIL.Image.Resampling.LANCZOS)
38
-
39
- dominant_color = get_dominant_color(image)
40
-
41
- new_image = Image.new("RGB", target_size, dominant_color)
42
-
43
- # size allignment position left, right, and center
44
-
45
- if size_align == "Left":
46
- x_offset = 0
47
- elif size_align == "Right":
48
- x_offset = target_size[0] - image.width
49
- else:
50
- x_offset = (target_size[0] - image.width) // 2
51
-
52
- # size allignment position top, bottom, and center
53
-
54
- if size_align == "Top":
55
- y_offset = 1
56
- elif size_align == "Bottom":
57
- y_offset = target_size[1] - image.width
58
- else:
59
- y_offset = (target_size[1] - image.width) // 2
60
-
61
- new_image.paste(image, (x_offset, y_offset))
62
-
63
- return new_image
64
-
65
- def gradio_interface():
66
- with gr.Blocks() as demo:
67
- gr.Markdown("# Advanced Image Resizer with Background and Margins")
68
- with gr.Row():
69
- image_input = gr.Image(type="pil", label="Upload Image")
70
- size_input = gr.Dropdown(list(size_options.keys()), label="Size Options")
71
- with gr.Row():
72
- size_alignment = gr.Radio(["Left", "Center", "Right", "Top", "Bottom"], value="Center", label="Size Alignment")
73
- image_output = gr.Image(type="pil", label="Resized Image with Background")
74
- submit_button = gr.Button("Resize Image")
75
-
76
- submit_button.click(fn=change_size,
77
- inputs=[image_input, size_input, size_alignment],
78
- outputs=image_output)
79
-
80
- return demo
81
-
82
- demo = gradio_interface()
83
  demo.launch(share=True)
 
1
+ import gradio as gr
2
+ import PIL
3
+ from PIL import Image, ImageOps
4
+ from collections import Counter
5
+ import numpy as np
6
+
7
+ size_options ={"1200 x 1500": (1200, 1500),
8
+ "1200 x 1200": (1200, 1200),
9
+ "1200 x 628": (1200, 628),
10
+ "Meta - Square (Feed)": (1200, 1200),
11
+ "Meta - Story": (1080, 1920),
12
+ "Tiktok Pangle - 1280 x 720": (1280, 720),
13
+ "Tiktok Pangle - 1200 x 628": (1200, 628),
14
+ "Tiktok Pangle - 640 x 640": (640, 640),
15
+ "Tiktok Pangle - 300 x 250": (300, 250),
16
+ "Tiktok Pangle - 720 x 1280": (720, 1280),
17
+ "Tiktok Pangle - 300 x 50": (300, 50),
18
+ "Tiktok Pangle - 320 x 50": (320, 50),
19
+ "Tiktok Pangle - 320 x 480": (320, 480),
20
+ "Tiktok Pangle - 250 x 250": (250, 250),
21
+ "Tiktok Pangle - 600 x 770": (600, 770),
22
+ "Tiktok Pangle - 720 x 1067": (720, 1067),
23
+ "Tiktok Pangle - 480 x 320": (480, 320)}
24
+
25
+ def get_dominant_color(image):
26
+ image = image.resize((50, 50)) # Resize to speed up
27
+ pixels = np.array(image)
28
+ pixels = pixels.reshape((-1, 3))
29
+ counter = Counter(map(tuple, pixels))
30
+ dominant_color = counter.most_common(1)[0][0]
31
+ return dominant_color
32
+
33
+ def change_size(image, selected_size, size_align):
34
+ target_size = size_options[selected_size]
35
+
36
+ # Maintain aspect ratio without stretching
37
+ image.thumbnail(target_size, PIL.Image.Resampling.LANCZOS)
38
+
39
+ dominant_color = get_dominant_color(image)
40
+
41
+ new_image = Image.new("RGB", target_size, dominant_color)
42
+
43
+ # size allignment position left, right, and center
44
+
45
+ if size_align == "Left":
46
+ x_offset = 0
47
+ elif size_align == "Right":
48
+ x_offset = target_size[0] - image.width
49
+ else:
50
+ x_offset = (target_size[0] - image.width) // 2
51
+
52
+ # size allignment position top, bottom, and center
53
+
54
+ if size_align == "Top":
55
+ y_offset = 0
56
+ elif size_align == "Bottom":
57
+ y_offset = target_size[1] - image.width
58
+ else:
59
+ y_offset = (target_size[1] - image.width) // 2
60
+
61
+ new_image.paste(image, (x_offset, y_offset))
62
+
63
+ return new_image
64
+
65
+ def gradio_interface():
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("# Advanced Image Resizer with Background and Margins")
68
+ with gr.Row():
69
+ image_input = gr.Image(type="pil", label="Upload Image")
70
+ size_input = gr.Dropdown(list(size_options.keys()), label="Size Options")
71
+ with gr.Row():
72
+ size_alignment = gr.Radio(["Left", "Center", "Right", "Top", "Bottom"], value="Center", label="Size Alignment")
73
+ image_output = gr.Image(type="pil", label="Resized Image with Background")
74
+ submit_button = gr.Button("Resize Image")
75
+
76
+ submit_button.click(fn=change_size,
77
+ inputs=[image_input, size_input, size_alignment],
78
+ outputs=image_output)
79
+
80
+ return demo
81
+
82
+ demo = gradio_interface()
83
  demo.launch(share=True)