ChatGPT API的使用(二)

接上文,我们通过API访问OpenAI GPT-3与GPT-3.5后,OpenAI还提供了图片生成与语音转换功能。

实现通过文字生成图片,可以提供多个prompts,使用逗号隔开,画风十分真实,可以同时生成多张图片,并保存在本地。

img_num = 'the number of pictures you want to create'
img_path = r'the path you want to save these images'

response = openai.Image.create(
    prompt = image_prompt,
    n = image_num,
    size = "1024x1024"#256*256,512*512,1024*1024
    )

datas = response['data']
for data in datas:
    image_url = data['url']
    r = requests.get(image_url)
    with open(img_path + '/' + image_prompt + '.png', 'wb') as f: 
        f.write(r.content)
        img = Image.open(img_path + '/' + image_prompt + '.png')
        img.show()

可以根据图片通过增加描述的方式,修改图片细节。

image_path = r'the image path you want to variation'
img_path = r'the path you want to save these images'

response = openai.Image.create_variation(
image = open(image_path, "rb"),
n = image_num,
size = "1024x1024"
)  

datas = response['data']
for data in datas:
    image_url = data['url']
    r = requests.get(image_url)
    #重名会覆盖
    with open(img_path +  'variation' + '.png', 'wb') as f: 
        f.write(r.content)
    img = Image.open(img_path + 'variation' + '.png')
    img.show()

如果对图片的部分不满意可以通过使用PhotoShop擦除部分图片,并描述缺失部分的方式,生成更加个性化的图片。

image_path = r‘the image path you want to edit’
mask_path = r'the image you have edited'
img_path = r'the path you want to save these images'
prompt = 'the descriptionthe of missing part '

response = openai.Image.create_edit(
image = open(image_path, "rb"),
mask = open(mask_path, "rb"),
prompt = image_prompt,
n = image_num,
size = "1024x1024"
)


datas = response['data']
for data in datas:
    image_url = data['url']
    r = requests.get(image_url)
    #重名会覆盖
    with open(img_path + '/' + image_prompt + 'edited' + '.png', 'wb') as f: 
        f.write(r.content)
    img = Image.open(img_path + '/' + image_prompt + 'edited' + '.png')
    img.show()

将音频转化为文字

audio_path = 'the audio file you want to transcription'
language = 'the audio language'
txt_path = r'the path you want to save the audio txt file'

audio_file = open(audio_path, "rb")#mp3, mp4, mpeg, mpga, m4a, wav, or webm. are availiable
transcript = openai.Audio.transcribe("whisper-1", audio_file)
language = language#iso标准码
prompt = prompt
response_format = 'text'

file = open(txt_path + '/' + name + '.txt', 'w')
file.write(transcript)
file.close

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chisato_Nishikigi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值