-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·392 lines (365 loc) · 14.3 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/python3
import pygame
from sys import argv
from textbox import TextBox
from notify import NotifyBar
pygame.font.init()
path = argv[1]
clock = pygame.time.Clock()
try:
image = pygame.image.load(path)
width = image.get_width()
height = image.get_height()
print("Image loaded")
except pygame.error:
print("Editing new image")
width = int(input("Width: "))
height = int(input("Height: "))
image = pygame.Surface((width, height))
if width > 1120:
width = 1120
if height > 630:
height = 630
s = pygame.display.set_mode((width, height))
s.blit(image, (0, 0))
pygame.display.set_caption(argv[1])
update = pygame.display.update
penColor = pygame.Color(255, 0, 0)
zoomLevel = 1.0
offset = [0, 0]
undoStack = []
redoStack = []
def getZoomed(surface, scale):
width = float(surface.get_width()) * scale
height = float(surface.get_height()) * scale
return pygame.transform.scale(surface, (int(width), int(height)))
oldPos = (0, 0)
arrows = 0
radius = 1
movepr = 10
crop = False
notifics = NotifyBar(12)
def parseHex(hexStr):
# assume rrggbb
r = int(hexStr[0:2], 16)
g = int(hexStr[2:4], 16)
b = int(hexStr[4:6], 16)
return r, g, b
def getRectFromPoints(point1, point2):
p1x, p1y = point1
p2x, p2y = point2
if p2x < p1x:
left = p2x
else:
left = p1x
if p2y < p1y:
top = p2y
else:
top = p1y
width = abs(p1x - p2x)
height = abs(p1y - p2y)
left = int(left * zoomLevel)
top = int(top * zoomLevel)
width = int(width * zoomLevel)
height = int(height * zoomLevel)
return pygame.Rect(left, top, width, height)
step = 0
def incStep(val=1.0):
global step
step += val
step = step % 12
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
elif event.type == pygame.VIDEORESIZE:
size = event.dict['size']
width, height = size
s = pygame.display.set_mode(size)
elif event.type == pygame.KEYDOWN:
key = event.key
if key == pygame.K_EQUALS: # plus sign on equals key
zoomLevel += 0.25
notifics.addNotification("zoom: " + str(zoomLevel * 100) + "%",
500)
# print(zoomLevel)
elif key == pygame.K_MINUS:
zoomLevel -= 0.25
if zoomLevel <= 0:
zoomLevel = 0.25
notifics.addNotification("zoom: " + str(zoomLevel * 100) + "%",
500)
# print(zoomLevel)
elif key == pygame.K_UP:
offset[1] += movepr * zoomLevel
elif key == pygame.K_DOWN:
offset[1] -= movepr * zoomLevel
elif key == pygame.K_LEFT:
offset[0] += movepr * zoomLevel
elif key == pygame.K_RIGHT:
offset[0] -= movepr * zoomLevel
elif key == pygame.K_c:
done = False
colorInput = TextBox("hex: ", (width, 16), 12)
while not done:
hexColor = colorInput.input()
if hexColor is not None:
R, G, B = parseHex(hexColor)
done = True
filled = colorInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
penColor = pygame.Color(R, G, B)
elif key == pygame.K_r:
radInput = TextBox("radius: ", (width, 16), 12)
done = False
while not done:
radSize = radInput.input()
if radSize is not None:
radius = int(radSize)
done = True
filled = radInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
elif key == pygame.K_k:
crop = not crop
notifics.addNotification("crop: " + ("on" if crop else "off"),
2000)
elif key == pygame.K_t:
done = False
textInput = TextBox("text: ", (width, 16), 12)
while not done:
text = textInput.input()
if text is not None:
done = True
filled = textInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
sizeInput = TextBox("size: ", (width, 16), 12)
done = False
while not done:
size = sizeInput.input()
if size is not None:
size = int(size)
done = True
filled = sizeInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
hexInput = TextBox("hex: ", (width, 16), 12)
done = False
while not done:
color = hexInput.input()
if color is not None:
color = parseHex(color)
done = True
filled = hexInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
notifics.addNotification("location", 10)
notificsBar = notifics.getBar()
textfont = pygame.font.SysFont("Courier", size)
renderedText = textfont.render(text, True, color)
undoStack.append(image.copy())
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
mousePos = pygame.mouse.get_pos()
boundsRect = pygame.Rect(mousePos[0], mousePos[1], 0, 0)
boundsRect.width = renderedText.get_width() * zoomLevel
boundsRect.height = renderedText.get_height() * zoomLevel
s.fill((0, 0, 0))
s.blit(getZoomed(image, zoomLevel), offset)
s.blit(notificsBar, (1, height - notificsBar.get_height()))
pygame.draw.rect(s, (255, 255, 255), boundsRect, 2)
if pygame.mouse.get_pressed()[0]:
done = True
update()
incStep()
clock.tick(24)
mousePos = (
int(float(mousePos[0] - offset[0]) / zoomLevel),
int(float(mousePos[1] - offset[1]) / zoomLevel)
)
oldPos = mousePos
image.blit(renderedText, mousePos)
notifics.addNotification("text added", 750)
elif key == pygame.K_z:
if len(undoStack) > 0:
redoStack.append(image.copy())
image = undoStack.pop()
notifics.addNotification("undone", 1000)
else:
notifics.addNotification("nothing to undo", 1000)
elif key == pygame.K_y:
if len(redoStack) > 0:
undoStack.append(image.copy())
image = redoStack.pop()
notifics.addNotification("redone", 1000)
else:
notifics.addNotification("nothing to redo", 1000)
elif key == pygame.K_f:
undoStack.append(image.copy())
image = pygame.transform.flip(image, False, True)
notifics.addNotification("flipped", 500)
elif key == pygame.K_ESCAPE:
saveInput = TextBox("save as \"" + path + "\"? [y/n/c]: ",
(width, 16), 12)
done = False
while not done:
response = saveInput.input()
if response is not None:
done = True
filled = saveInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
print("done")
if response == "y":
pygame.image.save(image, path)
print("saved")
elif response == "n":
if "\\" in path:
while "\\" != path[-1]:
path = path[:-1]
saveInput = TextBox("save as: ", (width, 16), 12)
done = False
saveInput.string = path
while not done:
pathResponse = saveInput.input()
if pathResponse is not None:
done = True
filled = saveInput.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
clock.tick(24)
pygame.image.save(image, pathResponse)
print("saved")
else:
raise SystemExit
raise SystemExit
keys = pygame.key.get_pressed()
if (keys[pygame.K_UP] or keys[pygame.K_DOWN] or keys[pygame.K_RIGHT]
or keys[pygame.K_LEFT]):
arrows += 1
else:
arrows = 0
if arrows > 24:
arrows -= 1
if keys[pygame.K_UP]:
offset[1] += movepr * zoomLevel
if keys[pygame.K_DOWN]:
offset[1] -= movepr * zoomLevel
if keys[pygame.K_LEFT]:
offset[0] += movepr * zoomLevel
if keys[pygame.K_RIGHT]:
offset[0] -= movepr * zoomLevel
mousePos = pygame.mouse.get_pos()
mousePos = (
int(float(mousePos[0] - offset[0]) / zoomLevel),
int(float(mousePos[1] - offset[1]) / zoomLevel)
)
if pygame.mouse.get_pressed()[0]:
undoStack.append(image.copy())
if crop:
if mousePos == oldPos:
undoStack.pop()
else:
baseImage = getZoomed(image, zoomLevel)
startPos = mousePos
while pygame.mouse.get_pressed()[0]:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
mousePos = pygame.mouse.get_pos()
mousePos = (
int(float(mousePos[0] - offset[0]) / zoomLevel),
int(float(mousePos[1] - offset[1]) / zoomLevel)
)
cropRect = getRectFromPoints(startPos, mousePos)
rectColor = (255, 255, 255)
if int(step) % 2:
rectColor = (0, 0, 0)
pygame.draw.rect(baseImage, rectColor, cropRect, 2)
s.blit(baseImage, offset)
update()
incStep(0.5)
clock.tick(24)
baseImage = getZoomed(image, zoomLevel)
realWidth = round(
float(cropRect.width + (offset[0] * 0)) / zoomLevel
)
realHeight = round(
float(cropRect.height + (offset[1] * 0)) / zoomLevel
)
newImage = pygame.Surface((realWidth, realHeight))
realTop = round(float(cropRect.top) / zoomLevel)
realLeft = round(float(cropRect.left) / zoomLevel)
realCropRect = pygame.Rect(
realLeft,
realTop,
realWidth,
realHeight
)
newImage.blit(image, (0, 0), area=realCropRect)
cropPrompt = TextBox("crop? [y/n]: ", (width, 16), 12)
done = False
while not done:
response = cropPrompt.input()
if response is not None:
done = True
filled = cropPrompt.getFilled()
s.blit(filled, (0, height - filled.get_height()))
update()
incStep()
clock.tick(24)
if response == "y":
image = newImage
width = image.get_width()
height = image.get_height()
if width > 1120:
width = 1120
if height > 630:
height = 630
s = pygame.display.set_mode((width, height))
notifics.addNotification("cropped", 750)
zoomLevel = 1.0
offset = [0, 0]
else:
notifics.addNotification("canceled", 1500)
else:
if mousePos == oldPos:
image.set_at(mousePos, penColor)
else:
while pygame.mouse.get_pressed()[0]:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
mousePos = pygame.mouse.get_pos()
mousePos = (
int(float(mousePos[0] - offset[0]) / zoomLevel),
int(float(mousePos[1] - offset[1]) / zoomLevel)
)
pygame.draw.line(image, penColor, oldPos, mousePos, radius)
s.fill((0, 0, 0))
zoomedImage = getZoomed(image, zoomLevel)
s.blit(zoomedImage, offset)
update()
oldPos = mousePos
incStep()
clock.tick(60)
s.fill((0, 0, 0))
zoomedImage = getZoomed(image, zoomLevel)
s.blit(zoomedImage, offset)
notificsBar = notifics.getBar()
s.blit(notificsBar, (1, height - notificsBar.get_height()))
update()
oldPos = mousePos
incStep()
clock.tick(24)