利用Python将图片批量转化成素描图的过程记录
作者:guihunkun 时间:2022-08-21 09:53:59
前言
正常图片转化成素描图片无非对图片像素的处理,矩阵变化而已。目前很多拍照修图App都有这一功能,核心代码不超30行。如下利用 Python 实现读取一张图片并将其转化成素描图片。至于批处理也简单,循环读取文件夹里的图片处理即可。具体代码可以去我的 GitHub 下载。
程序
Method 1
def plot_sketch(origin_picture, out_picture) :
a = np.asarray(Image.open(origin_picture).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.0)
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(out_picture)
print("转换成功,请查看 : ", out_picture)
Method 2
def plot_sketch2(origin_picture, out_picture, alpha=1.0):
img = Image.open(origin_picture)
blur = 20
img1 = img.convert('L') # 图片转换成灰色
img2 = img1.copy()
img2 = ImageOps.invert(img2)
for i in range(blur): # 模糊度
img2 = img2.filter(ImageFilter.BLUR)
width, height = img1.size
for x in range(width):
for y in range(height):
a = img1.getpixel((x, y))
b = img2.getpixel((x, y))
img1.putpixel((x, y), min(int(a*255/(256-b*alpha)), 255))
img1.save(out_picture)
完整代码
from PIL import Image, ImageFilter, ImageOps
import numpy as np
import os
def plot_sketch(origin_picture, out_picture) :
a = np.asarray(Image.open(origin_picture).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.0)
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(out_picture)
print("转换成功,请查看 : ", out_picture)
def plot_sketch2(origin_picture, out_picture, alpha=1.0):
img = Image.open(origin_picture)
blur = 20
img1 = img.convert('L') # 图片转换成灰色
img2 = img1.copy()
img2 = ImageOps.invert(img2)
for i in range(blur): # 模糊度
img2 = img2.filter(ImageFilter.BLUR)
width, height = img1.size
for x in range(width):
for y in range(height):
a = img1.getpixel((x, y))
b = img2.getpixel((x, y))
img1.putpixel((x, y), min(int(a*255/(256-b*alpha)), 255))
img1.save(out_picture)
if __name__ == '__main__':
origin_picture = "pictures/5.jpg"
out_picture = "sketchs/sketch.jpg"
plot_sketch(origin_picture, out_picture)
origin_path = "./pictures"
out_path = "./sketchs"
dirs = os.listdir(origin_path)
for file in dirs:
origin_picture = origin_path + "/" + file
out_picture = out_path + "/" + "sketch_of_" + file
plot_sketch2(origin_picture, out_picture)
结果
总结
来源:https://blog.csdn.net/guihunkun/article/details/119533748
标签:python,图片,素描画
0
投稿
猜你喜欢
Python callable内置函数原理解析
2022-02-16 11:27:03
Python编码时应该注意的几个情况
2021-03-15 20:59:54
python实现代码审查自动回复消息
2021-09-15 22:20:46
Django框架之中间件MiddleWare的实现
2021-03-25 15:08:30
Oracle常见错误代码的分析与解决
2010-07-26 13:01:00
利用Python正则表达式过滤敏感词的方法
2023-05-07 05:05:18
asp伪继承初探_实例代码
2011-04-19 10:32:00
python获取全国城市pm2.5、臭氧等空气质量过程解析
2023-06-04 21:46:07
利用python实现蝴蝶曲线
2022-09-30 18:51:51
jquery 禁止鼠标右键并监听右键事件
2024-02-23 14:34:19
vue中的input框点击后不聚焦问题
2024-05-02 16:33:12
JavaScript实现的反序列化json字符串操作示例
2024-04-10 10:46:01
matplotlib绘制鼠标的十字光标的实现(自定义方式,官方实例)
2021-06-09 02:33:06
Python解决两个整数相除只得到整数部分的实例
2021-12-29 23:29:21
详细总结Python类的多继承知识
2021-10-02 03:15:02
详解python字节码
2021-12-16 16:56:29
vs code开发中语法正确但显示报错问题分析解决
2023-08-05 11:54:26
Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)
2021-01-23 04:23:13
Python如何脚本过滤文件中的注释
2021-10-03 15:24:52
详解Django模板层过滤器和继承的问题
2023-02-08 06:28:04