概述
PIL(Python Image Library)
PIL库是一个具有强大图像处理能力的第三方库。
安装
在命令行下执行:
pip install pillow
图像的数组表示及转换
图象是一个由像素组成的二维矩阵,每个元素都是一个RGB值(R, G, B)
test.jpg
>>> from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg"))
>>> print(a.shape, a.dtype)
(960, 640, 3) uint8
由此可见,图象是一个三维数组,维度分别是高度、宽度和像素RGB值~
图像的变换
>>> from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg"))
>>> print(a.shape, a.dtype)
(960, 640, 3) uint8
>>> b = [255, 255, 255] - a # 直接取“反色”
>>> im = Image.fromarray(b.astype('uint8'))
>>> im.save("C:\\Users\\Shinelon\\Desktop\\test2.jpg")
👇test.jpg → test2.jpg
from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg").convert('L')) # 转化成灰度图片
>>> b = 255 - a # 取“反色”
>>> im = Image.fromarray(b.astype('uint8'))
>>> im.save("C:\\Users\\Shinelon\\Desktop\\test3.jpg")
👇test.jpg → test3.jpg
>>> from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg").convert('L'))
>>> b = (100/255)*a + 150 # 区间变换,浅灰度~
>>> im = Image.fromarray(b.astype('uint8'))
>>> im.save("C:\\Users\\Shinelon\\Desktop\\test4.jpg")
👇test.jpg → test4.jpg
>>> from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg").convert('L'))
>>> b = 255 * (a/255)**2 # 像素平方,深色灰度~
>>> im = Image.fromarray(b.astype('uint8'))
>>> im.save("C:\\Users\\Shinelon\\Desktop\\test5.jpg")
👇test.jpg → test5.jpg
手绘风格:
>>> from PIL import Image
>>> import numpy as np
>>> a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg").convert('L')).astype('float')
>>> depth = 10. # 虚拟深度值(0~100)
>>> 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.) # 构造x轴和y轴梯度的三维归一化单位坐标系
>>> 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) # 光源对x轴的影响
>>> dy = np.cos(vec_el)*np.sin(vec_az) # 光源对y轴的影响
>>> dz = np.sin(vec_el) # 光源对z轴的影响
>>> b = 255*(dx*uni_x + dy*uni_y + dz*uni_z) # 光源归一化,梯度于光源相互作用,将梯度还原为灰度
>>> b = b.clip(0,255) # 去噪,防止出错
>>> im = Image.fromarray(b.astype('uint8')) # 重构图像
>>> im.save("C:\\Users\\Shinelon\\Desktop\\testHD.jpg")
👇test.jpg → testHD.jpg
分析:
手绘效果几个特征:1. 黑白灰 2. 边界线条较重 3.相同或相近色彩趋于白色 4.略有光源效果
本质:手绘风格是对图像进行灰度化的基础上,由立体效果和明暗效果叠加而成
换个方位角~
from PIL import Image
import numpy as np
a = np.array(Image.open("C:\\Users\\Shinelon\\Desktop\\test.jpg").convert('L')).astype('float')
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/1. ############## 更换方位角~
dx = np.cos(vec_el)*np.cos(vec_az)
dy = np.sin(vec_el)*np.cos(vec_az)
dz = np.sin(vec_el)
b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)
b = b.clip(0,255)
im = Image.fromarray(b.astype('uint8'))
im.save("C:\\Users\\Shinelon\\Desktop\\testHD2.jpg")