-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
63 lines (56 loc) · 1.74 KB
/
image.py
File metadata and controls
63 lines (56 loc) · 1.74 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
55
56
57
58
59
60
61
62
63
import os
import sys
import base64
from datetime import datetime
import urllib.request
from dotenv import load_dotenv
import openai
from openai import OpenAI
load_dotenv()
# Fail if no commandline argument is provided
if len(sys.argv) < 2:
print("Please provide an text")
exit(1)
text_prompt = sys.argv[1]
client = OpenAI()
image_b64 = None
image_url = None
try:
image_model = os.getenv("IMAGE_MODEL") or "gpt-image-2"
if image_model in ("gpt-image-2", "gpt-image-1"):
response = client.images.generate(
model=image_model,
prompt=text_prompt,
size=os.getenv("IMAGE_SIZE") or "auto",
quality=os.getenv("IMAGE_QUALITY") or "auto",
background=os.getenv("IMAGE_BACKGROUND") or "auto",
n=1,
)
image_b64 = response.data[0].b64_json
else:
response = client.images.generate(
model=image_model,
prompt=text_prompt,
size=os.getenv("IMAGE_SIZE") or "1024x1024",
quality=os.getenv("IMAGE_QUALITY") or "standard",
n=1,
)
image_url = response.data[0].url
print(image_url)
except openai.OpenAIError as e:
print(e.http_status)
print(e.error)
exit(1)
# Save image to file in the image folder
if not os.path.isdir("image"):
os.mkdir("image")
cur_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
with open(f"image/{cur_time}.txt", "wb") as f:
f.write(text_prompt.encode("utf-8"))
if image_b64:
with open(f"image/{cur_time}.png", "wb") as f:
f.write(base64.b64decode(image_b64))
elif image_url:
with urllib.request.urlopen(image_url) as url_response:
with open(f"image/{cur_time}.png", "wb") as f:
f.write(url_response.read())