AItool commited on
Commit
225022a
·
verified ·
1 Parent(s): 6f8ddad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -33
app.py CHANGED
@@ -16,45 +16,33 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
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):
 
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
+ # Increase height to create a rectangle
25
+ new_height = int(img.width * (4/3)) # Example: height = width * 4/3
26
+ newimg = Image.new(
27
+ 'RGB',
28
+ (img.width, new_height),
29
+ (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
30
+ )
31
+ padding_top = (new_height - img.height) // 2
32
+ newimg.paste(img, (0, padding_top)) # Center the image vertically
33
+ return newimg
 
 
 
 
34
 
35
  elif padding_type == "left_right":
36
+ # Increase width to create a rectangle
37
+ new_width = int(img.height * (4/3)) # Example: width = height * 4/3
38
+ newimg = Image.new(
39
+ 'RGB',
40
+ (new_width, img.height),
41
+ (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
42
+ )
43
+ padding_left = (new_width - img.width) // 2
44
+ newimg.paste(img, (padding_left, 0)) # Center the image horizontally
45
+ return newimg
 
 
 
 
 
 
 
 
46
 
47
  # Function for cropping and filling the image
48
  def fill_rectangle_cropper1(img):