新闻动态

利用Python将图片批量转化成素描图的过程记录

发布日期:2022-02-14 10:32 | 文章来源:CSDN

正常图片转化成素描图片无非对图片像素的处理,矩阵变化而已。目前很多拍照修图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)

结果








总结

到此这篇关于利用Python将图片批量转化成素描图的文章就介绍到这了,更多相关Python图片批量转素描图内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

美国服务器租用

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部