-
Notifications
You must be signed in to change notification settings - Fork 0
/
用numpy实现滤镜效果.py
57 lines (50 loc) · 1.71 KB
/
用numpy实现滤镜效果.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
# 滤镜套装
# -*- coding: utf-8 -*-
from PIL import Image
import numpy as np
import os
filePath="violet.png"
img = Image.open(filePath)
rgb = np.asarray(img, dtype='int')
r = rgb[:, :, 0]
g = rgb[:, :, 1]
b = rgb[:, :, 2]
a = rgb[:, :, 3]
# 1:绿色
F = np.array([[[1, 0, 0, 0, 50],
[0, 2, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0]],
# 2:紫色
[[1, 0, 0, 0, 50],
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[0, 0, 0, 1, 0]],
# 3:黄色
[[2, 0, 0, 0, 0],
[0, 1, 0, 0, 50],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0]],
# 4:红色
[[2, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0]],
# 5:橙色
[[2, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0]]])
def filter(num):
newr = F[num - 1][0][0] * r + F[num - 1][0][1] * g + F[num - 1][0][2] * b + F[num - 1][0][3] * a + F[num - 1][0][4]
newg = F[num - 1][1][0] * r + F[num - 1][1][1] * g + F[num - 1][1][2] * b + F[num - 1][1][3] * a + F[num - 1][1][4]
newb = F[num - 1][2][0] * r + F[num - 1][2][1] * g + F[num - 1][2][2] * b + F[num - 1][2][3] * a + F[num - 1][2][4]
newa = F[num - 1][3][0] * r + F[num - 1][3][1] * g + F[num - 1][3][2] * b + F[num - 1][3][3] * a + F[num - 1][3][4]
y, x = r.shape
im = Image.new("RGBA", (x, y))
for j in range(0, x):
for i in range(0, y):
# rgb转化为像素
im.paste(((int(newr[i, j]), int(newg[i, j]), int(newb[i, j]), int(newa[i, j]))), (j, i, j + 1, i + 1))
im.save("result.png")
filter(5)