-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressor.py
114 lines (84 loc) · 2.91 KB
/
compressor.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
# Compress, scale, and create thumbnails for images
# Note: made very quickly, not supposed to be well writen and comprehensive
import subprocess as sp
import sys
import os
import shutil
from PIL import Image
import argparse
EXCLUDE_EXTS = ['.mp4', '.mov']
EXCLUDE_PATTERN = '_compressed'
MAX_WIDTH = 2500
MAX_HEIGHT = 2500
MAX_THUMB = (600,600)
THUMB_ASPECT_RATIO = 16/9
THUMB_QUALITY = 75
def getFiles(dirr):
files = [f for f in os.listdir(dirr) if os.path.isfile(os.path.join(dirr, f))]
files.sort()
files = [f for f in files if EXCLUDE_PATTERN not in f]
files = [f for f in files if os.path.splitext(f)[1].lower() not in EXCLUDE_EXTS]
print('\nCompressing Input Files: ', files)
files = [os.path.join(dirr, f) for f in files]
return files
def compressImg(img, filename, quality, outDir, maxwidth, infile):
ext = '.webp'
if jpegCmp is True:
ext = '.jpg'
outfile = os.path.join(outDir, filename + '_q' + str(quality) + ext)
if os.path.exists(outfile) == True:
print('Skipping: already exits ', outfile)
return
w,h = img.size
sf = w / h
resize = False
if w > maxwidth:
w = maxwidth
h = w * (1.0/sf)
resize = True
elif w < maxwidth:
print('Img: ', filename, ' too small at: ', img.size, ' needed size: ', maxwidth)
return
w = int(w)
h = int(h)
if jpegCmp is True:
img = img.resize((w,h), Image.ANTIALIAS)
img.save(outfile,"JPEG",optimize=True, quality=quality)
else:
cmd = ['cwebp', '-m', '6', '-q', str(quality)]
if resize is True:
cmd += ['-resize', str(w), str(h)]
cmd += [infile, '-o', outfile]
sp.check_output(cmd)
def main(inDir, outDirAbs, quality):
imgSizes = [3840, 2560, 1920, 1600, 1366]
files = getFiles(inDir)
if outDirAbs is None:
outDirAbs = os.path.join(inDir, 'compressed')
for i in imgSizes:
outDir = os.path.join(outDirAbs, str(i))
if os.path.exists(outDir) == False:
os.makedirs(outDir)
print("Created directory: ", outDir)
for f in files:
img = Image.open(f)
fname = os.path.basename(f)
fname = os.path.splitext(fname)[0]
compressImg(img, fname, quality, outDir, i, f)
if __name__ == "__main__":
argv=sys.argv[1:]
# Quick command line arguments
parser = argparse.ArgumentParser(description='Quick img compressor and thumbnailer')
parser.add_argument('-i', type=str, required=True, help='Input directory. All files in directory will be compressed. Takes priority over -f')
parser.add_argument('-o', '--output', type=str,
help='Output directory. A directory will be created called compressed, along with the subfolder thumbs. If not specified, does this in the input directory.')
parser.add_argument('-q', '--quality', default=85, type=int, help='Quality of compression, 1-100')
parser.add_argument('-j', '--jpeg', action='store_true', help='Do jpeg compression')
args = parser.parse_args(argv)
q = args.quality
inDir = args.i
outDir = None
jpegCmp = args.jpeg
if args.output:
outDir = args.output
main(inDir, outDir, q)