Last active
August 27, 2022 16:06
-
-
Save htoyryla/7b86f5e4769c59ac9bf86f2802d4da3e to your computer and use it in GitHub Desktop.
script to generate 100 frames transition from init image towards the promp with Stablediffusion
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
from torch import autocast | |
import torch | |
import requests | |
from PIL import Image | |
import sys | |
from image_to_image import StableDiffusionImg2ImgPipeline, preprocess | |
import random | |
# script to generate 100 frames transition from init image towards the prompt | |
# usage: | |
# | |
# python i2is.py prompt init-image output_file [start_step seed] | |
# | |
# needs image_to_image.py from https://github.com/huggingface/diffusers/blob/main/examples/inference/image_to_image.py | |
# load the pipeline | |
device = "cuda" | |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained( | |
"CompVis/stable-diffusion-v1-4", | |
revision="fp16", | |
torch_dtype=torch.float16, | |
use_auth_token=True | |
).to(device) | |
fn = sys.argv[2] | |
ofn = sys.argv[3] | |
if len(sys.argv) > 4: | |
start = int(sys.argv[4]) | |
else: | |
start = 0 | |
if len(sys.argv) > 5: | |
seed = int(sys.argv[5]) | |
else: | |
seed = random.randint(0,63444) | |
steps = 100 | |
print(start, seed) | |
init_image = Image.open(fn).convert("RGB") | |
init_image = init_image.resize((768, 768)) | |
init_image = preprocess(init_image) | |
prompt = sys.argv[1] | |
for n in range(start,steps): | |
beta = n/100 | |
with autocast("cuda"): | |
generator = torch.Generator("cuda").manual_seed(seed) | |
images = pipe(prompt=prompt, init_image=init_image, strength=beta, guidance_scale=7.5, generator=generator)["sample"] | |
images[0].save(ofn+"-"+str(n)+".png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment