Create your own QR code !!
The topic I’m going to introduce today is creating your unique QR code that exists nowhere else in the world.
Let’s goooooooooooooooo ! 🔥🔥🔥🔥🔥
⭐Step 1:⭐
Let’s quietly (-q) install the required libraries first.
pip install -q qrcode diffusers transformers accelerate xformers
⭐Step 2:⭐
Import the installed libraries.
import qrcode
import torch
from PIL import Image
from diffusers import StableDiffusionPipeline, StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler, DPMSolverMultistepScheduler
from diffusers.utils import load_image
import matplotlib.pyplot as plt
⭐Step 3:⭐
Testing the generation of QR code.
qr = qrcode.QRCode(
version=1, # 1-40
# Low, Medium High (qr code quality)
error_correction=qrcode.constants.ERROR_CORRECT_H, # L, M, H
box_size=17,
border=4,
)
qr.add_data("https://medium.com/@hichengkang")
qr.make()
img = qr.make_image(fill_color="black", back_color="white")
print(img.size)
img.save("./my_qrcode.png")
print("Finish !")
⭐Step 4:⭐
model setting.
# popular
# "runwayml/stable-diffusion-v1-5",
# animate
# "stablediffusionapi/anything-v5",
# others :
# huggingface.com
sd_pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
safety_checker=None,
torch_dtype=torch.float16)
# create new scheduler
sd_pipe.scheduler = DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config, use_karras=True, algorithm_type="sde-dpmsolver++")
# turn off the cpu
sd_pipe.enable_model_cpu_offload()
⭐Step 5:⭐
generation the Image.
# Similarly stable diffusion
image = sd_pipe(
prompt="cyberpunk city",
negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
# set image size
width=768,
height=768,
# set seed
generator=torch.manual_seed(-1),
)
image.images[0].save("init.png")
image.images[0]
⭐Step 6:⭐
Fictionalize Step 1 ~ 5
import qrcode
import torch
from PIL import Image
from diffusers import StableDiffusionPipeline, StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler, DPMSolverMultistepScheduler
from diffusers.utils import load_image
import matplotlib.pyplot as plt
controlnet = ControlNetModel.from_pretrained(
"DionTimmer/controlnet_qrcode-control_v1p_sd15",
torch_dtype=torch.float16)
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
controlnet=controlnet,
safety_checker=None,
torch_dtype=torch.float16)
# pipe.enable_xformers_memory_efficient_attention()
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras=True, algorithm_type="sde-dpmsolver++")
pipe.enable_model_cpu_offload()
resize the image
def resize_for_condition_image(input_image: Image, resolution: int):
input_image = input_image.convert("RGB")
W, H = input_image.size
k = float(resolution) / min(H, W)
H *= k
W *= k
H = int(round(H / 64.0)) * 64
W = int(round(W / 64.0)) * 64
img = input_image.resize((W, H), resample=Image.LANCZOS)
return img
Return the result
def make_qr_art(
url,
init_image,
prompt,
guidance_scale=10,
controlnet_conditioning_scale=1.4,
seed=-1,
strength=0.9,
num_inference_steps=40):
qr = qrcode.QRCode(
version=1, # 1 - 40
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=17,
border=4,
)
qr.add_data(url)
qr.make()
qr_image = qr.make_image(fill_color="black", back_color="white")
qr_image_resized = resize_for_condition_image(qr_image, 768)
init_image_resized = resize_for_condition_image(init_image, 768)
image = pipe(
prompt=prompt,
negative_prompt="ugly, disfigured, low quality, blurry, nsfw",
image=init_image_resized,
control_image=qr_image_resized,
width=768,
height=768,
guidance_scale=guidance_scale, # 20, 7.5
controlnet_conditioning_scale=controlnet_conditioning_scale, # 1.5. 1.3
generator=torch.manual_seed(seed),
strength=strength, # 0.9
num_inference_steps=num_inference_steps, # 150, 40
)
return image.images[0]
⭐Final Step:⭐
Let’s generate the QR code image!!!
image:
Background image
control_image:
QR code
guidance_scale:
How well to follow the prompt
controlnet_conditioning_scale:
How well to preserve the QR code
strength:
How well to preserve the QR code
num_inference_steps:
Quality of the generated image, Diffusion denoising steps”
[ =====>More detail <=====]
make_qr_art(
url="https://medium.com/@hichengkang",
init_image=load_image("init.png"),
prompt="cyberpunk city",
guidance_scale=7.5,
controlnet_conditioning_scale=1.5,
seed=-1,
strength=0.9,
num_inference_steps=50)