实例:python图像处理(Pillow+numpy)

(python 3.5)

示例代码:


# -*- coding:utf8 -*-

from PIL import Image
import numpy as np

a = np.array(Image.open("C:/Users/Desktop/***.jpg").convert('L')).astype('float')   #打开目标图片 convert('L'):转换模式为灰度图

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_el = np.pi/2.2
vec_az = np.pi/4.
dx = np.cos(vec_el)*np.cos(vec_az)
dy = np.cos(vec_el)*np.sin(vec_az)
dz = np.sin(vec_el)

b = 255*(dx*uni_x+dy*uni_y+dz*uni_z)
b = b.clip(0, 225)

im = Image.fromarray(b.astype('uint8'))

im.save("C:/Users/Desktop/###.jpg")   #保存图片


效果展示:


实例:python图像处理(Pillow+numpy)_第1张图片


实例:python图像处理(Pillow+numpy)_第2张图片

你可能感兴趣的:(python)