reedmayhew commited on
Commit
0782bc0
1 Parent(s): 4a66938

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -1
app.py CHANGED
@@ -6,6 +6,20 @@ from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution
6
  import gradio as gr
7
  import spaces
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Function to upscale an image using Swin2SR
10
  def upscale_image(image, model, processor, device):
11
  # Convert the image to RGB format
@@ -30,6 +44,9 @@ def main(image, model_choice, save_as_jpg=True):
30
  # Check if GPU is available and set the device accordingly
31
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
32
 
 
 
 
33
  # Define model paths
34
  model_paths = {
35
  "Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
@@ -72,7 +89,7 @@ interface = gr.Interface(
72
  ],
73
  outputs=gr.File(label="Download Upscaled Image"),
74
  title="Image Upscaler",
75
- description="Upload an image, select a model, upscale it, and download the new image.",
76
  )
77
 
78
  # Launch the interface
 
6
  import gradio as gr
7
  import spaces
8
 
9
+ # Function to resize image to max 2048x2048 while maintaining aspect ratio
10
+ def resize_image(image, max_size=2048):
11
+ width, height = image.size
12
+ if width > max_size or height > max_size:
13
+ aspect_ratio = width / height
14
+ if width > height:
15
+ new_width = max_size
16
+ new_height = int(new_width / aspect_ratio)
17
+ else:
18
+ new_height = max_size
19
+ new_width = int(new_height * aspect_ratio)
20
+ image = image.resize((new_width, new_height), Image.LANCZOS)
21
+ return image
22
+
23
  # Function to upscale an image using Swin2SR
24
  def upscale_image(image, model, processor, device):
25
  # Convert the image to RGB format
 
44
  # Check if GPU is available and set the device accordingly
45
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
46
 
47
+ # Resize the input image
48
+ image = resize_image(image)
49
+
50
  # Define model paths
51
  model_paths = {
52
  "Pixel Perfect": "caidas/swin2SR-classical-sr-x4-64",
 
89
  ],
90
  outputs=gr.File(label="Download Upscaled Image"),
91
  title="Image Upscaler",
92
+ description="Upload an image, select a model, upscale it, and download the new image. Images larger than 2048x2048 will be resized while maintaining aspect ratio.",
93
  )
94
 
95
  # Launch the interface