Skip to content

Instantly share code, notes, and snippets.

@htoyryla
Last active August 27, 2022 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save htoyryla/7b86f5e4769c59ac9bf86f2802d4da3e to your computer and use it in GitHub Desktop.
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
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