Replies: 1 comment
-
|
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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