-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageResize.py
More file actions
361 lines (320 loc) · 12.4 KB
/
Copy pathImageResize.py
File metadata and controls
361 lines (320 loc) · 12.4 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
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
__author__ = 'Mark Mon Monteros'
import os, shutil
from psd_tools import PSDImage
from PIL import Image
class ImageResize():
def __init__(self):
self.path = os.path.dirname(os.path.realpath(__file__))
self.source = self.path + '\\psd_pics\\'
self.converted = self.path + '\\converted\\'
self.output = self.path + '\\output\\'
self.psdFiles = list()
self.dpi = [75, 300]
self.x_small_w = float()
self.x_small_h = float()
self.small_w = float()
self.small_h = float()
self.medium_w = float()
self.medium_h = float()
self.large_w = float()
self.large_h = float()
self.x_large_w = float()
self.x_large_h = float()
self.start()
self.convert_psd()
def start(self):
try:
os.mkdir(self.output)
os.mkdir(self.converted)
except FileExistsError:
shutil.rmtree(self.output)
shutil.rmtree(self.converted)
os.mkdir(self.output)
os.mkdir(self.converted)
def convert_psd(self):
print('\nConverting PSD to JPEG...')
for f in os.listdir(self.source):
if f.endswith('.psd'):
self.psdFiles.append(f)
while (True):
try:
dpi_input = int(input("Choose DPI [75] or [300]: "))
except ValueError:
print('Should be a number..')
continue
if dpi_input in self.dpi:
break
else:
print('DPI should be 75 or 300.')
continue
while (True):
try:
quality_input = int(input("Select QUALITY from range [1-100]: "))
except ValueError:
print('Should be a number..')
continue
if quality_input < 1:
continue
elif quality_input > 100:
continue
else:
break
for f in self.psdFiles:
psd = PSDImage.open(self.source + f)
basename = os.path.basename(f)
print(str('[*] -- ' + f))
print(psd.size)
psd.compose().save(os.path.join(self.converted, basename.replace(basename[basename.rindex('.'):], '') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
if dpi_input == 75:
self._75here(dpi_input, quality_input)
if dpi_input == 300:
self._300here(dpi_input, quality_input)
def _75here(self, dpi_input, quality_input):
print('\nSetting image size...')
print(str('\nDPI set to: '), dpi_input)
print(str('QUALITY set to: '), quality_input)
self.set_x_small()
self.set_small()
self.set_medium()
self.set_large()
self.set_x_large()
self.switch(dpi_input, quality_input)
'''
x_small = img.resize((300, 375), Image.ANTIALIAS)
small = img.resize((375, 468), Image.ANTIALIAS)
medium = img.resize((600, 750), Image.ANTIALIAS)
large = img.resize((825, 1031), Image.ANTIALIAS)
x_large = img.resize((1350, 1687), Image.ANTIALIAS)
'''
def _300here(self, dpi_input, quality_input):
print('\nSetting image size...')
print(str('\nDPI set to: '), dpi_input)
print(str('QUALITY set to \n'), quality_input)
self.set_x_small()
self.set_small()
self.set_medium()
self.set_large()
self.set_x_large()
self.switch(dpi_input, quality_input)
'''
x_small = img.resize((1200, 1500), Image.ANTIALIAS)
small = img.resize((1500, 1875), Image.ANTIALIAS)
medium = img.resize((2400, 3000), Image.ANTIALIAS)
large = img.resize((3300, 4125), Image.ANTIALIAS)
x_large = img.resize((5400, 6750), Image.ANTIALIAS)
'''
def switch(self, dpi_input, quality_input):
checker = False
print('\nCheck the sizes below: ')
print(str('\n[1]-- X-SMALL: '), '({}"x {}")'.format(self.x_small_w, self.x_small_h))
print(str('[2] -- SMALL: '), '({}"x {}")'.format(self.small_w, self.small_h))
print(str('[3] -- MEDIUM: '), '({}"x {}")'.format(self.medium_w, self.medium_h))
print(str('[4] -- LARGE: '), '({}"x {}")'.format(self.large_w, self.large_h))
print(str('[5] -- X-LARGE: '), '({}"x {}")'.format(self.x_large_w, self.x_large_h))
try:
num = float(input('\nSelect a number from [1-5] if you want to edit. \nOr press any key when your good! '))
except ValueError:
checker = False
num = float(0)
if num == 1:
checker = True
self.set_x_small()
elif num == 2:
checker = True
self.set_small()
elif num == 3:
checker = True
self.set_medium()
elif num == 4:
checker = True
self.set_large()
elif num == 5:
checker = True
self.set_x_large()
else:
checker = False
if (checker):
self.switch(dpi_input, quality_input)
else:
self.to_pixels(dpi_input, quality_input)
def set_x_small(self):
#X-SMALL - width
while (True):
print('\n+-+- Enter number in inches for X-SMALL size (width, height) -+-+')
try:
self.x_small_w = float(input('width: '))
except ValueError:
print('Should be a number..')
continue
if self.x_small_w <=0 :
print('Width should be greater than 0..')
else:
break
#X-SMALL - height
while (True):
print('\n+-+- Enter number in inches for X-SMALL size (width, height) -+-+')
print(str('width: '), self.x_small_w)
try:
self.x_small_h = float(input('height: '))
except ValueError:
print('Should be a number..')
continue
if self.x_small_h <= 0:
print('Height should be greater than 0..')
continue
else:
break
def set_small(self):
#SMALL - width
while (True):
print('\n+-+- Enter number in inches for SMALL size (width, height) -+-+')
try:
self.small_w = float(input('width: '))
except ValueError:
print('Should be a number..')
continue
if self.small_w <=0 :
print('Width should be greater than 0..')
else:
break
#SMALL - height
while (True):
print('\n+-+- Enter number in inches for SMALL size (width, height) -+-+')
print(str('width: '), self.small_w)
try:
self.small_h = float(input('height: '))
except ValueError:
print('Should be a number..')
continue
if self.small_h <=0 :
print('Height should be greater than 0..')
else:
break
def set_medium(self):
#MEDIUM - width
while (True):
print('\n+-+- Enter number in inches for MEDIUM size (width, height) -+-+')
try:
self.medium_w = float(input('width: '))
except ValueError:
print('Should be a number..')
continue
if self.medium_w <= 0:
print('Width should be greater than 0..')
else:
break
#MEDIUM - height
while (True):
print('\n+-+- Enter number in inches for MEDIUM size (width, height) -+-+')
print(str('width: '), self.medium_w)
try:
self.medium_h = float(input('height: '))
except ValueError:
print('Should be a number..')
continue
if self.medium_h <= 0:
print('Height should be greater than 0..')
else:
break
def set_large(self):
#LARGE - width
while (True):
print('\n+-+- Enter number in inches for LARGE size (width, height) -+-+')
try:
self.large_w = float(input('width: '))
except ValueError:
print('Should be a number..')
continue
if self.large_w <= 0:
print('Width should be greater than 0..')
else:
break
#LARGE - height
while (True):
print('\n+-+- Enter number in inches for LARGE size (width, height) -+-+')
print(str('width: '), self.large_w)
try:
self.large_h = float(input('height: '))
except ValueError:
print('Should be a number..')
continue
if self.large_h <= 0:
print('Height should be greater than 0..')
else:
break
def set_x_large(self):
#X-LARGE - width
while (True):
print('\n-+- Enter number in inches for X-LARGE size (width, height) -+-+')
try:
self.x_large_w = float(input('width: '))
except ValueError:
print('Should be a number..')
continue
if self.x_large_w <- 0:
print('Width should be greater than 0..')
else:
break
#X-LARGE - height
while (True):
print('\n-+- Enter number in inches for X-LARGE size (width, height) -+-+')
print(str('width: '), self.x_large_w)
try:
self.x_large_h = float(input('height: '))
except ValueError:
print('Should be a number..')
continue
if self.x_large_h <= 0:
print('Height should be greater than 0..')
else:
break
def to_pixels(self, dpi_input, quality_input):
print('\nResizing image...')
#Formula: Inches X DPI = Pixels
self.x_small_w = int(self.x_small_w * dpi_input)
self.x_small_h = int(self.x_small_h * dpi_input)
self.small_w = int(self.small_w * dpi_input)
self.small_h = int(self.small_h * dpi_input)
self.medium_w = int(self.medium_w * dpi_input)
self.medium_h = int(self.medium_h * dpi_input)
self.large_w = int(self.large_w * dpi_input)
self.large_h = int(self.large_h * dpi_input)
self.x_large_w = int(self.x_large_w * dpi_input)
self.x_large_h = int(self.x_large_h * dpi_input)
for f in os.listdir(self.converted):
if f.endswith('.jpg'):
img = Image.open(self.converted + f)
basename = os.path.basename(f)
x_small = img.resize((self.x_small_w, self.x_small_h), Image.ANTIALIAS)
small = img.resize((self.small_w, self.small_h), Image.ANTIALIAS)
medium = img.resize((self.medium_w, self.medium_h), Image.ANTIALIAS)
large = img.resize((self.large_w, self.large_h), Image.ANTIALIAS)
x_large = img.resize((self.x_large_w, self.x_large_h), Image.ANTIALIAS)
print(str('[*] -- ' + f), str('@'), '({} x {})'.format(x_small.width, x_small.height), str('--'), dpi_input, str('DPI'), '<{} quality>'.format(quality_input))
print(str('[*] -- ' + f), str('@'), '({} x {})'.format(small.width, small.height), str('--'), dpi_input, str('DPI'), '<{} quality>'.format(quality_input))
print(str('[*] -- ' + f), str('@'), '({} x {})'.format(medium.width, medium.height), str('--'), dpi_input, str('DPI'), '<{} quality>'.format(quality_input))
print(str('[*] -- ' + f), str('@'), '({} x {})'.format(large.width, large.height), str('--'), dpi_input, str('DPI'), '<{} quality>'.format(quality_input))
print(str('[*] -- ' + f), str('@'), '({} x {})'.format(x_large.width, x_large.height), str('--'), dpi_input, str('DPI'), '<{} quality>'.format(quality_input))
if dpi_input == 75:
x_small.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (75dpi)_x-small') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
small.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (75dpi)_small') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
medium.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (75dpi)_medium') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
large.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (75dpi)_large') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
x_large.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (75dpi)_x-large') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
if dpi_input == 300:
x_small.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (300dpi)_x-small') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
small.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (300dpi)_small') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
medium.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (300dpi)_medium') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
large.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (300dpi)_large') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
x_large.save(os.path.join(self.output, basename.replace(basename[basename.rindex('.'):], ' (300dpi)_x-large') + '.jpg'), dpi=(dpi_input, dpi_input), quality=quality_input)
if __name__ == '__main__':
print('\nIMAGE RESIZE AUTOMATION')
print('\nCreated by: ' + __author__)
print('\nINSTRUCTIONS:')
print('[*] -- Create a folder named "psd_pics", make sure this folder and the application are both in the same directory.')
print('[*] -- Place .PSD image/s inside psd_pics folder.')
print('[*] -- Execute the application.')
print('[*] -- A folder named "converted" is created where the converted image/s from PSD to JPEG are stored.')
print('[*] -- A folder named "output" is created where the resized image/s are stored.')
ImageResize()
print('\nDONE!!!\n')