Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active July 29, 2023 19:43
Show Gist options
  • Save twobob/edb40960ab49a7ad6d932201179f0da0 to your computer and use it in GitHub Desktop.
Save twobob/edb40960ab49a7ad6d932201179f0da0 to your computer and use it in GitHub Desktop.
Amendment to the stable diffusion notebook to do titling and generate /n/ images
#Self contain this for easy single clicking
#from PIL import Image
#def image_grid(imgs, rows, cols):
# assert len(imgs) == rows*cols
# w, h = imgs[0].size
# grid = Image.new('RGB', size=(cols*w, rows*h))
# grid_w, grid_h = grid.size
# for i, img in enumerate(imgs):
# grid.paste(img, box=(i%cols*w, i//cols*h))
# return grid
# add a couple of options
!wget -nc "https://github.com/PrincetonUniversity/COS333_Comet/blob/master/android/app/src/main/assets/fonts/Microsoft%20Sans%20Serif.ttf?raw=true"
!test -f "/content/Microsoft Sans Serif.ttf?raw=true" && mv "/content/Microsoft Sans Serif.ttf?raw=true" "/content/SansSerif.ttf"
font = ImageFont.truetype("/content/SansSerif.ttf", 120)
man_seed = 25
image_count = 8
step_increase = 5
NSFW_skip_start_step = 5
height = 640
width = 832
# once we are ready to prompt.
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
images=[]
prompt = "My awesome prompt,\
multiline,\
sharp focus"
current_steps = NSFW_skip_start_step
for i in range(image_count):
with autocast("cuda"):
title = str(current_steps)
generator = torch.Generator("cuda").manual_seed(man_seed)
img = pipe(prompt, generator=generator, height=height, width=width, num_inference_steps=current_steps, guidance_scale=8 )["sample"][0]
draw = ImageDraw.Draw(img)
draw.text((400, 50), title, (255,255,255),font=font)
#img.save('/content/MyDrive/uniquename.jpg') TODO
images.append(img)
current_steps += step_increase
#single row
rows = 1
cols = len(images)
grid = image_grid(images, rows=rows, cols=cols)
#double row et cetera - no error checking for divisibility
# rows = 2
# cols = int(len(images)/rows)
#grid = image_grid(images, rows=rows, cols=cols)
#might as well
assert len(images) == rows*cols
#now show it
grid
@twobob
Copy link
Author

twobob commented Aug 23, 2022

added automatic retrieval of font

@twobob
Copy link
Author

twobob commented Aug 23, 2022

updated font grab to do tests to save bandwidth

@twobob
Copy link
Author

twobob commented Aug 23, 2022

text will now just be placed 50% from the left automatically on any size image
draw.text((width/2, 50), title, (255,255,255),font=font) <--see to adjust this implementation

@twobob
Copy link
Author

twobob commented Aug 23, 2022

added a basic GUI, attempts to skip over "bad" step values silently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment