Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ import io
|
|
10 |
import os
|
11 |
import warnings
|
12 |
import math
|
|
|
13 |
from IPython.display import display
|
14 |
from PIL import Image
|
15 |
from stability_sdk import client
|
@@ -78,43 +79,75 @@ def img2img( path ,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
|
|
78 |
#####
|
79 |
|
80 |
|
81 |
-
max_pixels = 1048576
|
82 |
-
|
83 |
-
# Open the image and retrieve its width and height
|
84 |
img = Image.open(path)
|
85 |
width, height = img.size
|
86 |
-
|
87 |
-
# Calculate the number of pixels in the image
|
88 |
num_pixels = width * height
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
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 |
else:
|
105 |
-
# Set the new dimensions to the original dimensions of the image
|
106 |
new_width = width
|
107 |
new_height = height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
# Resize the image
|
114 |
-
img = img.resize((new_width, new_height), resample=Image.Resampling.BILINEAR)
|
115 |
-
|
116 |
-
|
117 |
|
|
|
118 |
|
119 |
|
120 |
|
|
|
10 |
import os
|
11 |
import warnings
|
12 |
import math
|
13 |
+
from math import sqrt
|
14 |
from IPython.display import display
|
15 |
from PIL import Image
|
16 |
from stability_sdk import client
|
|
|
79 |
#####
|
80 |
|
81 |
|
82 |
+
# max_pixels = 1048576
|
|
|
|
|
83 |
img = Image.open(path)
|
84 |
width, height = img.size
|
|
|
|
|
85 |
num_pixels = width * height
|
86 |
|
87 |
+
# Calculate the maximum number of pixels allowed
|
88 |
+
max_pixels = 1048576
|
89 |
+
|
90 |
+
# Calculate the new size of the image, making sure that the number of pixels does not exceed the maximum limit
|
91 |
+
if width * height > max_pixels:
|
92 |
+
# Calculate the new width and height of the image
|
93 |
+
ratio = width / height
|
94 |
+
new_width = int(math.sqrt(max_pixels * ratio))
|
95 |
+
new_height = int(math.sqrt(max_pixels / ratio))
|
|
|
|
|
|
|
|
|
|
|
96 |
else:
|
|
|
97 |
new_width = width
|
98 |
new_height = height
|
99 |
+
|
100 |
+
# Make sure that either the width or the height of the resized image is a multiple of 64
|
101 |
+
if new_width % 64 != 0:
|
102 |
+
new_width = ((new_width + 63) // 64) * 64
|
103 |
+
if new_height % 64 != 0:
|
104 |
+
new_height = ((new_height + 63) // 64) * 64
|
105 |
+
|
106 |
+
# Resize the image
|
107 |
+
img = img.resize((new_width, new_height), resample=Image.BILINEAR)
|
108 |
+
|
109 |
+
# Check if the number of pixels in the resized image is within the maximum limit
|
110 |
+
# If not, adjust the width and height of the image to bring the number of pixels within the maximum limit
|
111 |
+
if new_width * new_height > max_pixels:
|
112 |
+
while new_width * new_height > max_pixels:
|
113 |
+
new_width -= 1
|
114 |
+
new_height = int(max_pixels / new_width)
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
|
119 |
+
# Calculate the closest multiple of 64 for each value
|
120 |
+
if new_width % 64 != 0:
|
121 |
+
new_width = (new_width // 64) * 64
|
122 |
+
if new_height % 64 != 0:
|
123 |
+
new_height = (new_height // 64) * 64
|
124 |
+
|
125 |
+
# Make sure that the final values are less than the original values
|
126 |
+
if new_width > 1407:
|
127 |
+
new_width -= 64
|
128 |
+
if new_height > 745:
|
129 |
+
new_height -= 64
|
130 |
+
|
131 |
+
new_height ,new_width
|
132 |
+
# Initialize the values
|
133 |
+
widthz = new_width
|
134 |
+
heightz = new_height
|
135 |
+
|
136 |
+
# Calculate the closest multiple of 64 for each value
|
137 |
+
if widthz % 64 != 0:
|
138 |
+
widthz = (widthz // 64) * 64
|
139 |
+
if heightz % 64 != 0:
|
140 |
+
heightz = (heightz // 64) * 64
|
141 |
+
|
142 |
+
# Make sure that the final values are less than the original values
|
143 |
+
if widthz > 1407:
|
144 |
+
widthz -= 64
|
145 |
+
if heightz > 745:
|
146 |
+
heightz -= 64
|
147 |
+
|
148 |
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
img = img.resize((widthz, heightz), resample=Image.BILINEAR)
|
151 |
|
152 |
|
153 |
|