dginovker commited on
Commit
3852566
·
verified ·
1 Parent(s): 597eac8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -41,7 +41,7 @@ pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
41
  # pipe.enable_xformers_memory_efficient_attention()
42
 
43
 
44
- def resize_for_condition_image(input_image: Image.Image, resolution: int):
45
  input_image = input_image.convert("RGB")
46
  W, H = input_image.size
47
  k = float(resolution) / min(H, W)
@@ -49,8 +49,22 @@ def resize_for_condition_image(input_image: Image.Image, resolution: int):
49
  W *= k
50
  H = int(round(H / 64.0)) * 64
51
  W = int(round(W / 64.0)) * 64
52
- img = input_image.resize((W, H), resample=Image.LANCZOS)
53
- return img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
 
56
  SAMPLER_MAP = {
@@ -100,10 +114,10 @@ def inference(
100
  qr.make(fit=True)
101
 
102
  qrcode_image = qr.make_image(fill_color="black", back_color="white")
103
- qrcode_image = resize_for_condition_image(qrcode_image, 768)
104
  else:
105
  print("Using QR Code Image")
106
- qrcode_image = resize_for_condition_image(qrcode_image, 768)
107
 
108
  # hack due to gradio examples
109
  init_image = qrcode_image
 
41
  # pipe.enable_xformers_memory_efficient_attention()
42
 
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)
 
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 = canvas_width // 2 # Start pasting in the middle of the canvas
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
+
68
 
69
 
70
  SAMPLER_MAP = {
 
114
  qr.make(fit=True)
115
 
116
  qrcode_image = qr.make_image(fill_color="black", back_color="white")
117
+ qrcode_image = resize_for_condition_image(qrcode_image, 768, width, height)
118
  else:
119
  print("Using QR Code Image")
120
+ qrcode_image = resize_for_condition_image(qrcode_image, 768, width, height)
121
 
122
  # hack due to gradio examples
123
  init_image = qrcode_image