Skip to content

Commit 2539b10

Browse files
committed
add gpt-image-1 qs
1 parent 7d17f94 commit 2539b10

File tree

1 file changed

+81
-3
lines changed

1 file changed

+81
-3
lines changed

articles/ai-services/openai/includes/dall-e-python.md

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Use this guide to get started generating images with the Azure OpenAI SDK for Py
1919
- An Azure subscription. <a href="https://azure.microsoft.com/free/ai-services" target="_blank">Create one for free</a>.
2020
- <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>.
2121
- An Azure OpenAI resource created in a compatible region. See [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability).
22-
- Then, you need to deploy a `dalle3` model with your Azure resource. For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
22+
- Then, you need to deploy a `gpt-image-1` model or `dalle3` model with your Azure resource. For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
2323

2424
## Setup
2525

@@ -53,12 +53,89 @@ pip install requests
5353
pip install pillow
5454
```
5555

56-
## Generate images with DALL-E
56+
## Generate images
5757

5858
Create a new python file, _quickstart.py_. Open it in your preferred editor or IDE.
5959

60+
#### [GPT-image-1](#tab/gpt-image-1)
61+
6062
Replace the contents of _quickstart.py_ with the following code.
6163

64+
```python
65+
import os
66+
import requests
67+
import base64
68+
from PIL import Image
69+
from io import BytesIO
70+
71+
# You will need to set these environment variables or edit the following values.
72+
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
73+
subscription_key = os.getenv("AZURE_OPENAI_API_KEY")
74+
75+
deployment = "gpt-image-1" # the name of your GPT-image-1 deployment
76+
api_version = "2025-04-01-preview" # or later version
77+
78+
def decode_and_save_image(b64_data, output_filename):
79+
image = Image.open(BytesIO(base64.b64decode(b64_data)))
80+
image.show()
81+
image.save(output_filename)
82+
83+
def save_all_images_from_response(response_data, filename_prefix):
84+
for idx, item in enumerate(response_data['data']):
85+
b64_img = item['b64_json']
86+
filename = f"{filename_prefix}_{idx+1}.png"
87+
decode_and_save_image(b64_img, filename)
88+
print(f"Image saved to: '{filename}'")
89+
90+
base_path = f'openai/deployments/{deployment}/images'
91+
params = f'?api-version={api_version}'
92+
93+
generation_url = f"{endpoint}{base_path}/generations{params}"
94+
generation_body = {
95+
"prompt": "girl falling asleep",
96+
"n": 1,
97+
"size": "1024x1024",
98+
"quality": "medium",
99+
"output_format": "png"
100+
}
101+
generation_response = requests.post(
102+
generation_url,
103+
headers={
104+
'Api-Key': subscription_key,
105+
'Content-Type': 'application/json',
106+
},
107+
json=generation_body
108+
).json()
109+
save_all_images_from_response(generation_response, "generated_image")
110+
111+
# In addition to generating images, you can edit them.
112+
edit_url = f"{endpoint}{base_path}/edits{params}"
113+
edit_body = {
114+
"prompt": "girl falling asleep",
115+
"n": 1,
116+
"size": "1024x1024",
117+
"quality": "medium"
118+
}
119+
files = {
120+
"image": ("generated_image_1.png", open("generated_image_1.png", "rb"), "image/png"),
121+
# You can use a mask to specify which parts of the image you want to edit.
122+
# The mask must be the same size as the input image.
123+
# "mask": ("mask.png", open("mask.png", "rb"), "image/png"),
124+
}
125+
edit_response = requests.post(
126+
edit_url,
127+
headers={'Api-Key': subscription_key},
128+
data=edit_body,
129+
files=files
130+
).json()
131+
save_all_images_from_response(edit_response, "edited_image")
132+
```
133+
134+
1. Change the value of `prompt` to your preferred text.
135+
1. Change the value of `deployment` to the name of your deployed GPT-image-1 model.
136+
137+
#### [DALL-E](#tab/dall-e-3)
138+
62139
```python
63140
from openai import AzureOpenAI
64141
import os
@@ -101,10 +178,11 @@ image = Image.open(image_path)
101178
image.show()
102179
```
103180

104-
1. Enter your endpoint URL and key in the appropriate fields.
105181
1. Change the value of `prompt` to your preferred text.
106182
1. Change the value of `model` to the name of your deployed DALL-E 3 model.
107183

184+
---
185+
108186
> [!IMPORTANT]
109187
> Remember to remove the key from your code when you're done, and never post your key publicly. For production, use a secure way of storing and accessing your credentials. For more information, see [Azure Key Vault](/azure/key-vault/general/overview).
110188

0 commit comments

Comments
 (0)