Attempt 2 to keep qr code size ration when making high res generations
Browse files
app.py
CHANGED
|
@@ -44,13 +44,7 @@ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
|
| 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 |
-
|
| 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
|
|
@@ -59,8 +53,16 @@ def resize_for_condition_image(input_image: Image.Image, resolution: int, canvas
|
|
| 59 |
|
| 60 |
canvas = Image.new("RGB", (canvas_width, canvas_height), "white")
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
qr_y = (canvas_height - H) // 2 # Center the QR code vertically
|
| 65 |
canvas.paste(input_image, (qr_x, qr_y))
|
| 66 |
return canvas
|
|
|
|
| 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 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Create a blank canvas with the specified dimensions
|
| 49 |
if canvas_width is None:
|
| 50 |
canvas_width = W
|
|
|
|
| 53 |
|
| 54 |
canvas = Image.new("RGB", (canvas_width, canvas_height), "white")
|
| 55 |
|
| 56 |
+
# Determine the relative size of the QR code based on the canvas dimensions
|
| 57 |
+
qr_scale_ratio = 768 / min(1024, 768) # Base ratio (relative to 1024x768)
|
| 58 |
+
qr_target_size = int(min(canvas_width, canvas_height) * qr_scale_ratio)
|
| 59 |
+
|
| 60 |
+
# Resize the QR code to maintain its relative size
|
| 61 |
+
input_image = input_image.resize((qr_target_size, qr_target_size), resample=Image.LANCZOS)
|
| 62 |
+
W, H = input_image.size
|
| 63 |
+
|
| 64 |
+
# Paste the resized QR code 2/3rds of the way over the canvas
|
| 65 |
+
qr_x = int(canvas_width * (2 / 3)) - (W // 2) # Adjust x-coordinate to 2/3 of the canvas
|
| 66 |
qr_y = (canvas_height - H) // 2 # Center the QR code vertically
|
| 67 |
canvas.paste(input_image, (qr_x, qr_y))
|
| 68 |
return canvas
|