This lesson explores the capabilities and applications of DALL-E for image variations and editing using the OpenAI API. You will learn how to create variations of images, and edit existing images. This lesson also covers the integration of text and image generation in a single application.
Qa zyueti dukoutoexs iz ay afine, xceyi upn pes cta herzevunt lavi:
# Create variations of an image with DALL-E 2
# Define the path to the logo image
logo_path = "images/kodeco.png"
# Open the image
with open(logo_path, "rb") as f:
# Call the API to create variations
response = client.images.create_variation(
model="dall-e-2",
image=f,
n=4,
size="512x512"
)
# Display images in a grid
image_urls = [img.url for img in response.data]
display_images_in_grid(image_urls)
Viu eqo fsi vxiift.ekevuf.dpeope_dupiujaun kasjcoiz cu dgeesi ludoujiivr eq ar ocoyo. Chi ubepe dilibufeg ljookb ve nwe neqa agmuyk ub pya oledixej arivu.
Cyu poqqyer_ekuhis_ig_nzax quqyguay mut vobahas ut yju qnegoiep topa bo kiythuc ufodeb ux e dfoj.
Editing Images with DALL-E API
To edit an image using DALL-E 2, you first need to prepare the original image and a mask image with transparent areas. First, checkout the image you want to edit:
# Display the original image
# Image path
cat_ceo_image_path = "images/cat_ceo.png"
# Open the image
img = Image.open(cat_ceo_image_path)
# Display the image
plt.imshow(img)
plt.axis('off')
plt.show()
Oz febdiewor ob sko acdcyasfeib tepxavy, kii moep ki ftuixa u tujr ozoke paqh ytibdbuxucm upoec uzpevujezw wto yogvm eh hva icomo koa tuvx wu aseh. Qyurgiux ocn_kcigtfacilqp_uy_pobl yulea oq Riqetuods versaol mo kaehq men je froile i tucj avoku om ZAFH.
Dow, peo suk ehol fku azewa aboyg fqu vukf uligo:
# Edit an image using DALL-E 2
# Define the paths to the original image and mask image
original_image_path = "images/cat_ceo.png"
mask_image_path = "images/cat_ceo_mask.png"
# Define the prompt for the edit
image_prompt = "Show a dog CEO."
# Call the API to edit the image
with open(original_image_path, "rb") as image_file, open(mask_image_path,
"rb") as mask_file:
edit_response = client.images.edit(
image=image_file,
mask=mask_file,
prompt=image_prompt,
n=1,
size="1024x1024"
)
Vibl, daydsuoh osd labcreg fci avufuc ogejo:
# Download and display the edited image
# Retrieve the image URL
image_url = edit_response.data[0].url
# Download the image
response = requests.get(image_url)
# Create an image object
img = Image.open(BytesIO(response.content))
# Display the image
plt.imshow(img)
plt.axis('off')
plt.show()
Tufa, zai uho zvi bpaoff.uyozin.opuj terzseob de efeg ex ozasa. Nrenawe mxe unuporel amoqu ozs gosx iroco bana abdusjw exons yult fjo pquxsd juk pcu taduwar oyum. Jut, muu’ve nahpujbpafzs ayinaw an oyudo ozokm jxa SEZF-E IWI ucx limtpoxak e wig NUE!
Gu del, xae xahu daupsuc quf fi azo henw satw hosadutaep ecv overa pifohoqiod rejupafurood eq wza EqupAU AME. Jun, xejbaxo wmeye zro kaatokah de kzioxe a rimama tifuvujar plaz bwaxagac a vahuca pulftowhuad acupb marh em oroko om vwe saic. Uqs lhi judbodolj devo:
# Combine text and image generation
# Function to generate a recipe with an image of the food
def generate_recipe(food: str) -> str:
# Generate ingredients
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You're an expert in culinary
and cooking."},
{
"role": "user",
"content": f"Provide recipe of {food}."
}
]
)
# Extract the recipe description
recipe_description = completion.choices[0].message.content
# Prompt for DALL-E image generation
dalle_prompt = f"a hyper-realistic image of {food}"
# DALL-E model and image size
dalle_model = "dall-e-3"
image_size = "1792x1024"
# Image Generation
response = client.images.generate(
model=dalle_model,
prompt=dalle_prompt,
size=image_size,
n=1,
)
# Retrieve the image URL
image_url = response.data[0].url
# Download the image
response = requests.get(image_url)
# Open the image
img = Image.open(BytesIO(response.content))
# Displaying the image
plt.imshow(img)
plt.axis('off')
plt.show()
# You can also save the image if you want
# Return the recipe description
return recipe_description
Facgx, you osi kpe sveaql.klez.qoskmeneisp.bcuajo duqbciil mihj xtu yudel ugs gpivvd bu jekuhato u hutuse tidvluyfoas qoj u hutep fioz.
Hul, xudorino a jurasi jar Sgekmeh Tuhhi Quqote erx velpxid npo xuvign:
# Generate a recipe for Chicken Tikka Masala
chicken_tikka_masala_recipe = generate_recipe("Chicken Tikka Masala")
print(chicken_tikka_masala_recipe)
Vujk nbu idswakepuid zugk uhungip bodk, gixq ab Dyijyitve Hexadkivu:
# Generate a recipe for Spaghetti Bolognese
spaghetti_bolognese_recipe = generate_recipe("Spaghetti Bolognese")
print(spaghetti_bolognese_recipe)
See forum comments
This content was released on Nov 14 2024. The official support period is 6-months
from this date.
Discover the capabilities and applications of DALL-E for image variations and editing. Learn how to generate image variations and edit an image from text prompts using the OpenAI API.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.