-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
217 lines (190 loc) · 7.29 KB
/
main.py
File metadata and controls
217 lines (190 loc) · 7.29 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from dataclasses import dataclass
from collections.abc import MutableSequence
from PIL import Image, ImageFilter
import numpy as np
@dataclass
class ivec2:
x: int
y: int
@dataclass
class Layer:
img: Image
name: str
pos: ivec2
alpha: float
class InformalCommandInterface:
def getName(self) -> str:
pass
def call(self, tokens: MutableSequence[str]) -> None:
pass
global layerArray
layerArray: MutableSequence[Layer] = []
def getLayer(name: str) -> Layer:
for l in layerArray:
if (l.name == name): return l
def getIndexOfLayer(name: str) -> int:
i = 0
for l in layerArray:
if (l.name == name): return i
else: i += 1
class LoadCommand(InformalCommandInterface):
def getName(self) -> str:
return "load"
def call(self, tokens: MutableSequence[str]) -> None:
layerArray.append(Layer(Image.fromarray(np.array(Image.open(tokens[1]))).convert("RGBA"), tokens[2], (0, 0), 1.0))
class ResizeCommand(InformalCommandInterface):
def getName(self) -> str:
return "resize"
def call(self, tokens: MutableSequence[str]) -> None:
getLayer(tokens[1]).img.thumbnail((int(tokens[2]), int(tokens[3])))
class ExportCommand(InformalCommandInterface):
def getName(self) -> str:
return "export"
def call(self, tokens: MutableSequence[str]) -> None:
image = getLayer(tokens[1]).img.convert("RGB")
if (len(tokens) == 4): image.save(tokens[2], dpi=(int(tokens[3]), int(tokens[3])))
else: image.save(tokens[2])
class DeleteLayerCommand(InformalCommandInterface):
def getName(self) -> str:
return "delete"
def call(self, tokens: MutableSequence[str]) -> None:
layerArray.pop(getIndexOfLayer(tokens[1]))
class ShowLayerCommand(InformalCommandInterface):
def getName(self) -> str:
return "show"
def call(self, tokens: MutableSequence[str]) -> None:
getLayer(tokens[1]).img.show()
class ListLayersCommand(InformalCommandInterface):
def getName(self) -> str:
return "list"
def call(self, tokens: MutableSequence[str]) -> None:
if (len(layerArray) > 0):
print("Layers:")
for l in layerArray:
print("- " + l.name)
print(" Sized at " + str(l.img.size))
print(" Located at " + str(l.pos))
print(" Alpha of " + str(l.alpha))
else:
print("No current layers!")
class QuitCommand(InformalCommandInterface):
def getName(self) -> str:
return "quit"
def call(self, tokens: MutableSequence[str]) -> None:
exit(0)
class AddCommand(InformalCommandInterface):
def getName(self) -> str:
return "add"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels+=int(tokens[2])
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels<clone]=255
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class SubCommand(InformalCommandInterface):
def getName(self) -> str:
return "sub"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels = pixels - int(tokens[2])
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels>clone]=0
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class MulCommand(InformalCommandInterface):
def getName(self) -> str:
return "mul"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels = pixels * int(tokens[2])
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels<clone]=255
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class DivCommand(InformalCommandInterface):
def getName(self) -> str:
return "div"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels = pixels / int(tokens[2])
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels>clone]=0
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class MoveCommand(InformalCommandInterface):
def getName(self) -> str:
return "move"
def call(self, tokens: MutableSequence[str]) -> None:
getLayer(tokens[1]).pos = ivec2(int(tokens[2]), int(tokens[3]))
class AlphaCommand(InformalCommandInterface):
def getName(self) -> str:
return "alpha"
def call(self, tokens: MutableSequence[str]) -> None:
getLayer(tokens[1]).alpha = float(tokens[2])
class MergeDownCommand(InformalCommandInterface):
def getName(self) -> str:
return "merge"
def call(self, tokens: MutableSequence[str]) -> None:
for i in range(1, len(layerArray)):
layerArray[0].img = Image.blend(layerArray[0].img, layerArray[i].img, layerArray[i].alpha)
class LayerAddCommand(InformalCommandInterface):
def getName(self) -> str:
return "ladd"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB")) + np.array(getLayer(tokens[2]).img.convert("RGB"))
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels<clone]=255
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class LayerMulCommand(InformalCommandInterface):
def getName(self) -> str:
return "lmul"
def call(self, tokens: MutableSequence[str]) -> None:
pixels = np.array(getLayer(tokens[1]).img.convert("RGB")) * np.array(getLayer(tokens[2]).img.convert("RGB"))
clone = np.array(getLayer(tokens[1]).img.convert("RGB"))
pixels[pixels<clone]=255
getLayer(tokens[1]).img = Image.fromarray(pixels, 'RGB')
class GaussianBlurCommand(InformalCommandInterface):
def getName(self) -> str:
return "blur"
def call(self, tokens: MutableSequence[str]) -> None:
rad = 10
if (len(tokens) == 3): rad = int(tokens[2])
getLayer(tokens[1]).img = getLayer(tokens[1]).img.filter(ImageFilter.GaussianBlur(radius=rad))
class SobelCommand(InformalCommandInterface):
def getName(self) -> str:
return "edge"
def call(self, tokens: MutableSequence[str]) -> None:
getLayer(tokens[1]).img = getLayer(tokens[1]).img.convert("RGB").filter(ImageFilter.Kernel((3, 3), (
-1, -1, -1,
-1, 8, -1,
-1, -1, -1),
1, 0))
global commandArray
commandArray: MutableSequence[InformalCommandInterface] = [
LoadCommand(),
ResizeCommand(),
ExportCommand(),
DeleteLayerCommand(),
ShowLayerCommand(),
ListLayersCommand(),
QuitCommand(),
AddCommand(),
SubCommand(),
MulCommand(),
DivCommand(),
MoveCommand(),
AlphaCommand(),
MergeDownCommand(),
GaussianBlurCommand(),
SobelCommand(),
LayerAddCommand(),
LayerMulCommand()
]
def parseCommand(command: str):
tokens = command.split(" ")
for c in commandArray:
if (tokens[0] == c.getName()):
try:
c.call(tokens)
except:
print("Failed to complete operation.")
print("Epicurus Editor")
while (True):
parseCommand(input("> "))