try to change the image size
Browse files
app.py
CHANGED
@@ -32,36 +32,42 @@ def get_dominant_color(image):
|
|
32 |
|
33 |
def change_size(image, selected_size, size_align):
|
34 |
target_size = size_options[selected_size]
|
|
|
35 |
|
36 |
-
#
|
37 |
-
image.thumbnail(
|
38 |
-
|
|
|
39 |
dominant_color = get_dominant_color(image)
|
40 |
|
|
|
41 |
new_image = Image.new("RGB", target_size, dominant_color)
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
if size_align == "Left":
|
46 |
x_offset = 0
|
|
|
47 |
elif size_align == "Right":
|
48 |
x_offset = target_size[0] - image.width
|
49 |
-
|
|
|
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 |
-
|
58 |
-
|
59 |
-
|
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")
|
|
|
32 |
|
33 |
def change_size(image, selected_size, size_align):
|
34 |
target_size = size_options[selected_size]
|
35 |
+
new_width, new_height = target_size
|
36 |
|
37 |
+
# Resize the image to maintain aspect ratio and fit within the half-height
|
38 |
+
image.thumbnail((new_width, new_height // 2), PIL.Image.Resampling.LANCZOS)
|
39 |
+
|
40 |
+
# Obtain the dominant color of the image
|
41 |
dominant_color = get_dominant_color(image)
|
42 |
|
43 |
+
# Create a new image with the dominant color as the background
|
44 |
new_image = Image.new("RGB", target_size, dominant_color)
|
45 |
|
46 |
+
# Size alignment for position: left, right, center, bottom, top
|
|
|
47 |
if size_align == "Left":
|
48 |
x_offset = 0
|
49 |
+
y_offset = 0 # Place at the top
|
50 |
elif size_align == "Right":
|
51 |
x_offset = target_size[0] - image.width
|
52 |
+
y_offset = 0 # Place at the top
|
53 |
+
elif size_align == "Top":
|
54 |
x_offset = (target_size[0] - image.width) // 2
|
55 |
+
y_offset = 0 # Place at the top
|
|
|
|
|
|
|
|
|
56 |
elif size_align == "Bottom":
|
57 |
+
x_offset = (target_size[0] - image.width) // 2
|
58 |
+
y_offset = (target_size[1] - image.height) # Place at the bottom
|
59 |
+
else: # Center
|
60 |
+
x_offset = (target_size[0] - image.width) // 2
|
61 |
+
y_offset = 0 # Place at the top
|
62 |
+
|
63 |
+
# Paste the resized image onto the new background
|
64 |
new_image.paste(image, (x_offset, y_offset))
|
65 |
|
66 |
return new_image
|
67 |
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
def gradio_interface():
|
72 |
with gr.Blocks() as demo:
|
73 |
gr.Markdown("# Advanced Image Resizer with Background and Margins")
|