-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanti-aliasing.py
More file actions
executable file
·62 lines (48 loc) · 1.53 KB
/
anti-aliasing.py
File metadata and controls
executable file
·62 lines (48 loc) · 1.53 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
#encoding=utf-8
import os
from os.path import exists
from PIL import Image
import numpy as np
import cv2
import argparse
def anti_aliasing(image, strength=2):
"""使用高斯模糊來減少鋸齒效果"""
ksize = max(1, strength * 2 + 1) # 確保奇數大小的核心
blurred = cv2.GaussianBlur(image, (ksize, ksize), 0)
return blurred
def main():
parser = argparse.ArgumentParser(description='anti aliasing')
parser.add_argument("--input",
help="input font file",
required=True,
type=str)
parser.add_argument("--output",
help="output font folder",
default="output.png",
type=str)
parser.add_argument("--strength",
help="鋸齒效果, from: 0 to N",
default=2,
type=int)
parser.add_argument("--threshold",
help="binary threshold value",
default=127,
type=int)
args = parser.parse_args()
if not exists(args.input):
print("image file not found:", args.input)
else:
# 讀取影像
image = cv2.imread(args.input)
# 應用去鋸齒
img_rgb = anti_aliasing(image, args.strength)
# to binary
ret, img_rgb = cv2.threshold(img_rgb, args.threshold, 255, cv2.THRESH_BINARY)
# conver to gray
#img_rgb = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# 儲存結果
cv2.imwrite(args.output, img_rgb)
print(f"去鋸齒處理完成,結果已儲存至 {args.output}")
if __name__ == '__main__':
main()