dramp77 commited on
Commit
9282482
1 Parent(s): 3973a71

try to change the image size

Browse files
Files changed (1) hide show
  1. app.py +21 -15
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
- # 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")
 
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")