This commit is contained in:
fengbohan 2024-01-12 14:46:42 +08:00
commit 0d71fb70b3
7 changed files with 78 additions and 0 deletions

44
ReadMe.md Normal file
View File

@ -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
```

BIN
color.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 826 KiB

BIN
color.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 826 KiB

BIN
gray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 KiB

32
img.py Normal file
View File

@ -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")

BIN
output.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Pillow==10.1.0
Pillow==10.2.0