图像的手绘效果(PIL的应用实例)

图像的变换:

原图像:

1.读入图像后,获得像素RGB值(所有RGB值取反),修改后保存为新的文件

from PIL import Image
import numpy as np
im=np.array(Image.open("D:/pytest/a.jpeg"))
print(im.shape,im.dtype)
b=[255,255,255] -im
newim=Image.fromarray(b.astype('uint8'))
newim.save("D:/pytest/a1.jpg")

2.利用convert将im变为灰度值


from PIL import Image
import numpy as np
im=np.array(Image.open("D:/pytest/a.jpeg").convert('L'))

b=255 -im
newim=Image.fromarray(b.astype('uint8'))
newim.save("D:/pytest/a2.jpg")

 

3.对灰度值做区间变换

from PIL import Image
import numpy as np
im=np.array(Image.open("D:/pytest/a.jpeg").convert('L'))

b=(100/255)*im+150
newim=Image.fromarray(b.astype('uint8'))
newim.save("D:/pytest/a3.jpg")

4.像素平方

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 25 21:41:30 2019

@author: Administrator
"""

from PIL import Image
import numpy as np
im=np.array(Image.open("D:/pytest/a.jpeg").convert('L'))

b=255*(im/255)**2
newim=Image.fromarray(b.astype('uint8'))
newim.save("D:/pytest/a4.jpg")

 

5. 图像的手绘效果

手绘效果的几个特征:
• 黑白灰色
• 边界线条较重
• 相同或相近色彩趋于白色
• 略有光源效果

梯度的重构:利用像素之间的梯度值和虚拟深度值对图像进行重构根据灰度变化来模拟人类视觉的远近程度

光源效果:根据灰度变化来模拟人类视觉的远近程度
• 设计一个位于图像斜上的虚拟光源
• 光源相对于图像的俯视角为Elevation,方位角为Azimuth

• 建立光源对个点梯度值的影响函数
• 运算出各点的新像

from PIL import Image
import numpy as np
a=np.asarray(Image.open('D:/pytest/a.jpeg').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)   #光源对x轴的影响
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,255)                    #避免数据越界,将生成的灰度值裁剪至0-255内
im=Image.fromarray(b.astype('uint8'))    #图像重构
im.save('D:/pytest/aHD.jpg')

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值