Views
No views yet

pip -q install diffusers transformers accelerate torch xformers1import torch
2from PIL import Image
3from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler
4from diffusers.utils import load_image
5
6controlnet = ControlNetModel.from_pretrained("DionTimmer/controlnet_qrcode-control_v11p_sd21",
7 torch_dtype=torch.float16)
8
9pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
10 "stabilityai/stable-diffusion-2-1",
11 controlnet=controlnet,
12 safety_checker=None,
13 torch_dtype=torch.float16
14)
15
16pipe.enable_xformers_memory_efficient_attention()
17pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
18pipe.enable_model_cpu_offload()
19
20def resize_for_condition_image(input_image: Image, resolution: int):
21 input_image = input_image.convert("RGB")
22 W, H = input_image.size
23 k = float(resolution) / min(H, W)
24 H *= k
25 W *= k
26 H = int(round(H / 64.0)) * 64
27 W = int(round(W / 64.0)) * 64
28 img = input_image.resize((W, H), resample=Image.LANCZOS)
29 return img
30
31
32# play with guidance_scale, controlnet_conditioning_scale and strength to make a valid QR Code Image
33
34# qr code image
35source_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/6064e095abd8d3692e3e2ed6/A_RqHaAM6YHBodPLwqtjn.png")
36# initial image, anything
37init_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/noauth/KfMBABpOwIuNolv1pe3qX.jpeg")
38condition_image = resize_for_condition_image(source_image, 768)
39init_image = resize_for_condition_image(init_image, 768)
40generator = torch.manual_seed(123121231)
41image = pipe(prompt="a bilboard in NYC with a qrcode",
42 negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
43 image=init_image,
44 control_image=condition_image,
45 width=768,
46 height=768,
47 guidance_scale=20,
48 controlnet_conditioning_scale=1.5,
49 generator=generator,
50 strength=0.9,
51 num_inference_steps=150,
52 )
53
54image.images[0]
55