-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgresize.py
More file actions
133 lines (116 loc) · 4.42 KB
/
imgresize.py
File metadata and controls
133 lines (116 loc) · 4.42 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
from PIL import Image, ExifTags
import os
import sys
import re
from datetime import datetime
# set globals
CURDIR = os.getcwd()
DIRTREE = [CURDIR] # result list of directories
MAXSIZE = 1920 # default value of maximum pixel dimesion
NESTING = 0 # max level of directory nesting, no subdirectories by default
REPLACE = False
HELP = False
DIRECTIONS={3 : 180, 6 : 270, 8 : 90} # rotate by {value}-degrees if {key} is found as orientation in exif-metadata
for ORIENTATION in ExifTags.TAGS.keys(): # get 16-bit integer EXIF tag enumeration
if ExifTags.TAGS[ORIENTATION] == 'Orientation':
break
print ('''
_ _
|_|_____ ___ ___ ___ ___|_|___ ___
| | | . | _| -_|_ -| |- _| -_|
|_|_|_|_|_ |_| |___|___|_|___|___| build 20210306
|___|
by error on line 1 (erroronline.one)
resizes all jpg and png files in the calling directory and its children (if given) that extend the maximum allowed dimension
you can paste the script in a top level directory and call it like 'py ../../imgresize.py' to process the current nested dir
terminal use for additional options
$ imgresize --help for overview''')
HELPTEXT= '''
[help]
this program resizes all jpg and png files in the calling directory and its children (if given) that extend the maximum allowed dimension
files that are below the given dimension will be kept untouched
usage: imgresize [ -h | --help ] this message, priority handling
[ -m | --max ] set maximum size in pixel for longer side, {0} by default
[ -n | --nesting ] set maximum nesting for processed subdirectories, {1} by default
[ -r | --replace ] delete original files after processing
processed files lose all metadata. original modified date will be added to the filename.
'''.format(MAXSIZE, NESTING)
def tree(curdir, nesting):
dir = os.listdir(curdir)
nesting -= 1
for file in dir:
if nesting > 0:
if file.find('.') < 1:
DIRTREE.append(curdir + "\\" + file)
tree(curdir + "\\" + file, nesting)
def resize(curdir):
dir = os.listdir(curdir)
for file in dir:
if file.find('.') < 1:
continue
extension = file[file.rindex('.'):].lower()
if extension in ('.jpg', '.png'):
img = Image.open(file)
if img.width > MAXSIZE or img.height > MAXSIZE:
name = file[0:file.rindex('.')]
mtime = os.path.getmtime(file)
try:
exif = dict(img._getexif().items())
if exif[ORIENTATION] in DIRECTIONS:
img = img.rotate(DIRECTIONS[exif[ORIENTATION]], expand = True)
except (AttributeError, KeyError, IndexError):
# cases: image doesn't have getexif-data
pass
if img.width >= img.height:
height = round(MAXSIZE * img.height / img.width)
width = MAXSIZE
else:
width = round(MAXSIZE * img.width / img.height)
height = MAXSIZE
img = img.resize((width, height), Image.ANTIALIAS)
newname = '{0}_{1}x{2}_{3}{4}'.format(name, width, height, datetime.utcfromtimestamp(mtime).strftime('%Y%m%d') , extension)
img.save(newname)
if REPLACE:
os.unlink(file)
# _ _ _ _ _ _
# |_|___|_| |_|_|___| |_|___ ___
# | | | | _| | .'| | |- _| -_|
# |_|_|_|_|_| |_|__,|_|_|___|___|
#
if __name__ == '__main__':
# argument handler
# omit first argument (scriptname)
sys.argv.pop(0)
# find and assign option arguments
options = {
'h': '--help|-h',
'm': '((?:--max|-m)[:\\s]+)(\\d+)',
'n': '((?:--nesting|-n)[:\\s]+)(\\d+)',
'r': '--replace|-r'
}
params = ' '.join(sys.argv) + ' '
for opt in options:
arg = re.findall(options[opt], params, re.IGNORECASE)
if opt == 'h' and arg:
HELP=True
break
elif opt == 'm' and bool(arg):
# not less than 1, default otherwise
MAXSIZE = int(arg[0][1]) if int(arg[0][1]) > 0 else MAXSIZE
params = params.replace(''.join(arg[0]), '')
elif opt == 'n' and bool(arg):
# not less than 1, default otherwise
NESTING = int(arg[0][1]) if int(arg[0][1]) > 0 else NESTING
params = params.replace(''.join(arg[0]), '')
elif opt == 'r' and arg:
REPLACE = True
params = params.replace(''.join(arg[0]), '')
if HELP:
print (HELPTEXT)
else:
confirmation=str(input('\n\nall images in directory "' + CURDIR + '"' + (' and its {0}.-level nested subdirectories'.format(NESTING) if NESTING else '') + ' will be resized to ' + str(MAXSIZE) + 'px maximum width or height! [ y / n ]: ')).lower()
if confirmation == 'y':
tree(CURDIR, NESTING + 1)
for dir in DIRTREE:
resize(dir)
sys.exit()