Multimodal Integration with OpenAI

Nov 14 2024 · Python 3.12, OpenAI 1.52, JupyterLab, Visual Studio Code

Lesson 03: Image Generation & Editing with DALL-E

Demo of DALL-E Image Generation

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

This lesson explores the capabilities and applications of DALL-E for image generation. You will learn how to generate images based on text prompts and use various parameters to adjust the image generation.

# Install dependencies
!pip install Pillow
# Load the OpenAI library
from openai import OpenAI

# Set up relevant environment variables
from dotenv import load_dotenv

load_dotenv()

# Create the OpenAI connection object
client = OpenAI()
# Import necessary libraries
import requests
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt
import base64
# Define a function to generate an image using DALL-E
def generate_image(client, model, prompt, size, quality=None,
  style=None, response_format='url', n=1):
    params = {
        'model': model,
        'prompt': prompt,
        'size': size,
        'n': n,
        'response_format': response_format
    }
    if style:
        params['style'] = style
    if quality:
        params['quality'] = quality
    response = client.images.generate(**params)
    return response
# Define a function to display an image from a URL
def display_image_from_url(image_url):
    response = requests.get(image_url)
    img = Image.open(BytesIO(response.content))
    plt.imshow(img)
    plt.axis('off')
    plt.show()
# Define a function to display an image from base64
def display_image_from_base64(b64_string):
    img_data = base64.b64decode(b64_string)
    img = Image.open(BytesIO(img_data))
    plt.imshow(img)
    plt.axis('off')
    plt.show()
# Define a function to save an image to local storage
def save_image_to_local(image_url, filename):
    response = requests.get(image_url)
    img = Image.open(BytesIO(response.content))
    img.save(filename)
# Define a function to display multiple images in a grid
def display_images_in_grid(image_urls):
    num_images = len(image_urls)
    grid_size = int(num_images**0.5)
    fig, axes = plt.subplots(grid_size, grid_size, figsize=(10, 10))
    for i, image_url in enumerate(image_urls):
        response = requests.get(image_url)
        img = Image.open(BytesIO(response.content))
        row, col = divmod(i, grid_size)
        axes[row, col].imshow(img)
        axes[row, col].axis('off')
    plt.show()
# Generate and display an image with DALL-E 3
dalle_model = "dall-e-3"
dalle_prompt = "a samurai cat is eating ramen"
image_size = "1024x1792"
image_quality = "standard"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, image_quality)
image_url = response.data[0].url
display_image_from_url(image_url)
# Change the image quality and size
image_quality = "hd"
image_size = "1792x1024"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, image_quality)
image_url = response.data[0].url
display_image_from_url(image_url)
# Change image quality and style
image_quality = "standard"
image_style = "natural"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, image_quality, image_style)
image_url = response.data[0].url
display_image_from_url(image_url)
# Generate multiple images with DALL-E 2
dalle_model = "dall-e-2"
image_size = "512x512"
dalle_prompt = "a samurai cat is singing on a stage"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, n=4)
image_urls = [img.url for img in response.data]
display_images_in_grid(image_urls)
# Generate an image in base64 format
image_response_format = "b64_json"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, response_format=image_response_format)
b64_string = response.data[0].b64_json
display_image_from_base64(b64_string)
# Save the image to a local file
file_path = "samurai_cat_singing_on_stage.png"

response = generate_image(client, dalle_model, dalle_prompt,
  image_size, image_quality)
image_url = response.data[0].url
save_image_to_local(image_url, file_path)
See forum comments
Cinema mode Download course materials from Github
Previous: DALL-E Image Generation Next: DALL-E Image Variations & Editing