0% found this document useful (0 votes)
22 views4 pages

Lab4 Image Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Lab4 Image Generation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX3: TEXT-TO-IMAGE AND IMAGE-TO-TEXT GENERATION

EX3: GENERATE AN IMAGE FROM THE PROMPT USING DALL-E in OPENAI

PROGRAM:

from openai import OpenAI

client = OpenAI()

response = client.images.generate(

prompt = "water color image of Zurich with color reflections in water",

size = "1024x1024"

#print(response)

print(response.data[0].url)

NOTE : THIS WILL GENERATE THE URL OF THE IMAGE. OPEN IT FROM THE BROWSER. SAMPLE
OUTPUT IS GIVEN BELOW.
EX2: GENERATE DESCRIPTIONS OF A GIVEN IMAGE (IMAGE-TO-TEXT GENERATION)

INPUT : URL of an image

PROGRAM:

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(

model="gpt-4-vision-preview",

messages=[

"role": "user",

"content": [

{"type": "text", "text": "What’s in this image?"},

"type": "image_url",

"image_url": {

"url": "https://images.unsplash.com/photo-1597431783670-205a592f954e?
crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=800&ixid=MnwxfDB8MXxyYW5kb218MHx8fHx8fHx
8MTcwMzE3ODc4Ng&ixlib=rb-4.0.3&q=80&utm_campaign=api-
credit&utm_medium=referral&utm_source=unsplash_source&w=1900"

},

],

],

temperature=1,

max_tokens=256,
top_p=1,

frequency_penalty=0,

presence_penalty=0

print(response)

print(response.choices[0].message.content)

Ex3 : IMAGE-TO-TEXT GENERATION FOR A CUSTOM IMAGE

INPUT: PATH TO THE CUSTOM IMAGE

PROGRAM:

import base64

from openai import OpenAI

client = OpenAI()

def encode_image(image_path):

with open(image_path, "rb") as image_file:

return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image

image_path = "C:/Users/HP/OneDrive -
presidencyuniversity.in/Desktop/GenAI_course_Udemy/Programs/openaiapidemo/Vision/
sample.jpeg"

# Getting the base64 string

base64_image = encode_image(image_path)

response = client.chat.completions.create(

model="gpt-4-vision-preview",
messages=[

"role": "user",

"content": [

{"type": "text", "text": "What’s in this image?"},

"type": "image_url",

"image_url": {

"url":f"data:image/jpeg;base64, {base64_image}"

},

],

],

temperature=1,

max_tokens=256,

top_p=1,

frequency_penalty=0,

presence_penalty=0

print(response)

print(response.choices[0].message.content)

You might also like