-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (126 loc) · 3.95 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# imports
import Render.render as render
import Render.cam as cam
import Render.imagen as imagen
import Transform.transform as transform
import os
import sys
import time
import imghdr
import shutil
import argparse
import requests
from uuid import uuid4 as uuid
def banner():
return """
credit: yy
"""
def parse_args():
parser = argparse.ArgumentParser(description="boioioing")
parser.add_argument(
"-u",
"--url",
help="takes a url argument containing the image"
)
parser.add_argument(
"-p",
"--path",
help="specifies a path for the program to get image from"
)
parser.add_argument(
"-m",
"--mode",
help="specifies what effects to apply to the input image, defaults to boioioing",
default="boing",
choices=["boing", "static", "rotate", "spin", "stream"],
nargs="?"
)
parser.add_argument(
"--generate",
help="generate an image with stable diffusion",
default="NO_GEN",
nargs="?"
)
parser.add_argument(
"--gif",
help="generate a gif (not implemented yet)"
)
if len(sys.argv) == 1:
print(banner())
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def get_image(url, save_path):
response = requests.get(url)
if response.status_code == 200:
with open(save_path, 'wb') as file_handle: # 'wb' - write mode for binary files
file_handle.write(response.content)
image_format = imghdr.what(save_path)
new_path = f"{save_path}.{image_format}"
os.rename(save_path, new_path)
else:
raise Exception("Cannot get image")
return new_path
def main(): # program main entry point
save_path = "./temp/"
sysos = sys.platform
if sysos == "win32":
def clear_terminal():
os.system("cls")
if sysos == "linux" or sysos == "darwin": # darwin is macos
def clear_terminal():
os.system("clear")
if os.path.exists(save_path):
shutil.rmtree(save_path) # remove temp and its contents
args = parse_args()
mode = args.mode
rng = str(uuid())
os.mkdir(save_path)
if mode == "stream":
for img_path in cam.get_cam_input(save_path):
transform.resize_image(img_path)
render.render_image(img_path)
time.sleep(1/ 50)
clear_terminal()
os.remove(img_path)
if args.generate != "NO_GEN":
# with openai api
#args.url = imagen.generate(args.generate)
args.path = imagen._generate(args.generate, save_path+rng)
if args.path is not None:
image_src = transform.resize_image(args.path)
else:
image_src = transform.resize_image(get_image(args.url, save_path+rng))
if mode == "boing":
image_paths = transform.boioioing(image_src)
while True:
for i in range(len(image_paths)):
render.render_image(image_paths[i])
time.sleep(1 / 50)
clear_terminal()
if mode == "static":
render.render_image(image_src)
if mode == "rotate":
image_paths = transform.rotato(image_src)
while True:
for i in range(len(image_paths)):
render.render_image(image_paths[i])
time.sleep(1 / 50)
clear_terminal()
if mode == "spin":
image_paths = transform.speen(image_src)
while True:
for i in range(len(image_paths)):
render.render_image(image_paths[i])
time.sleep(1 / 50)
clear_terminal()
# some code to export the result as a gif
# import imageio
# if args.gif == "a":
# img = []
# for i in range(0, len(image_paths)):
# img.append(imageio.imread(image_paths[i]))
# imageio.mimsave('wallahi.gif', img)
if __name__ == "__main__":
main()