numpy对图像的处理演示
pip3 install pillow
from PIL import Image import numpy as np im=np.array(Image.open('timg.jpg')) print(im.shape,im.dtype)
from PIL import Image import numpy as np im=np.array(Image.open('timg.jpg')) print(im.shape,im.dtype) b=[255,255,255]-im a=Image.fromarray(b.astype('uint8')) a.save('new-timg.jpg')
运算结果:
D:\python3\python.exe D:/python入门/数据可视化/numpy_demo/的mo.py (749, 1200, 3) uint8 Process finished with exit code 0
# from PIL import Image # import numpy as np # im=np.array(Image.open('timg.jpg')) # print(im.shape,im.dtype) # b=im*(100/255)+150 # a=Image.fromarray(b.astype('uint8')) # a.save('new-timg.jpg') # from PIL import Image # import numpy as np # im=np.array(Image.open('timg.jpg')) # print(im.shape,im.dtype) # b=255-im # a=Image.fromarray(b.astype('uint8')) # a.save('new-timg.jpg') from PIL import Image import numpy as np im=np.array(Image.open('timg.jpg')) print(im.shape,im.dtype) b=255*(im/255)**2 a=Image.fromarray(b.astype('uint8')) a.save('new-timg.jpg')
手绘风格的效果
from PIL import Image import numpy as np a = np.asarray(Image.open('timg.jpg').convert('L')).astype('float') depth = 8. # (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.) 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('b.jpg')