Python实现手绘图像效果转换
2021/11/14 22:10:24
本文主要是介绍Python实现手绘图像效果转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
作业三:手绘图像效果
编写手绘图像效果程序,选择一张新的图片,将其转换成手绘图像效果
from PIL import Image import numpy as np a = np.array(Image.open(r"C:\Users\xia\Pictures\Cyberpunk 2077\1.png").convert('L')) depth = 10 # 预设深度值是10 grad = np.gradient(a) grad_x, grad_y = grad # 提取x和y方向的梯度值 grad_x = grad_x * depth / 100 grad_y = grad_y * depth / 100 # 根据深度调整x和y方向的梯度值,归一化处理 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_az) # dx,dy,dz是光源对x/y/z三方向的影响程度 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 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(r"C:\Users\xia\Pictures\Cyberpunk 2077\2.png")
输入图片:
输出图片:
这篇关于Python实现手绘图像效果转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python