图像一般是使用RGB模式,是一个三色数组
PIL库
from PIL import Image import numpy as np im = np.array(Image.open("ZSC.jpg")) print(im.shape,im.dtype) print(im)
图像变化(相反)
from PIL import Image import numpy as np a = np.array(Image.open("timg.jpg")) print(a.shape,a.dtype) b = [255,255,255] - a im = Image.fromarray(b.astype('uint8')) im.save('untimg.jpg')
图像变化(灰度)
from PIL import Image import numpy as np a = np.array(Image.open("timg.jpg").convert('L')) print(a.shape,a.dtype) b = 255 - a im = Image.fromarray(b.astype('uint8')) im.save('huitimg.jpg')
图像变化(色彩变淡)
from PIL import Image import numpy as np a = np.array(Image.open("timg.jpg")) print(a.shape,a.dtype) b = (100/255)*a +150 im = Image.fromarray(b.astype('uint8')) im.save('jujitimg.jpg')
图像变化(色彩变深)
from PIL import Image import numpy as np a = np.array(Image.open("timg.jpg")) print(a.shape,a.dtype) b = 255*(a/255)**2 im = Image.fromarray(b.astype('uint8')) im.save('liangtimg.jpg')
图像的手绘效果
手绘特点:
1.黑白灰色系
2.边界较重
3.相同相近的色彩趋近白色
4.有光源
from PIL import Image import numpy as np a = np.asarray(Image.open('shouhui.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/4. #改变这两个弧度值可以调整光源角度 dx = np.cos(vec_el)*np.cos(vec_az) #此处np.cos(vec_el)单位光源在地平面上的投影长度 dy = np.cos(vec_el)*np.sin(vec_az) #xyz是光源对三个方向的影响程度 dz = np.sin(vec_el) b = 255*(dx*uni_x + dy*uni_y + dz*uni_z) #梯度与光源互相作用 b = b.clip(0,255) #处理超过255的像素 im = Image.fromarray(b.astype('uint8')) im.save('sou.jpg')