-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-testimages-pillow.py
42 lines (37 loc) · 1.11 KB
/
generate-testimages-pillow.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
#! /usr/bin/env python3
"""
Generate test images at different JPEG quality levels
"""
import os
import argparse
from PIL import Image
def parseCommandLine():
"""Parse command line"""
parser = argparse.ArgumentParser()
parser.add_argument('imageIn',
action="store",
type=str,
help="input image")
parser.add_argument('dirOut',
action="store",
type=str,
help="output directory")
# Parse arguments
args = parser.parse_args()
return args
def main():
args = parseCommandLine()
imageIn = args.imageIn
dirOut = args.dirOut
nameBase = os.path.splitext(os.path.basename(imageIn))[0]
with open(imageIn, 'rb') as fIn:
im = Image.open(fIn)
im.load()
im = im.convert('RGB')
for i in [5, 10, 25, 50, 75, 100]:
nameOut = ("{}{}{}.jpg".format(nameBase, '_pil_', f'{i:03}'))
fileOut = os.path.join(dirOut, nameOut)
im2 = im
im2.save(fileOut, quality=i)
if __name__ == "__main__":
main()