-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_images.py
42 lines (29 loc) · 1.25 KB
/
optimize_images.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
from PIL import Image
import os
def compress_images(directory=False,outputPath="",quality=1):
# 1. If there is a directory then change into it, else perform the next operations inside of the
# current working directory:
if directory:
os.chdir(directory)
# 2. Extract all of the .png and .jpeg files:
files = os.listdir()
# 3. Extract all of the images:
images = [file for file in files if file.endswith(('jpg', 'png'))]
# 4. Loop over every image:
for image in images:
print(image)
# 5. Open every image:
img = Image.open(image)
# 5. Compress every image and save it with a new name:
img.save(outputPath+image, optimize=True, quality=quality)
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-i", "--inputPath",default="/Users/rachelchen/Desktop/father's web/3rd/")
parser.add_argument("-o", "--outputPath", default="/Users/rachelchen/Desktop/father's web/compressed/")
# the desired number of colors in the compressed image
parser.add_argument("-q", "--quality",default=1)
args = parser.parse_args()
inputPath =args.inputPath
outputPath = args.outputPath
quality =args.quality
compress_images(directory=inputPath, outputPath=outputPath,quality=quality)