Replies: 2 comments
-
|
The issue is that Fix for issue 1 -- loading the generated image into PIL: import io
import requests
from PIL import Image
from openai import OpenAI
client = OpenAI(api_key=api_key)
response = client.images.generate(model="dall-e-3", prompt="A puppy wearing a cosmonaut helmet")
# Download and load into PIL
image_data = requests.get(response.data[0].url).content
image = Image.open(io.BytesIO(image_data))Use Fix for issue 2 -- passing the image to the edit API: The # Convert to RGBA and save to a buffer as PNG
image = image.convert("RGBA")
# Resize if needed -- dall-e-2 edit requires square images (1024x1024 or smaller)
image = image.resize((1024, 1024))
# Save to buffer as PNG
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
# Pass the buffer to the edit API
response = client.images.edit(
image=buf,
model="dall-e-2",
prompt="This character is reading a book",
size="1024x1024",
)Key gotchas:
Complete working example: import io
import requests
from PIL import Image
from openai import OpenAI
client = OpenAI()
# Generate
gen_response = client.images.generate(
model="dall-e-3",
prompt="A puppy wearing a cosmonaut helmet",
)
# Load into memory
image_data = requests.get(gen_response.data[0].url).content
image = Image.open(io.BytesIO(image_data)).convert("RGBA")
# Prepare for edit API
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
# Edit
edit_response = client.images.edit(
image=buf,
model="dall-e-2",
prompt="This character is reading a book",
)
print(edit_response.data[0].url) |
Beta Was this translation helpful? Give feedback.
-
SolutionIssue 1: Loading DALL-E image into PIL.ImageYou need to use import io
import requests
from PIL import Image
image_data = requests.get(response.data[0].url).content
image = Image.open(io.BytesIO(image_data))
# image is now a PIL.Image you can work with
Issue 2: Passing image to the edit() APIThe import io
# Save as PNG to a BytesIO buffer
png_buffer = io.BytesIO()
image.save(png_buffer, format="PNG")
png_buffer.seek(0)
png_buffer.name = "image.png" # Required so httpx detects it as a file upload
response = client.images.edit(
model="dall-e-2",
image=png_buffer,
prompt="This character is reading a book"
)Key points:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to generate an image with DALL-e and then use this image as a starting point to generate other images and I'm running into 2 issues:
Creation of the image:
1) First issue
This triggers the following error:
2) Second issue
This triggers the following error:
Beta Was this translation helpful? Give feedback.
All reactions