forked from huggingface/community-events
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoblack.py
78 lines (61 loc) · 2.6 KB
/
toblack.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
from PIL import Image, ImageEnhance, ImageFilter,ImageOps
import numpy as np
import cv2
def imgprocess(path):
# 打开原始图像
img = Image.open(path)
if img.mode != 'RGB':
img = img.convert('RGB')
img = img.convert('L')
# img = ImageOps.equalize(img, mask=None)
img = img.resize((1536, 1536), resample=Image.BILINEAR)
enhancer = ImageEnhance.Contrast(img)
img_contrast = enhancer.enhance(1.3)
img = img_contrast
# 使用高斯模糊去除一部分细节
img = img.filter(ImageFilter.GaussianBlur(radius=5))
img = img.filter(ImageFilter.BoxBlur(radius=15))
img = img.filter(ImageFilter.GaussianBlur(radius=10))
# 使用中值滤波器去除更多的细节
img = img.filter(ImageFilter.MedianFilter(size=3))
img= img.filter(ImageFilter.SMOOTH)
# 使用边缘增强滤波器保留光影和色块的模糊关系
img = img.filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=1))
img_array = np.array(img)
img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 对图像进行自适应直方图均衡化
clahe = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(8,8))
cl_img = clahe.apply(gray)
# 将图像转换为 Pillow 格式,并显示处理后的图像
img = Image.fromarray(cl_img)
img = img.resize((img.size[0] // 16, img.size[1] // 16), resample=Image.BOX)
img = img.resize((img.size[0] * 16, img.size[1] * 16), resample=Image.NEAREST)
img = img.filter(ImageFilter.GaussianBlur(radius=10))
enhancer = ImageEnhance.Contrast(img)
img_contrast = enhancer.enhance(1.3)
img = img_contrast
img = img.resize((512, 512), resample=Image.BILINEAR)
return img
# 保存处理后的图像
# img.save('filtered_image.jpg')
if __name__ == '__main__':
files = os.listdir()
for filename in files:
# 判断当前文件是否为图片文件
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
# 拼接文件路径
filepath = os.path.join(os.getcwd(), filename)
# 调用函数 A 进行处理
processed_img = imgprocess(filepath)
# 显示处理后的图像
fig, ax = plt.subplots(1, 2,dpi=300)
ax[1].imshow(np.array(processed_img.convert('RGB')))
ax[0].set_title('Original Image')
ax[0].imshow(Image.open(filepath))
ax[1].set_title('Processed Image')
plt.show()