Stable Diffusion Diffusers Toolbox 01 (NSFW, Image Size and Generating Mutiple Images)

1 minute read

Let’s look at a few esential tools that will come in handy later.

from diffusers import StableDiffusionPipeline
DEVICE='mps'
pipe = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5", use_auth_token=True).to(DEVICE)

Not Safe for Work Flag (NSFW)

At your own discression you can turn off the NSFW checker. This will prevent any NSFW messages but may introduce images that possibly have mature content.

pipe.safety_checker = lambda images, clip_input: (images, False)

Output Image Size (Resolution)

As the plan to eventually produce AI generated TikTok videos, let’s experiment with getting a quality portrait image up to 1080 x 1920 resolution. If we use an 3x upscaler (discussed later) the output from stable diffusion should be …

1080/3, 1920/3
(360.0, 640.0)

Note height and width have to be divisible by 8. We’re good with the values above.

Let’s set a height and width image dimensions and try out an example.

image = pipe("realistic photo of woman vampire, wide mouth showing fangs, moody, foggy, gloomy", height=640, width=360).images[0]
image
  0%|          | 0/51 [00:00<?, ?it/s]

png

Upscaling Images

I will come back to this problem. I tried to install a potential library called super-image but had package version conflicting dependencies. For now I have a good Mac App that let’s me upscale images in batch and why re-invent the wheel? The App I use FYI is called Super Photo Upscaler.

Generating and Saving Multiple Images in loop.

As you’re probably aware already, sometimes you have to generate mutliple images to get a few really good ones.

Working on the same prompt as above, let’s get our Apple Silicon working and generating mutiple images in our ‘outputs’ folder.

Note: I’m aware you can also do this by passing in multiple prompts to the pipeline, but let’s play with that later.

num_images=50
prefix='img_'
out_path='outputs/'
for i in range(num_images):
    image = pipe("realistic photo of woman vampire, wide mouth showing fangs, moody, foggy, gloomy", height=640, width=360).images[0]
    filename=prefix+str(i).zfill(5)+'.png'
    image.save(out_path+filename)
  0%|          | 0/51 [00:00<?, ?it/s]



  0%|          | 0/51 [00:00<?, ?it/s]



  0%|          | 0/51 [00:00<?, ?it/s]

[Snipped]

  0%|          | 0/51 [00:00<?, ?it/s]



  0%|          | 0/51 [00:00<?, ?it/s]



  0%|          | 0/51 [00:00<?, ?it/s]

This will generate 50 images in out ouputs folder. Let’s use these to create a video.

Updated:

Leave a comment