forked from guifaChild/text_to_vedio_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_to_image.py
More file actions
59 lines (43 loc) · 1.46 KB
/
data_to_image.py
File metadata and controls
59 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
"""
作者:张贵发
日期:2023年07月07日
描述:根据生成的prompt提示词来生成对应的图片
"""
import os.path
import requests
import openai
import pandas as pd
def SaveImgFromUrl(response, save_path):
numOfOutput = len(response)
org_path = save_path
for i in range(numOfOutput):
save_path = org_path
img_content = requests.get(response[i]["url"]).content
if i >= 1:
save_path = save_path.split(".")[0] + "_" + str(i + 1) + "." + save_path.split(".")[1]
with open(save_path, "wb") as f:
f.write(img_content)
def CreateImage( description, path,key):
size = "1024x1024"
if size not in ["256x256", "512x512", "1024x1024"]: # 校验生成图片尺寸
raise Exception("图片尺寸不符,仅支持 256x256, 512x512, 1024x1024三种大小")
openai.api_key = key
image = openai.Image.create(
prompt=description,
n=1,
size=size,
response_format="url",
)
SaveImgFromUrl(image.data, path)
def load_image_data(path,key):
df = pd.read_csv(path)
newpath = path.split(".csv")[0].replace("data_prompt", "data_image")
if not os.path.exists(newpath):
os.makedirs(newpath)
for index, row in df.iterrows():
childpath = os.path.join(newpath,str(index)+".png")
CreateImage(row["prompt"][:10],childpath,key)
return newpath
if __name__ == '__main__':
size = "1024x1024"