How to Use OpenAI’s GPT-Image-1 to Generate and Edit Images with Python

OpenAI recently released a powerful new image model called GPT-Image-1.
It can generate, edit, and enhance images with high quality, realistic details — and even handle text inside images better than ever before.

In this tutorial, I’ll show you how to use GPT-Image-1 with a few lines of Python!

Let’s dive in


What You’ll Need

  • Python 3.8+
  • OpenAI Python SDK (openai library)
  • Your OpenAI API key (you can get one at platform.openai.com)

Install the OpenAI library

If you haven’t installed it yet, run:

pip install openai

1. How to Generate a New Image from a Text Prompt

Here’s the simplest way to generate an image:

from openai import OpenAI
import base64

# Initialize OpenAI client
client = OpenAI()

# Define your prompt
prompt = "A futuristic city at sunset with flying cars, digital art style."

# Request image generation
response = client.images.generate(
    model="gpt-image-1",
    prompt=prompt,
    size="1024x1024",
    quality="high",
    style="vivid"
)

# Decode and save the image
image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("futuristic_city.png", "wb") as f:
    f.write(image_bytes)

print("Image saved as futuristic_city.png!")

Done! You just created your first AI image with GPT-Image-1!


2. How to Edit an Existing Image

You can also edit an image using a new prompt.
You can optionally provide a mask if you want to edit only part of the image.

Full Image Edit (No Mask)

from openai import OpenAI
import base64

client = OpenAI()

response = client.images.edit(
    model="gpt-image-1",
    image=open("original_image.png", "rb"),
    prompt="Turn the scene into a snowy winter wonderland.",
    size="1024x1024",
    quality="high",
    style="natural"
)

image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("winter_scene.png", "wb") as f:
    f.write(image_bytes)

print("Edited image saved as winter_scene.png!")

Partial Image Edit (Using a Mask)

use of mask feature

If you want to only modify part of the image, you can upload a mask:

from openai import OpenAI
import base64

client = OpenAI()

response = client.images.edit(
    model="gpt-image-1",
    image=open("beach_scene.png", "rb"),
    mask=open("mask.png", "rb"),  # Mask out only the sky, for example
    prompt="Replace the sky with a beautiful aurora borealis.",
    size="1024x1024",
    quality="high",
    style="vivid"
)

image_base64 = response.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("aurora_scene.png", "wb") as f:
    f.write(image_bytes)

print("Partially edited image saved as aurora_scene.png!")

Important:

  • The mask.png must be the same size as the original image.
  • In the mask, white areas are editable, black areas stay unchanged.

Available Options for GPT-Image-1

ParameterDescriptionExample
sizeImage size"1024x1024", "1536x1024", "1024x1536"
qualityImage detail level"high", "medium", "low"
styleImage artistic style"natural" or "vivid"
backgroundTransparent background"transparent" or "opaque"
formatOutput format"png", "jpeg", "webp"

Why GPT-Image-1 Is Special

  • Better text rendering inside images (logos, posters, ads).
  • Real-world knowledge (accurate objects, brands, people).
  • Flexible editing — full scene edits or masked inpainting.
  • Large high-quality outputs up to 1536px wide.

It’s OpenAI’s most powerful image model yet!


Final Thoughts

With just a few lines of Python, you can start generating, editing, and customizing beautiful AI images using GPT-Image-1.

This makes it super easy to:

  • Create original artwork
  • Prototype designs
  • Edit product shots
  • Build AI-driven content creation apps

The possibilities are endless.


Leave a Reply

x
Advertisements