aifeifei798 commited on
Commit
e1dcd59
·
verified ·
1 Parent(s): de2709c

Upload feifeifilter.py

Browse files
Files changed (1) hide show
  1. feifeilib/feifeifilter.py +67 -0
feifeilib/feifeifilter.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # feifeilib/feifeifilter.py
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ def validate_image(img):
7
+ """确保图像为3通道uint8格式"""
8
+ if img.dtype != np.uint8:
9
+ img = img.astype(np.uint8)
10
+ if len(img.shape) == 2:
11
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
12
+ elif img.shape[2] == 4:
13
+ img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
14
+ return img
15
+
16
+ def japanese_style(
17
+ input_img,
18
+ brightness=15,
19
+ blue_intensity=1.1,
20
+ softlight_strength=0.2,
21
+ sharpen_strength=9
22
+ ):
23
+ """日系风格处理函数(已修复参数接收问题)"""
24
+ # 参数类型转换(Gradio的Slider返回float类型)
25
+ brightness = int(brightness)
26
+ blue_intensity = float(blue_intensity)
27
+ softlight_strength = float(softlight_strength)
28
+ sharpen_strength = int(sharpen_strength)
29
+
30
+ # 格式转换
31
+ img = np.array(input_img)
32
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
33
+ img = validate_image(img)
34
+
35
+ # ===== 基础调整 =====
36
+ img = cv2.convertScaleAbs(img, alpha=1.1, beta=brightness)
37
+
38
+ # ===== 色调调整 =====
39
+ b, g, r = cv2.split(img)
40
+ b = np.clip(b * blue_intensity, 0, 255).astype(np.uint8)
41
+ g = np.clip(g * 1.05, 0, 255).astype(np.uint8)
42
+ img = cv2.merge((b, g, r))
43
+
44
+ # ===== 柔光效果 =====
45
+ overlay = img.copy()
46
+ cv2.rectangle(overlay, (0,0), (img.shape[1], img.shape[0]),
47
+ (210,230,250), -1)
48
+ img = cv2.addWeighted(overlay, softlight_strength,
49
+ img, 1-softlight_strength, 0)
50
+
51
+ # ===== 曲线调整 =====
52
+ lut = np.array([int(255 * (0.5 * np.sin((x/255 - 0.5)*np.pi) + 0.5))
53
+ for x in range(256)])
54
+ img = cv2.LUT(img, lut)
55
+
56
+ # 锐化前再次验证格式
57
+ img = validate_image(img)
58
+
59
+ # ===== 锐化处理 =====
60
+ # kernel = np.array([[-1,-1,-1],
61
+ # [-1,sharpen_strength,-1],
62
+ # [-1,-1,-1]])
63
+ # img = cv2.filter2D(img, cv2.CV_8U, kernel)
64
+
65
+ # 转回PIL格式
66
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
67
+ return Image.fromarray(img)