Undo previous commit that attempted to make the QR code larger if the resolution was larger because it fucked things
Browse files
app.py
CHANGED
|
@@ -43,25 +43,25 @@ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
|
| 43 |
|
| 44 |
def resize_for_condition_image(input_image: Image.Image, resolution: int, canvas_width: int = None, canvas_height: int = None):
|
| 45 |
input_image = input_image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# Default canvas size (if not provided)
|
| 48 |
-
if canvas_width is None or canvas_height is None:
|
| 49 |
-
raise ValueError("Canvas width and height must be provided for consistent resizing")
|
| 50 |
-
|
| 51 |
-
# Determine desired QR code size as a fraction of canvas height
|
| 52 |
-
desired_qr_height = canvas_height // 2 # QR code height = 1/2 of canvas height
|
| 53 |
-
aspect_ratio = input_image.width / input_image.height
|
| 54 |
-
desired_qr_width = int(desired_qr_height * aspect_ratio)
|
| 55 |
-
|
| 56 |
-
# Resize the QR code
|
| 57 |
-
input_image = input_image.resize((desired_qr_width, desired_qr_height), resample=Image.LANCZOS)
|
| 58 |
-
|
| 59 |
# Create a blank canvas with the specified dimensions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
canvas = Image.new("RGB", (canvas_width, canvas_height), "white")
|
| 61 |
|
| 62 |
-
# Paste the resized QR code
|
| 63 |
-
qr_x = int(canvas_width * (2 / 3)) - (
|
| 64 |
-
qr_y = (canvas_height -
|
| 65 |
canvas.paste(input_image, (qr_x, qr_y))
|
| 66 |
return canvas
|
| 67 |
|
|
|
|
| 43 |
|
| 44 |
def resize_for_condition_image(input_image: Image.Image, resolution: int, canvas_width: int = None, canvas_height: int = None):
|
| 45 |
input_image = input_image.convert("RGB")
|
| 46 |
+
W, H = input_image.size
|
| 47 |
+
k = float(resolution) / min(H, W)
|
| 48 |
+
H *= k
|
| 49 |
+
W *= k
|
| 50 |
+
H = int(round(H / 64.0)) * 64
|
| 51 |
+
W = int(round(W / 64.0)) * 64
|
| 52 |
+
input_image = input_image.resize((W, H), resample=Image.LANCZOS)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Create a blank canvas with the specified dimensions
|
| 55 |
+
if canvas_width is None:
|
| 56 |
+
canvas_width = W
|
| 57 |
+
if canvas_height is None:
|
| 58 |
+
canvas_height = H
|
| 59 |
+
|
| 60 |
canvas = Image.new("RGB", (canvas_width, canvas_height), "white")
|
| 61 |
|
| 62 |
+
# Paste the resized QR code on the right half of the canvas
|
| 63 |
+
qr_x = int(canvas_width * (2 / 3)) - (W // 2) # 2/3rd of the canvas width, centering the QR code
|
| 64 |
+
qr_y = (canvas_height - H) // 2 # Center the QR code vertically
|
| 65 |
canvas.paste(input_image, (qr_x, qr_y))
|
| 66 |
return canvas
|
| 67 |
|