Commit
Β·
34f361d
1
Parent(s):
792d3be
Create src/create.py
Browse files- src/create.py +76 -0
src/create.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import openai
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
|
7 |
+
WIDTH = 720
|
8 |
+
HEIGHT = 1280
|
9 |
+
|
10 |
+
|
11 |
+
def create_with_upload(
|
12 |
+
image,
|
13 |
+
name,
|
14 |
+
org,
|
15 |
+
name_font_size=50,
|
16 |
+
org_font_size=35,
|
17 |
+
vspace=50,
|
18 |
+
hspace=50,
|
19 |
+
between=30,
|
20 |
+
red=0,
|
21 |
+
green=0,
|
22 |
+
blue=100,
|
23 |
+
):
|
24 |
+
img = Image.fromarray(image).resize((HEIGHT, WIDTH), resample=Image.BICUBIC)
|
25 |
+
name_font = ImageFont.truetype("γγ©γγγδΈΈγ³γ ProN W4.ttc", name_font_size)
|
26 |
+
org_font = ImageFont.truetype("γγ©γγγδΈΈγ³γ ProN W4.ttc", org_font_size)
|
27 |
+
draw = ImageDraw.Draw(img)
|
28 |
+
draw.text((hspace, vspace), org, (red, green, blue), font=org_font)
|
29 |
+
draw.text(
|
30 |
+
(hspace, vspace + org_font_size + between),
|
31 |
+
name,
|
32 |
+
(red, green, blue),
|
33 |
+
font=name_font,
|
34 |
+
)
|
35 |
+
return np.array(img)
|
36 |
+
|
37 |
+
|
38 |
+
def create_with_generate(
|
39 |
+
prompt,
|
40 |
+
use_before,
|
41 |
+
api_key,
|
42 |
+
name,
|
43 |
+
org,
|
44 |
+
name_font_size=50,
|
45 |
+
org_font_size=35,
|
46 |
+
vspace=50,
|
47 |
+
hspace=50,
|
48 |
+
between=30,
|
49 |
+
red=0,
|
50 |
+
green=0,
|
51 |
+
blue=100,
|
52 |
+
):
|
53 |
+
openai.api_key = api_key
|
54 |
+
if use_before == "Generate new one":
|
55 |
+
response = openai.Image.create(
|
56 |
+
prompt=prompt,
|
57 |
+
n=1,
|
58 |
+
size="1024x1024",
|
59 |
+
response_format="b64_json",
|
60 |
+
)
|
61 |
+
img_data = base64.b64decode(response["data"][0]["b64_json"])
|
62 |
+
with open("tmp.png", "wb") as f:
|
63 |
+
f.write(img_data)
|
64 |
+
|
65 |
+
img = Image.open("tmp.png").resize((HEIGHT, WIDTH), resample=Image.BICUBIC)
|
66 |
+
name_font = ImageFont.truetype("γγ©γγγδΈΈγ³γ ProN W4.ttc", name_font_size)
|
67 |
+
org_font = ImageFont.truetype("γγ©γγγδΈΈγ³γ ProN W4.ttc", org_font_size)
|
68 |
+
draw = ImageDraw.Draw(img)
|
69 |
+
draw.text((hspace, vspace), org, (red, green, blue), font=org_font)
|
70 |
+
draw.text(
|
71 |
+
(hspace, vspace + org_font_size + between),
|
72 |
+
name,
|
73 |
+
(red, green, blue),
|
74 |
+
font=name_font,
|
75 |
+
)
|
76 |
+
return np.array(img)
|