AItool commited on
Commit
9ee679e
·
verified ·
1 Parent(s): 8fcba64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -16,40 +16,46 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
16
 
17
  # Function to add padding
18
  def fill_rectangle_cropper(img, padding_type):
19
- # Calculate the average color of the image
20
  avg_color_per_row = np.average(np.array(img), axis=0)
21
  avg_color = np.average(avg_color_per_row, axis=0)
22
 
23
  if padding_type == "top_bottom":
24
- # Check if padding is needed for top/bottom
25
  if img.height < img.width:
26
- new_height = img.width # Make the height equal to width
27
  newimg = Image.new(
28
  'RGB',
29
  (img.width, new_height),
30
- (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
31
  )
32
  padding_top = (new_height - img.height) // 2
33
- newimg.paste(img, (0, padding_top))
34
  return newimg
35
  else:
36
- return img # No padding needed if height >= width
 
37
 
38
  elif padding_type == "left_right":
39
- # Check if padding is needed for left/right
40
  if img.width < img.height:
41
- new_width = img.height # Make the width equal to height
42
  newimg = Image.new(
43
  'RGB',
44
  (new_width, img.height),
45
- (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
46
  )
47
  padding_left = (new_width - img.width) // 2
48
- newimg.paste(img, (padding_left, 0))
49
  return newimg
50
  else:
51
- return img # No padding needed if width >= height
52
-
 
 
 
 
 
53
  # Function for cropping and filling the image
54
  def fill_rectangle_cropper1(img):
55
  imgsz = [img.height, img.width]
 
16
 
17
  # Function to add padding
18
  def fill_rectangle_cropper(img, padding_type):
19
+ # Ensure the image is correctly converted to a NumPy array for color calculations
20
  avg_color_per_row = np.average(np.array(img), axis=0)
21
  avg_color = np.average(avg_color_per_row, axis=0)
22
 
23
  if padding_type == "top_bottom":
24
+ # Ensure height is less than width before adding padding
25
  if img.height < img.width:
26
+ new_height = img.width # Set new height equal to width
27
  newimg = Image.new(
28
  'RGB',
29
  (img.width, new_height),
30
+ (round(avg_color[0]), round(avg_color[1]), round(avg_color[2])) # Fill with average color
31
  )
32
  padding_top = (new_height - img.height) // 2
33
+ newimg.paste(img, (0, padding_top)) # Center the image vertically
34
  return newimg
35
  else:
36
+ print("No padding needed for top/bottom.") # Debug message
37
+ return img
38
 
39
  elif padding_type == "left_right":
40
+ # Ensure width is less than height before adding padding
41
  if img.width < img.height:
42
+ new_width = img.height # Set new width equal to height
43
  newimg = Image.new(
44
  'RGB',
45
  (new_width, img.height),
46
+ (round(avg_color[0]), round(avg_color[1]), round(avg_color[2])) # Fill with average color
47
  )
48
  padding_left = (new_width - img.width) // 2
49
+ newimg.paste(img, (padding_left, 0)) # Center the image horizontally
50
  return newimg
51
  else:
52
+ print("No padding needed for left/right.") # Debug message
53
+ return img
54
+
55
+ else:
56
+ print("Invalid padding type.") # Debug message
57
+ return img
58
+
59
  # Function for cropping and filling the image
60
  def fill_rectangle_cropper1(img):
61
  imgsz = [img.height, img.width]