Skip to content

Commit 481a877

Browse files
committed
add gpt-image-1 to rest qs
1 parent 2539b10 commit 481a877

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

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

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Use this guide to get started calling the Azure OpenAI in Azure AI Foundry Model
1818
- <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>.
1919
- The following Python libraries installed: `os`, `requests`, `json`.
2020
- An Azure OpenAI resource created in a supported region. See [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability).
21-
- 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).
21+
- Then, you need to deploy a `gpt-image-1` 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).
2222

2323
## Setup
2424

@@ -41,6 +41,98 @@ Go to your resource in the Azure portal. On the navigation pane, select **Keys a
4141

4242
Create a new Python file named _quickstart.py_. Open the new file in your preferred editor or IDE.
4343

44+
#### [GPT-image-1](#tab/gpt-image-1)
45+
46+
1. Replace the contents of _quickstart.py_ with the following code. Change the value of `prompt` to your preferred text. Also set `deployment` to the deployment name you chose when you deployed the GPT-image-1 model.
47+
48+
```python
49+
import os
50+
import requests
51+
import base64
52+
from PIL import Image
53+
from io import BytesIO
54+
55+
# set environment variables
56+
endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
57+
subscription_key = os.getenv("AZURE_OPENAI_API_KEY")
58+
59+
deployment = "gpt-image-1" # the name of your GPT-image-1 deployment
60+
api_version = "2025-04-01-preview" # or later version
61+
62+
def decode_and_save_image(b64_data, output_filename):
63+
image = Image.open(BytesIO(base64.b64decode(b64_data)))
64+
image.show()
65+
image.save(output_filename)
66+
67+
def save_all_images_from_response(response_data, filename_prefix):
68+
for idx, item in enumerate(response_data['data']):
69+
b64_img = item['b64_json']
70+
filename = f"{filename_prefix}_{idx+1}.png"
71+
decode_and_save_image(b64_img, filename)
72+
print(f"Image saved to: '{filename}'")
73+
74+
base_path = f'openai/deployments/{deployment}/images'
75+
params = f'?api-version={api_version}'
76+
77+
generation_url = f"{endpoint}{base_path}/generations{params}"
78+
generation_body = {
79+
"prompt": "girl falling asleep",
80+
"n": 1,
81+
"size": "1024x1024",
82+
"quality": "medium",
83+
"output_format": "png"
84+
}
85+
generation_response = requests.post(
86+
generation_url,
87+
headers={
88+
'Api-Key': subscription_key,
89+
'Content-Type': 'application/json',
90+
},
91+
json=generation_body
92+
).json()
93+
save_all_images_from_response(generation_response, "generated_image")
94+
95+
# In addition to generating images, you can edit them.
96+
edit_url = f"{endpoint}{base_path}/edits{params}"
97+
edit_body = {
98+
"prompt": "girl falling asleep",
99+
"n": 1,
100+
"size": "1024x1024",
101+
"quality": "medium"
102+
}
103+
files = {
104+
"image": ("generated_image_1.png", open("generated_image_1.png", "rb"), "image/png"),
105+
# You can use a mask to specify which parts of the image you want to edit.
106+
# The mask must be the same size as the input image.
107+
# "mask": ("mask.png", open("mask.png", "rb"), "image/png"),
108+
}
109+
edit_response = requests.post(
110+
edit_url,
111+
headers={'Api-Key': subscription_key},
112+
data=edit_body,
113+
files=files
114+
).json()
115+
save_all_images_from_response(edit_response, "edited_image")
116+
```
117+
118+
The script makes a synchronous image generation API call.
119+
120+
> [!IMPORTANT]
121+
> 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).
122+
123+
1. Run the application with the `python` command:
124+
125+
```console
126+
python quickstart.py
127+
```
128+
129+
Wait a few moments to get the response.
130+
131+
132+
133+
#### [DALL-E](#tab/dall-e-3)
134+
135+
44136
1. Replace the contents of _quickstart.py_ with the following code. Change the value of `prompt` to your preferred text.
45137

46138
You also need to replace `<dalle3>` in the URL with the deployment name you chose when you deployed the DALL-E 3 model. Entering the model name will result in an error unless you chose a deployment name that is identical to the underlying model name. If you encounter an error, double check to make sure that you don't have a doubling of the `/` at the separation between your endpoint and `/openai/deployments`.
@@ -83,6 +175,8 @@ Create a new Python file named _quickstart.py_. Open the new file in your prefer
83175

84176
Wait a few moments to get the response.
85177

178+
---
179+
86180
## Output
87181

88182
The output from a successful image generation API call looks like the following example. The `url` field contains a URL where you can download the generated image. The URL stays active for 24 hours.
@@ -99,7 +193,7 @@ The output from a successful image generation API call looks like the following
99193
}
100194
```
101195

102-
The Image APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it doesn't generate an image. For more information, see [Content filtering](../concepts/content-filter.md). For examples of error responses, see the [DALL-E how-to guide](../how-to/dall-e.md).
196+
The Image APIs come with a content moderation filter. If the service recognizes your prompt as harmful content, it doesn't generate an image. For more information, see [Content filtering](../concepts/content-filter.md). For examples of error responses, see the [Image generation how-to guide](../how-to/dall-e.md).
103197

104198
The system returns an operation status of `Failed` and the `error.code` value in the message is set to `contentFilter`. Here's an example:
105199

0 commit comments

Comments
 (0)