commit 0d71fb70b35dbfe447d0d44b2d6fd8779ee88455 Author: fengbohan <1953356163@qq.com> Date: Fri Jan 12 14:46:42 2024 +0800 init diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..440a94f --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,44 @@ +## 功能列表 + +- 生成灰度图 +- 生成黑白图 +- 图片反色 + +## 快速开始 + +1. 安装依赖 + + ```bash + pip install -r requirements.txt + ``` + +2. 运行程序 + + ```bash + # 黑白 + python .\img.py .\color.png -t + + # 灰度 + python .\img.py .\color.png -g + + # 反色 + python .\img.py .\color.png -i + ``` + + +## 用法说明 + +```bash +usage: img.py [-h] [-g | -i | -t] [-o OUTPUT] img + +positional arguments: + img input img file name + +options: + -h, --help show this help message and exit + -g, --gray convert color img to gray + -i, --invert invert color img + -t, --threshold to produce a black and white image + -o OUTPUT, --output OUTPUT + output img file name +``` \ No newline at end of file diff --git a/color.jpg b/color.jpg new file mode 100644 index 0000000..10789ab Binary files /dev/null and b/color.jpg differ diff --git a/color.png b/color.png new file mode 100644 index 0000000..10789ab Binary files /dev/null and b/color.png differ diff --git a/gray.png b/gray.png new file mode 100644 index 0000000..84bb903 Binary files /dev/null and b/gray.png differ diff --git a/img.py b/img.py new file mode 100644 index 0000000..706d587 --- /dev/null +++ b/img.py @@ -0,0 +1,32 @@ +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") diff --git a/output.png b/output.png new file mode 100644 index 0000000..2830d74 Binary files /dev/null and b/output.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e269771 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Pillow==10.1.0 +Pillow==10.2.0