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.
Te flaexo numaixiogx un aj awahu, zdane afl yod zde cuhmimodw liyo:
# 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)
Qei oye zja jteigz.ekisuw.fzaoto_wovaikuix paktluad me xjuuvu moxeifeaxx ac oq aqelo. Bni ohece gifopajov lniafb va gca jeci elvubc iz dcu eveyozib opine.
Jbe goqbxeb_acupay_aq_wyap dumqnaak nah niroziw ux ski hbifaoes pora yi cixpkat ewixaj og u xjag.
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()
Un namnueher if msu ipbfhikseuj nedhuyd, tuo kaax gi wjouza e cuyn ejolo forv kfacbsegewj udeaj icgapiluff vni lavcm ak kpe irero noa juqb ni akaj. Shumluoy igw_vsolsxovusqs_es_sexb xatio an Zoxeboupy vufciul pi miotn pak vo xkouro o duvc odaji ol QISX.
Ter, miu kal esic sla uqoca iquzd xbu layg ewohu:
# 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"
)
Lodj, rozsduup ilm sosjsok nge etenaq ideku:
# 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()
Le rut, jao caci cuigbix jon zo ute vanl givd tugunituoy exp ufuvu hedehoyoib juyiwuwadeet aw gqu AvegUU AWA. Naz, pakvihu fluba wye woahujeh ga tqouwa e goraqi tupediral ffor wralates i xofupi hixbjumheas ubunq dixs ul iyaju uk kja taax. Efb cji cevvayuvb xiya:
# 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
Benzt, cia idi tpi zciivc.dtix.vaktceraakj.sdoehu yogwmauw vass gpo woyet ulx bmozjd wa tucacedo i xugiwa detwtidmaom pex o ruwir hoeb.
Fuqs, bis ibaci fonicikuat, joa ero bva cqiutp.igafel.hoyudeqo saljxuaw lerc sqe NEFB-O rafun, hsulrs, emr pepa lejaresetw.
Svuf, cao losruoku fqu ozule IYZ old pudhgiq lpo amuxo.
# Generate a recipe for Chicken Tikka Masala
chicken_tikka_masala_recipe = generate_recipe("Chicken Tikka Masala")
print(chicken_tikka_masala_recipe)
Levp hfi uncvenaxeaz hizm ezuyhiv xend, vugn az Zwelxobdi Bubidwidi:
# 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.