AItool commited on
Commit
af17c05
·
verified ·
1 Parent(s): e2ef6e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -14,7 +14,13 @@ app = FastAPI()
14
  app.mount("/static", StaticFiles(directory="static"), name="static")
15
 
16
  # Function to crop to desired dimensions while keeping ratio
 
 
17
  def cropper(img: Image.Image, target_width: int, target_height: int) -> Image.Image:
 
 
 
 
18
  # Original size
19
  orig_w, orig_h = img.size
20
  target_ratio = target_width / target_height
@@ -33,13 +39,13 @@ def cropper(img: Image.Image, target_width: int, target_height: int) -> Image.Im
33
  img_resized = img.resize((new_w, new_h), Image.LANCZOS)
34
 
35
  # Crop center
36
- left = (new_w - target_width) // 2
37
- top = (new_h - target_height) // 2
38
  right = left + target_width
39
  bottom = top + target_height
40
 
41
  return img_resized.crop((left, top, right, bottom))
42
-
43
  # Home Page
44
  @app.get("/", response_class=HTMLResponse)
45
  def home_page():
@@ -144,8 +150,6 @@ def demo_page():
144
  </body>
145
  </html>
146
  """
147
-
148
- # Application Page
149
  @app.get("/application", response_class=HTMLResponse)
150
  def application_page():
151
  return """
@@ -157,22 +161,24 @@ def application_page():
157
  <body>
158
  <img class="banner" src="/static/images/banner.jpg" alt="Banner" width="100%">
159
  <h2>Rectangle Image Application (CPU Optimized)</h2>
160
- <p>Upload a JPG image to rectangle and fill with color filler. Suitable for youtube shorts, instagram reels and TikTok.</p>
161
  <form action="/upload/" enctype="multipart/form-data" method="post">
162
- <label for="file">Upload your image:</label>
163
- <input name="file" type="file" required><br><br>
164
- <label>Choose the padding direction:</label><br>
165
- <input type="radio" id="top_bottom" name="padding_type" value="top_bottom" checked>
166
- <label for="top_bottom">Top/Bottom</label><br>
167
- <input type="radio" id="left_right" name="padding_type" value="left_right">
168
- <label for="left_right">Left/Right</label><br><br>
169
- <input type="submit" value="Rectangle It">
 
 
170
  </form>
171
  <a href="/">Back</a>
172
  <div id="credit">Image credit
173
- <a href="https://stock.adobe.com/es/contributor/212598146/UMAMI%20LAB" target="_blank">Adobe Stock User Umami Lab</a>
174
- and
175
- <a href="https://www.shutterstock.com/g/Idoia+Lerchundi?rid=430751957" target="_blank">Shutterstock User PhoArt101</a>.
176
  </div>
177
  </body>
178
  </html>
 
14
  app.mount("/static", StaticFiles(directory="static"), name="static")
15
 
16
  # Function to crop to desired dimensions while keeping ratio
17
+ from PIL import Image
18
+
19
  def cropper(img: Image.Image, target_width: int, target_height: int) -> Image.Image:
20
+ # Clamp target dimensions to a maximum of 1600px
21
+ target_width = min(target_width, 1600)
22
+ target_height = min(target_height, 1600)
23
+
24
  # Original size
25
  orig_w, orig_h = img.size
26
  target_ratio = target_width / target_height
 
39
  img_resized = img.resize((new_w, new_h), Image.LANCZOS)
40
 
41
  # Crop center
42
+ left = max((new_w - target_width) // 2, 0)
43
+ top = max((new_h - target_height) // 2, 0)
44
  right = left + target_width
45
  bottom = top + target_height
46
 
47
  return img_resized.crop((left, top, right, bottom))
48
+
49
  # Home Page
50
  @app.get("/", response_class=HTMLResponse)
51
  def home_page():
 
150
  </body>
151
  </html>
152
  """
 
 
153
  @app.get("/application", response_class=HTMLResponse)
154
  def application_page():
155
  return """
 
161
  <body>
162
  <img class="banner" src="/static/images/banner.jpg" alt="Banner" width="100%">
163
  <h2>Rectangle Image Application (CPU Optimized)</h2>
164
+ <p>Upload a JPG image and choose your target dimensions. The image will be cropped to fit while keeping its ratio. Max 1024px per side.</p>
165
  <form action="/upload/" enctype="multipart/form-data" method="post">
166
+ <label for="file">Upload your image:</label>
167
+ <input name="file" type="file" required><br><br>
168
+
169
+ <label for="target_width">Target Width (px):</label>
170
+ <input type="number" id="target_width" name="target_width" min="1" max="1600" required><br><br>
171
+
172
+ <label for="target_height">Target Height (px):</label>
173
+ <input type="number" id="target_height" name="target_height" min="1" max="1600" required><br><br>
174
+
175
+ <input type="submit" value="Crop It">
176
  </form>
177
  <a href="/">Back</a>
178
  <div id="credit">Image credit
179
+ <a href="https://stock.adobe.com/es/contributor/212598146/UMAMI%20LAB" target="_blank">Adobe Stock User Umami Lab</a>
180
+ and
181
+ <a href="https://www.shutterstock.com/g/Idoia+Lerchundi?rid=430751957" target="_blank">Shutterstock User PhoArt101</a>.
182
  </div>
183
  </body>
184
  </html>