Media Gen Solution

Image Generator Python client

The ImageGenClient class specializes in supporting image generation in your application, and guiding what options are available to modify your outputs. It will return a list of all images using the ImageGeneration type. It allows you to use both Stable Diffusion 1.5 and Stable Diffusion XL for text to image and image to image use cases, and set parameters and prompts either with weighted prompts with the prompt field as was common with Stable Diffusion 1.5 or human-readable descriptions using prompt_2 with Stable Diffusion XL 1.0.

This guide will walk you through a text to image example, and then we will use the resulting image to demonstrate the image to image use case.


Simple Text to Image Generation Example

1from octoai.util import to_file
2from octoai.client import OctoAI
4if __name__ == "__main__":
5 # If OCTOAI_TOKEN is not set as an envvar, you can also pass a token to the client with:
6 # ImageGenerator(token="YOUR_TOKEN_HERE")
7 client = OctoAI()
8 # images is a list of Images from octoai.types
9 image_resp = client.image_gen.generate_sdxl(
10 prompt="photorealistic, poodle, intricately detailed"
11 )
12 images = image_resp.images
14 # images can be filtered for safety, so since we only generated 1 image by default, this verifies
15 # we actually have an image to show.
16 if not images[0].removed_for_safety:
17 to_file(images[0], "output.jpg")

After running this simple prompt, you should hopefully have an output somewhat similar to the image below:


A good start and in our next example, we’ll use more features to help guide our outputs.

Text to Image Generation Example

One of the simplest ways to customize your outputs is a style preset, negative_prompt, loras, and model selection.

1from octoai.util import to_file
2from octoai.client import OctoAI
4if __name__ == "__main__":
5 client = OctoAI()
7 prompt = "photorealistic, colorful, poodle, intricately detailed"
8 file_name = "pretty_poodle_cinematic.jpeg"
10 images_resp = client.image_gen.generate_sdxl(
11 prompt=prompt,
12 negative_prompt="horror, scary, low-quality, extra limbs, cartoon",
13 checkpoint="crystal-clear",
14 style_preset="cinematic",
15 loras={"add-detail": 1.0},
16 steps=50,
17 )
18 images = images_resp.images
19 # It can also be helpful to run another generate method with
20 # num_images = image_resp.removed_for_safety to get your desired total images
21 if not images[0].removed_for_safety:
22 to_file(images[0], file_name)


Much more realistic! Now that we have our cinematic poodle, let’s go ahead and use this image as part of an image-to-image workflow.

Image to Image Generation Example

Image to Image Generation lets you use a base image, in our case the above pretty_poodle.jpeg to shape the feel of your outputs image. In our case, we’d expect some focal point in the center, and a blurred, bright background, but otherwise our output can look anywhere from completely different or quite similar depending on our prompt. In this case, let’s go for a complete different style of outputs and stray from the usual theme of poodles to a corgi in the rain.

1from octoai.util import to_file, from_file
2from octoai.client import OctoAI
4if __name__ == "__main__":
5 client = OctoAI()
7 init = from_file("pretty_poodle_cinematic.jpeg")
8 images_resp = client.image_gen.generate_sdxl(
9 prompt="corgi in the rain",
10 init_image=init, # Only used for image-to-image
11 strength=0.8, # Only used for image-to-image
12 style_preset="anime"
13 )
14 images = images_resp.images
16 to_file(images[0], "rain_corgi.jpeg")