接上文,我们通过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