33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
from pathlib import Path
|
|
import argparse
|
|
from PIL import Image, ImageOps, ImageEnhance
|
|
|
|
if __name__ == "__main__":
|
|
# get opt
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("img", help="input img file name", type=str)
|
|
group = parser.add_mutually_exclusive_group()
|
|
group.add_argument("-g", "--gray", help="convert color img to gray", action="store_true")
|
|
group.add_argument("-i", "--invert", help="invert color img", action="store_true")
|
|
group.add_argument("-t", "--threshold", help="to produce a black and white image", action="store_true")
|
|
parser.add_argument("-o", "--output", help="output img file name", type=str, default="output.png")
|
|
args = parser.parse_args()
|
|
|
|
if args.gray:
|
|
img = Image.open(args.img)
|
|
img_gray = img.convert('L')
|
|
img_gray.save(args.output)
|
|
elif args.invert:
|
|
img = Image.open(args.img)
|
|
img_invert = ImageOps.invert(img)
|
|
img_invert.save(args.output)
|
|
elif args.threshold:
|
|
img = Image.open(args.img)
|
|
img_gray = img.convert('L')
|
|
enhancer = ImageEnhance.Contrast(img_gray)
|
|
img_gray = enhancer.enhance(2)
|
|
img_bw = img_gray.point(lambda x: 0 if x < 128 else 255, '1')
|
|
img_bw.save(args.output)
|
|
else:
|
|
print("Please run scripts with -g or -i or -t")
|