Created
June 23, 2023 08:34
-
-
Save devxpy/9a9ea5edca72d79fd14d201e05d0af8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from PIL import Image | |
from diffusers import StableDiffusionPipeline, StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler | |
from diffusers.utils import load_image | |
controlnet = ControlNetModel.from_pretrained("DionTimmer/controlnet_qrcode-control_v11p_sd21", | |
torch_dtype=torch.float16) | |
model_id = "stabilityai/stable-diffusion-2-1" | |
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained( | |
model_id, | |
controlnet=controlnet, | |
safety_checker=None, | |
torch_dtype=torch.float16 | |
) | |
pipe.enable_xformers_memory_efficient_attention() | |
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config) | |
pipe.to("cuda") | |
sd_pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None) | |
sd_pipe.enable_xformers_memory_efficient_attention() | |
sd_pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config) | |
sd_pipe.to("cuda") | |
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 | |
# play with guidance_scale, controlnet_conditioning_scale and strength to make a valid QR Code Image | |
# qr code image | |
source_image = load_image( | |
"https://s3.amazonaws.com/moonup/production/uploads/6064e095abd8d3692e3e2ed6/A_RqHaAM6YHBodPLwqtjn.png") | |
# initial image, anything | |
# init_image = load_image("https://s3.amazonaws.com/moonup/production/uploads/noauth/KfMBABpOwIuNolv1pe3qX.jpeg") | |
condition_image = resize_for_condition_image(source_image, 768) | |
prompt = "a bilboard in NYC with a qrcode" | |
negative_prompt = "ugly, disfigured, low quality, blurry, nsfw" | |
generator = torch.manual_seed(123121231) | |
with torch.no_grad(), torch.inference_mode(): | |
init_image = sd_pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, | |
width=768, | |
height=768, | |
guidance_scale=7, | |
# controlnet_conditioning_scale=10.0, | |
generator=generator, | |
# strength=0.9, | |
num_inference_steps=50, | |
).images[0] | |
# init_image = resize_for_condition_image(init_image, 768) | |
pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, | |
image=init_image, | |
control_image=condition_image, | |
width=768, | |
height=768, | |
guidance_scale=7, | |
controlnet_conditioning_scale=2.0, | |
generator=generator, | |
strength=0.9, | |
num_inference_steps=150, | |
).images[0].save("out.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment