用PIL制作手绘风格滤镜
话不多说,直接上代码!
from PIL import Image
import numpy as np
def ArtDraw(path0,path1):
try:
a = np.asarray(Image.open(path0).convert('L')).astype('float')
print("图片的大小为:{},图片的RGB值为:{}".format(a.shape,a.dtype))
depth = 10.
grad = np.gradient(a)
grad_x, grad_y = grad
grad_x = grad_x * depth / 100
grad_y = grad_y * depth / 100
A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
uni_x = grad_x / A
uni_y = grad_y / A
uni_z = 1. / A
vec_e1 = np.pi / 2.2
vec_e2 = np.pi / 4.
dx = np.cos(vec_e1) * np.cos(vec_e2)
dy = np.cos(vec_e1) * np.sin(vec_e2)
dz = np.sin(vec_e1)
b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z)
b = b.clip(0, 255)
im = Image.fromarray(b.astype('uint8'))
im.save(path1)
print("转换成功!")
except:
print("转换失败!")
def main():
path0 = input("请输入原片的路径:")
path1 = input("请输入保存的路径:")
ArtDraw(path0,path1)
main()
运行结果如下: