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 Variations & Editing

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 variations and editing using the OpenAI API. You will learn how to create variations of images, and edit existing images. This lesson also covers the integration of text and image generation in a single application.

# Create variations of an image with DALL-E 2

# Define the path to the logo image
logo_path = "images/kodeco.png"

# Open the image
with open(logo_path, "rb") as f:
    # Call the API to create variations
    response = client.images.create_variation(
      model="dall-e-2",
      image=f,
      n=4,
      size="512x512"
    )

# Display images in a grid
image_urls = [img.url for img in response.data]
display_images_in_grid(image_urls)

Editing Images with DALL-E API

To edit an image using DALL-E 2, you first need to prepare the original image and a mask image with transparent areas. First, checkout the image you want to edit:

# Display the original image

# Image path
cat_ceo_image_path = "images/cat_ceo.png"

# Open the image
img = Image.open(cat_ceo_image_path)

# Display the image
plt.imshow(img)
plt.axis('off')
plt.show()
# Edit an image using DALL-E 2

# Define the paths to the original image and mask image
original_image_path = "images/cat_ceo.png"
mask_image_path = "images/cat_ceo_mask.png"

# Define the prompt for the edit
image_prompt = "Show a dog CEO."

# Call the API to edit the image
with open(original_image_path, "rb") as image_file, open(mask_image_path,
  "rb") as mask_file:
    edit_response = client.images.edit(
        image=image_file,
        mask=mask_file,
        prompt=image_prompt,
        n=1,
        size="1024x1024"
    )
# Download and display the edited image

# Retrieve the image URL
image_url = edit_response.data[0].url

# Download the image
response = requests.get(image_url)

# Create an image object
img = Image.open(BytesIO(response.content))

# Display the image
plt.imshow(img)
plt.axis('off')
plt.show()
# Combine text and image generation

# Function to generate a recipe with an image of the food
def generate_recipe(food: str) -> str:

    # Generate ingredients
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You're an expert in culinary
              and cooking."},
            {
                "role": "user",
                "content": f"Provide recipe of {food}."
            }
        ]
    )

    # Extract the recipe description
    recipe_description = completion.choices[0].message.content

    # Prompt for DALL-E image generation
    dalle_prompt = f"a hyper-realistic image of {food}"
    # DALL-E model and image size
    dalle_model = "dall-e-3"
    image_size = "1792x1024"

    # Image Generation
    response = client.images.generate(
      model=dalle_model,
      prompt=dalle_prompt,
      size=image_size,
      n=1,
    )

    # Retrieve the image URL
    image_url = response.data[0].url

    # Download the image
    response = requests.get(image_url)

    # Open the image
    img = Image.open(BytesIO(response.content))

    # Displaying the image
    plt.imshow(img)
    plt.axis('off')
    plt.show()

    # You can also save the image if you want

    # Return the recipe description
    return recipe_description
# Generate a recipe for Chicken Tikka Masala
chicken_tikka_masala_recipe = generate_recipe("Chicken Tikka Masala")
print(chicken_tikka_masala_recipe)
# Generate a recipe for Spaghetti Bolognese
spaghetti_bolognese_recipe = generate_recipe("Spaghetti Bolognese")
print(spaghetti_bolognese_recipe)
See forum comments
Cinema mode Download course materials from Github
Previous: DALL-E Image Variations & Editing Next: Conclusion