美文网首页
趣味Python — 不到20行代码制作一个 “手绘风” 视频

趣味Python — 不到20行代码制作一个 “手绘风” 视频

作者: 小张Python | 来源:发表于2021-03-31 08:14 被阅读0次

    Hello 大家好,我是小张,好久不见~

    本期推文与计算机视觉相关,用不到 20 行Python代码将一张图片由自然风转化为手绘风,期间未对图片进行任何预处理、后处理;代码中只借助了两个常见库,核心计算由 Numpy 负责 ,Pillow 负责图片读写

    在正文开始之前,先看一下最初效果,下面是单张图片转换前后对比

    图一

    图片

    图二

    图片

    图三

    图片

    为了增加趣味性,后面将这段代码应用到一个视频中,加上一个背景音乐,新鲜的 “手绘风视频” 出炉
    Python 手绘风视频制作!

    “手绘风”实现步骤

    讲解之前,需要了解手绘图像的三个主要特点:

    • 图片需为灰度图,是单通道的;

    • 边缘部分线条较重涂抹为黑色,相同或相近像素值转换后趋于白色;

    • 在光源效果的加持下,灰度变化可模拟人类视觉的远近效果

    读取图片,转化为数组

    因为后面要用到像素计算,为了方便,事先将读取后的图片转化为数组

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n24" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = np.asarray(Image.open("Annie1.jpg").convert('L')).astype('float')</pre>

    计算 x,y,z 轴梯度值,并归一化

    刚才提到手绘照片的一个特点,就是 手绘照片对边缘区域更加侧重,定位图片边缘部分,最有效方式就是计算梯度,用灰度变化来模拟图片远近效果,depth 表示预设深度,z 轴默认梯度为 1

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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.</pre>

    对梯度值完成归一化操作

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
    uni_x = grad_x / A
    uni_y = grad_y / A
    uni_z = 1. / A</pre>

    加入光源效果

    手绘风图片除了计算梯度值之外,还需要考虑光源影响;根据光源入射的角度不同最有对x,y,z 各轴上的梯度值有不同程度的影响,添加一个模拟光源,放置在斜上方,与 x , y 分别形成两个夹角

    图片

    并且这两个夹角是通过实验得到是已知的,然后根据正弦余弦函数计算出最终新的像素值

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">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) # 光源归一化,8 255
    b = b.clip(0, 255)# 对像素值低于0,高于255部分做截断处理</pre>

    导出图片,并保存

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n36" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">im.save("Annie_shouhui.jpg")</pre>

    以下是该步骤涉及到的的全部代码

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from PIL import Image
    import numpy as np

    a = np.asarray(Image.open("Annie1.jpg").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.)
    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("Annie_shouhui.jpg")</pre>

    制作手绘风视频

    图片转化后的效果虽然也不错,但图片毕竟是静态的,人作为视觉动物,如果能做成动态的那再好不过了,知道上面的方法之后,只需对视频再加上一个拆帧合并操作,就能制作一个手绘风 视频效果

    you-get 下载视频

    这里我用 you-get 命令在 B 站上找了一个视频,下载了下来,

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">you-get --format=dash-flv -o ./ https://www.bilibili.com/video/BV1tT4y1j7a9?from=search&8014393453748720686</pre>

    图片

    下载完之后,用 OpenCV2 对视频进行切帧操作,切帧同时对图片进行转化,写出到本地视频文件中

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> vc = cv2.VideoCapture(video_path)
    c = 0
    if vc.isOpened():
    rval,frame = vc.read()
    height,width = frame.shape[0],frame.shape[1]
    print(height, width)
    else:
    rval = False
    height,width = 960,1200

    jpg_list = [os.path.join('Pic_Directory/',i) for i in os.listdir('Pic_Directory') if i.endswith('.jpg')]

    fps = 24 # 视频帧率
    video_path1 = './text.mp4'
    video_writer = cv2.VideoWriter(video_path1,cv2.VideoWriter_fourcc(*'mp4v'),fps,(width,height))

    while rval:
    rval,frame = vc.read()# 读取视频帧
    img = coonvert_jpg(Image.fromarray(frame))
    frame_converted = np.array(img)

    转化为三通道

    image = np.expand_dims(frame_converted,axis = 2)
    result_arr = np.concatenate((image,image,image),axis = -1)

    video_writer.write(result_arr)
    print('Sucessfully Conveted---------{}'.format(c))
    c = c + 1
    if c >= 3000:
    break
    video_writer.release()</pre>

    在图片序列提取时,需要注意一点,因为转化后的图片是单通道的,直接借助 OpenCV 生成视频序列是无法播放的,需增加一个步骤单通道转化为三通道!

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> # 转化为三通道
    image = np.expand_dims(frame_converted,axis = 2)
    result_arr = np.concatenate((image,image,image),axis = -1)</pre>

    想让生成的视频更有感觉的话可以添加一个背影音乐,借助剪辑软件、Python 都可,这里建议最好用剪辑软件,原因是 Python 自定义增加音频效果并不理想,添加音乐时需要有实时反馈, 而 Python 暂时无法满足此要求

    数据源码获取

    文中涉及到的源码将数据获取方式,关注微信公号:小张Python,后台回复关键字:210322 即可!

    小结

    本文主要介绍了如何用 Python将一张图片转化为手绘风格,代码量很少但涉及知识领域与数学、物理相关,所以不容易理解,本篇文章目的只是为了向大家介绍图片手绘风转换有这么一种方法,当然如果有感兴趣的小伙伴可以深究一下

    好了以上就是本篇文章的全部内容了,最后感谢大家的阅读,我们下期见~

    相关文章

      网友评论

          本文标题:趣味Python — 不到20行代码制作一个 “手绘风” 视频

          本文链接:https://www.haomeiwen.com/subject/nqqohltx.html