AItool commited on
Commit
56a1f7e
·
verified ·
1 Parent(s): fa65478

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -7
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.staticfiles import StaticFiles
4
  import numpy as np
@@ -7,14 +7,49 @@ from io import BytesIO
7
  import requests
8
  import base64
9
  import os
 
10
 
11
  app = FastAPI()
12
 
13
  # Mount the static folder for CSS and other assets
14
  app.mount("/static", StaticFiles(directory="static"), name="static")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Function for cropping and filling the image
17
- def fill_rectangle_cropper(img):
18
  imgsz = [img.height, img.width]
19
  avg_color_per_row = np.average(img, axis=0)
20
  avg_color = np.average(avg_color_per_row, axis=0)
@@ -42,7 +77,16 @@ def fill_rectangle_cropper(img):
42
  return newimg
43
  else:
44
  return img
45
-
 
 
 
 
 
 
 
 
 
46
  # Home Page
47
  @app.get("/", response_class=HTMLResponse)
48
  def home_page():
@@ -111,7 +155,7 @@ def demo_page():
111
  # Process the first image
112
  response = requests.get(url1)
113
  img1 = Image.open(BytesIO(response.content)).convert("RGB")
114
- rectangled_img1 = fill_rectangle_cropper(img1)
115
  output1 = BytesIO()
116
  rectangled_img1.save(output1, format="JPEG")
117
  encoded_img1 = base64.b64encode(output1.getvalue()).decode("utf-8")
@@ -119,7 +163,7 @@ def demo_page():
119
  # Process the second image
120
  response = requests.get(url2)
121
  img2 = Image.open(BytesIO(response.content)).convert("RGB")
122
- rectangled_img2 = fill_rectangle_cropper(img2)
123
  output2 = BytesIO()
124
  rectangled_img2.save(output2, format="JPEG")
125
  encoded_img2 = base64.b64encode(output2.getvalue()).decode("utf-8")
@@ -157,8 +201,16 @@ def application_page():
157
  <h2>Rectangle Image Application</h2>
158
  <p>Upload a JPG image to rectangle and fill with color filler.</p>
159
  <form action="/upload/" enctype="multipart/form-data" method="post">
160
- <input name="file" type="file">
161
- <input type="submit" value="Rectangle It">
 
 
 
 
 
 
 
 
162
  </form>
163
  <a href="/">Back</a>
164
  </body>
 
1
+ from fastapi import FastAPI, File, UploadFilek, Form
2
  from fastapi.responses import HTMLResponse
3
  from fastapi.staticfiles import StaticFiles
4
  import numpy as np
 
7
  import requests
8
  import base64
9
  import os
10
+ from tkinter import Tk, Label, Button, Radiobutton, IntVar
11
 
12
  app = FastAPI()
13
 
14
  # Mount the static folder for CSS and other assets
15
  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(img, axis=0)
21
+ avg_color = np.average(avg_color_per_row, axis=0)
22
+
23
+ if padding_type == "top_bottom":
24
+ if img.width > img.height:
25
+ new_height = img.width
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))
33
+ return newimg
34
+ else:
35
+ return img # No change for already square images
36
+
37
+ elif padding_type == "left_right":
38
+ if img.height > img.width:
39
+ new_width = img.height
40
+ newimg = Image.new(
41
+ 'RGB',
42
+ (new_width, img.height),
43
+ (round(avg_color[0]), round(avg_color[1]), round(avg_color[2]))
44
+ )
45
+ padding_left = (new_width - img.width) // 2
46
+ newimg.paste(img, (padding_left, 0))
47
+ return newimg
48
+ else:
49
+ return img # No change for already square images
50
+
51
  # Function for cropping and filling the image
52
+ def fill_rectangle_cropper1(img,padding):
53
  imgsz = [img.height, img.width]
54
  avg_color_per_row = np.average(img, axis=0)
55
  avg_color = np.average(avg_color_per_row, axis=0)
 
77
  return newimg
78
  else:
79
  return img
80
+
81
+ def on_submit():
82
+ padding_choice = padding_option.get()
83
+ if padding_choice == 1:
84
+ result_img = fill_rectangle_cropper(img, "top_bottom")
85
+ elif padding_choice == 2:
86
+ result_img = fill_rectangle_cropper(img, "left_right")
87
+ # Here you can save or display result_img
88
+ print("Padding applied based on selection!")
89
+
90
  # Home Page
91
  @app.get("/", response_class=HTMLResponse)
92
  def home_page():
 
155
  # Process the first image
156
  response = requests.get(url1)
157
  img1 = Image.open(BytesIO(response.content)).convert("RGB")
158
+ rectangled_img1 = fill_rectangle_cropper(img1, "top_bottom")
159
  output1 = BytesIO()
160
  rectangled_img1.save(output1, format="JPEG")
161
  encoded_img1 = base64.b64encode(output1.getvalue()).decode("utf-8")
 
163
  # Process the second image
164
  response = requests.get(url2)
165
  img2 = Image.open(BytesIO(response.content)).convert("RGB")
166
+ rectangled_img2 = fill_rectangle_cropper(img2, "left_right")
167
  output2 = BytesIO()
168
  rectangled_img2.save(output2, format="JPEG")
169
  encoded_img2 = base64.b64encode(output2.getvalue()).decode("utf-8")
 
201
  <h2>Rectangle Image Application</h2>
202
  <p>Upload a JPG image to rectangle and fill with color filler.</p>
203
  <form action="/upload/" enctype="multipart/form-data" method="post">
204
+ <label for="file">Upload your image:</label>
205
+ <input name="file" type="file" required><br><br>
206
+
207
+ <label>Choose the padding direction:</label><br>
208
+ <input type="radio" id="top_bottom" name="padding_type" value="top_bottom" checked>
209
+ <label for="top_bottom">Top/Bottom</label><br>
210
+ <input type="radio" id="left_right" name="padding_type" value="left_right">
211
+ <label for="left_right">Left/Right</label><br><br>
212
+
213
+ <input type="submit" value="Rectangle It">
214
  </form>
215
  <a href="/">Back</a>
216
  </body>