-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaugan.py
83 lines (64 loc) · 2.13 KB
/
gaugan.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Inspired from https://github.com/erikKeresztes/gaugan.py
import sys
import requests
import re
import base64
import random
import string
import http.client
import imghdr
def getUrl():
r = requests.get("http://www.imaginaire.cc/gaugan2/demo.js")
urls = re.findall(r"\'(http.*?://.*?/)\'", r.text)
return urls[0]
def processImage(image, style="random", url=None, caption=""):
if not isinstance(image, bytes):
raise (ValueError("Image must be bytes."))
if imghdr.what(None, image) != "png":
raise (ValueError("Image must be PNG format."))
if not isinstance(style, int) and style != "random":
raise (ValueError("Wrong style, must be an integer or 'random'"))
if url is None:
url = getUrl()
# get b64 encoded image
imgb64 = "data:image/png;base64," + str(base64.b64encode(image))[2:-1]
# generate name for requests
name = "".join(random.choices(string.ascii_lowercase + string.digits, k=20))
# send map img to server
requests.post(
url + "gaugan2_infer",
data={
"masked_image": imgb64,
"masked_edgemap": imgb64,
"masked_segmap": imgb64,
"name": name,
"caption": caption,
"style_name": "random",
"enable_seg": "true",
"enable_edge": "false",
"enable_caption": "false",
"enable_image": "false",
"use_model2": "false",
},
)
# get generated img from server
r = requests.post(
url + "gaugan2_receive_output",
stream=True,
data={"name": name, "style_name": str(style)},
)
if r.status_code != 200:
raise (
RuntimeError(
f"{http.client.responses[r.status_code]} ({r.status_code}) while processing image."
)
)
r.raw.decode_content = True
return r.raw.data
if __name__ == "__main__":
image_path = sys.argv[1]
caption = sys.argv[2] if len(sys.argv) >= 3 else ""
with open(image_path, "rb") as f:
image = processImage(f.read(), caption=caption)
with open("output.jpg", "wb") as f:
f.write(image)