Xhaheen commited on
Commit
ebd8936
1 Parent(s): a57452c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -79,37 +79,38 @@ def img2img( path ,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
79
 
80
 
81
 
82
- # Path to the image
83
- # path = "path/to/image"
84
-
85
- # Maximum number of pixels allowed in the image
 
 
86
  max_pixels = 1048576
87
 
88
  # Open the image and retrieve its width and height
89
  img = Image.open(path)
90
  width, height = img.size
91
  num_pixels = width * height
92
-
93
  # Check if the number of pixels is within the allowed range
94
- if num_pixels > max_pixels:
95
- # Scale down the image to have the maximum allowed number of pixels, while maintaining its aspect ratio
96
- factor = math.sqrt(max_pixels / (width * height))
97
- new_width = int(width * factor)
98
- new_height = int(height * factor)
99
- else:
100
- # Set the new dimensions to the original dimensions of the image
101
- new_width = width
102
- new_height = height
103
 
104
- # Calculate the new size of the image, making sure that the width and height are multiples of 64
105
- new_width = ((new_width + 63) // 64) * 64
106
- new_height = ((new_height + 63) // 64) * 64
 
 
 
 
107
 
108
- # Resize the image
109
- img = img.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
110
 
111
-
112
-
113
 
114
  ########
115
  max_attempts = 5 # maximum number of attempts before giving up
 
79
 
80
 
81
 
82
+
83
+
84
+
85
+
86
+
87
+ # Maximum number of pixels allowed in the image
88
  max_pixels = 1048576
89
 
90
  # Open the image and retrieve its width and height
91
  img = Image.open(path)
92
  width, height = img.size
93
  num_pixels = width * height
 
94
  # Check if the number of pixels is within the allowed range
95
+ if num_pixels <= max_pixels:
96
+ # Calculate the new dimensions of the image, making sure that the width and height are within the allowed range
97
+ # and maintain the aspect ratio of the original image
98
+ if width > height:
99
+ new_width = max_width
100
+ new_height = int(new_width * height / width)
101
+ else:
102
+ new_height = max_height
103
+ new_width = int(new_height * width / height)
104
 
105
+ # Calculate the new size of the image, making sure that the width and height are multiples of 64
106
+ new_width = ((new_width + 63) // 64) * 64
107
+ new_height = ((new_height + 63) // 64) * 64
108
+
109
+ # Resize the image
110
+ img = img.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
111
+
112
 
 
 
113
 
 
 
114
 
115
  ########
116
  max_attempts = 5 # maximum number of attempts before giving up