-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcropper.py
308 lines (253 loc) · 11.2 KB
/
cropper.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
#!/usr/bin/env python3
import sys
import os
import pygame
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from PIL import Image # install the "Pillow" package to use PIL
from enum import Enum, auto
# TODO: better handling of images that can't be read
# disable DoS warning
Image.MAX_IMAGE_PIXELS = None
DEFAULT_WIDTH = "600"
DEFAULT_HEIGHT = "448"
class M:
"""
Display (M)odes supported by the program.
"""
FILL = auto()
H_CENTER = auto()
V_CENTER = auto()
LEFT = auto()
RIGHT = auto()
UP = auto()
DOWN = auto()
class D:
"""
Describes horizontal and vertical (D)irections
"""
HORIZONTAL = auto()
VERTICAL = auto()
class FolderCropper:
def __init__(self):
self.sourceFolder = None
self.destFolder = None
self.targetWidth = None
self.targetHeight = None
window = tk.Tk()
window.title("cropper")
window.minsize(450, 150)
label1 = ttk.Label(window, text="Source Folder: ")
label1.grid(column=0, row=1)
label2 = ttk.Label(window, text="Destination Folder: ")
label2.grid(column=0, row=2)
label3 = ttk.Label(window, text="Width: ")
label3.grid(column=0, row=3)
label4 = ttk.Label(window, text="Height: ")
label4.grid(column=0, row=4)
def setSourceFolder():
self.sourceFolder = filedialog.askdirectory(initialdir=sys.argv[0])
sourceFolderField.config(state="normal")
sourceFolderField.delete(0, tk.END)
sourceFolderField.insert(0, self.sourceFolder)
sourceFolderField.config(state="readonly")
return
def setDestFolder():
self.destFolder = filedialog.askdirectory(initialdir=sys.argv[0])
destFolderField.config(state="normal")
destFolderField.delete(0, tk.END)
destFolderField.insert(0, self.destFolder)
destFolderField.config(state="readonly")
return
def runCommand():
try:
self.sourceFolder = sourceFolderField.get()
self.destFolder = destFolderField.get()
self.targetHeight = int(heightTextField.get())
self.targetWidth = int(widthTextField.get())
assert self.sourceFolder != ""
assert self.destFolder != ""
except Exception as e:
print(e)
messagebox.showwarning("Error", "Invalid input.")
return
window.destroy()
self.cropAllImages()
sourceFolderField = ttk.Entry(window, state="readonly")
sourceFolderField.grid(column=1, row=1)
destFolderField = ttk.Entry(window, state="readonly")
destFolderField.grid(column=1, row=2)
widthTextField = ttk.Entry(window, width=6)
widthTextField.insert(0, DEFAULT_WIDTH)
widthTextField.grid(column=1, row=3)
heightTextField = ttk.Entry(window, width=6)
heightTextField.insert(0, DEFAULT_HEIGHT)
heightTextField.grid(column=1, row=4)
sourceButton = ttk.Button(window, text="Browse...", command=lambda: setSourceFolder())
sourceButton.grid(column=2, row=1)
destButton = ttk.Button(window, text="Browse...", command=lambda: setDestFolder())
destButton.grid(column=2, row=2)
runButton = ttk.Button(window, text="Run", command=lambda: runCommand())
runButton.grid(column=2, row=5)
window.mainloop()
exit(0)
def cropAllImages(self):
imagesToProcess = []
for filename in os.listdir(self.sourceFolder):
filePath = os.path.join(self.sourceFolder, filename)
if os.path.isfile(filePath) and filePath[-4:].lower() in [".png", ".jpg"]:
imagesToProcess.append((filePath, os.path.join(self.destFolder, filename)))
for pair in imagesToProcess:
if not os.path.exists(pair[1]):
ic = ImageCropper(pair[0])
ic.interactive_crop(pair[1], self.targetWidth, self.targetHeight)
else:
print("{} has been skipped because {} already exists".format(pair[0], pair[1]))
exit(0)
class ImageCropper:
def __init__(self, imagePath):
self.imagePath = imagePath
self.image = Image.open(imagePath)
self.savePath = None
self.targetWidth = None
self.targetHeight = None
# print("Opened Image of size {}x{}".format(self.image.width, self.image.height))
def interactive_crop(self, savePath, targetWidth, targetHeight):
self.savePath = savePath
self.targetWidth = targetWidth
self.targetHeight = targetHeight
wfactor = self.image.width / targetWidth
hfactor = self.image.height / targetHeight
if (wfactor / hfactor) > 1:
# the height doesn't need cropping
cropMode = D.HORIZONTAL
displayHeight = targetHeight
displayWidth = displayHeight * self.image.width // self.image.height
cropMargin = (displayWidth - targetWidth) // 2
# the image has to be filled vertically to expand it
fillMode = D.VERTICAL
fillWidth = targetWidth
fillHeight = fillWidth * self.image.height // self.image.width
fillMargin = (targetHeight - fillHeight) // 2
else:
# the width doesn't need cropping
cropMode = D.VERTICAL
displayWidth = targetWidth
displayHeight = displayWidth * self.image.height // self.image.width
cropMargin = (displayHeight - targetHeight) // 2
# the image has to be filled horizontally to expand it
fillMode = D.HORIZONTAL
fillHeight = targetHeight
fillWidth = fillHeight * self.image.width // self.image.height
fillMargin = (targetWidth - fillWidth) // 2
pygame.init()
pygame.display.set_caption('cropper')
clock = pygame.time.Clock()
pg_image = pygame.image.load(self.imagePath)
crop_image = pygame.transform.scale(pg_image, (displayWidth, displayHeight))
fill_image = pygame.transform.scale(pg_image, (fillWidth, fillHeight))
screen = pygame.display.set_mode(size=(displayWidth, displayHeight))
screen.fill((0, 0, 0))
frames = 0
mouseDown = False
rectColor = [255, 0, 0]
color = (0, 0, 0)
displayMode = M.V_CENTER if cropMode == D.VERTICAL else M.H_CENTER
while True:
frames += 1
if frames > 5:
frames = 0
rectColor = [255, 0, 0] if rectColor == [255, 255, 0] else [255, 255, 0]
# render the preview
if displayMode == M.FILL:
screen.fill(color)
if fillMode == D.VERTICAL:
imagePosition = (0, fillMargin)
else:
imagePosition = (fillMargin, 0)
screen.blit(fill_image, imagePosition)
else:
screen.blit(crop_image, [0, 0])
if displayMode == M.H_CENTER:
imagePosition = (cropMargin, 0)
elif displayMode == M.LEFT:
imagePosition = (0, 0)
elif displayMode == M.RIGHT:
imagePosition = (2 * cropMargin, 0)
elif displayMode == M.V_CENTER:
imagePosition = (0, cropMargin)
elif displayMode == M.UP:
imagePosition = (0, 0)
elif displayMode == M.DOWN:
imagePosition = (0, 2 * cropMargin)
else:
imagePosition = (0, 0)
pygame.draw.rect(screen, rectColor, [imagePosition[0], imagePosition[1],
targetWidth, targetHeight], width=3)
# handle events
oldDisplayMode = displayMode
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if displayMode == M.FILL:
self.save((fillWidth, fillHeight), imagePosition, color)
else:
self.save((displayWidth, displayHeight),
(-imagePosition[0], -imagePosition[1]))
pygame.quit()
return
elif event.key == pygame.K_ESCAPE:
return
elif event.key == pygame.K_RIGHT:
if cropMode == D.HORIZONTAL:
displayMode = M.H_CENTER if displayMode == M.LEFT else M.RIGHT
else:
displayMode = M.V_CENTER
elif event.key == pygame.K_LEFT:
if cropMode == D.HORIZONTAL:
displayMode = M.H_CENTER if displayMode == M.RIGHT else M.LEFT
else:
displayMode = M.V_CENTER
elif event.key == pygame.K_UP:
if cropMode == D.VERTICAL:
displayMode = M.V_CENTER if displayMode == M.DOWN else M.UP
else:
displayMode = M.H_CENTER
elif event.key == pygame.K_DOWN:
if cropMode == D.VERTICAL:
displayMode = M.V_CENTER if displayMode == M.UP else M.DOWN
else:
displayMode = M.H_CENTER
elif event.key == pygame.K_w:
displayMode = M.FILL
color = (255, 255, 255)
elif event.key == pygame.K_b:
displayMode = M.FILL
color = (0, 0, 0)
if event.type == pygame.MOUSEBUTTONUP:
mouseDown = False
position = pygame.mouse.get_pos()
color = screen.get_at(position)
displayMode = M.FILL
if event.type == pygame.MOUSEBUTTONDOWN:
mouseDown = True
if displayMode == M.FILL and mouseDown is True:
position = pygame.mouse.get_pos()
color = screen.get_at(position)
pygame.display.flip()
clock.tick(20)
# set new display mode only if the mode changed
if displayMode == M.FILL and oldDisplayMode != M.FILL:
screen = pygame.display.set_mode(size=(targetWidth, targetHeight))
if displayMode != M.FILL and oldDisplayMode == M.FILL:
screen = pygame.display.set_mode(size=(displayWidth, displayHeight))
def save(self, imageSize, imagePosition, color=(0, 0, 0)):
self.image = self.image.resize(imageSize, Image.LANCZOS)
output = Image.new("RGB", (self.targetWidth, self.targetHeight), color[:3])
output.paste(self.image, imagePosition)
output.save(self.savePath)
if __name__ == "__main__":
fc = FolderCropper()