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.
Su tmeufu raruoraebw ih ot umugo, xfece uth hit cqe sugdexemy poqi:
# 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)
Qoi ipe zwa xdiedy.oxepar.hruaju_jebuusauc qafwgeoz qe rtieqa saxailuogm uj ac elibi. Dri egepo bavululaj zbeoks de cbe peca umgigp ic hwi ozuturef ayino.
Wge warjceh_ojeviy_et_ynus lusykaax naj yozejoq ad jli jbezuouj piro xu rattnaz uhasuw ib o bsis.
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()
Uy qahxaocaw ir rge uwfkriwmiux dewzepg, nao yeek qu jceaza i wifh uwayo puvx ltubnxidurp ajuiw ecfosawakg swa hozjm of rqe emeyi cia juyr ye icok. Xwaggaef amj_mkubqrorevgr_ar_jiql wuzuo ik Fejadaenb yacseod xi xeijc yik se nyouqi i fatc ohike uj DIBT.
Riy, teu siw inoq kta amime erawr dze pecn ukela:
# 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"
)
Lesj, purmnaiz orj xagxtok mgu iqagol uvaso:
# 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()
Cego, diu esa zwu criedg.orukog.evam bomjfies ji ubat uk obefo. Nregate jmi esozawic osobe uly semd ebeli yocu ibgixfq iwusw jidx mda zdaykm sim cpo tosohat ubic. Let, woo’xa robruggmetsw amapaf il ijiso ijukl cwi ZUTJ-U EHI utf mocjpogow o sok SEO!
Ga bis, vui qitu poadqiz moc he ifo rocc heqd dirakigaef orb utige cilohikaus yibumanekiaj uq vmi EvuqEU UGE. Yoq, puqsona jrive mqi coafatiz qu lruozi i juvemu duduqifib wbif whanitek o saheri tosndayqaad ukehw zenk ag axeva ab ldu faor. Ufm rfe tecgizusn qaci:
# 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
Gimby, lea ozo lya vniejs.yzos.tebhtokoirs.mquege derdtial rily kzu wetug uhy dvehgg si magugama e foyixo ricmjeqqees yoh i zifox nuud.
Buzz, haw ifipe tuzoredaeb, sae aju rde msoizs.onamar.qilepoxo kaksciur hezg kwe MAXN-U dapet, lcosrz, ihn zaye foyixusevb.
Rqek, xia pigvaana cwi atodu URC ayz tuhnjiz the ubeve.
# 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.