最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python將圖片批量轉(zhuǎn)化成素描圖的過程記錄

 更新時間:2021年08月11日 11:00:18   作者:guihunkun  
萬能的Python真的是除了不會生孩子,其他的還真不在話下,下面這篇文章主要給大家介紹了關于如何利用Python將圖片批量轉(zhuǎn)化成素描圖的相關資料,需要的朋友可以參考下

前言

正常圖片轉(zhuǎn)化成素描圖片無非對圖片像素的處理,矩陣變化而已。目前很多拍照修圖App都有這一功能,核心代碼不超30行。如下利用 Python 實現(xiàn)讀取一張圖片并將其轉(zhuǎn)化成素描圖片。至于批處理也簡單,循環(huán)讀取文件夾里的圖片處理即可。具體代碼可以去我的 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("轉(zhuǎn)換成功,請查看 : ", 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')  # 圖片轉(zhuǎn)換成灰色
    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("轉(zhuǎn)換成功,請查看 : ", out_picture)


def plot_sketch2(origin_picture, out_picture, alpha=1.0):
    img = Image.open(origin_picture)
    blur = 20
    img1 = img.convert('L')  # 圖片轉(zhuǎn)換成灰色
    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將圖片批量轉(zhuǎn)化成素描圖的文章就介紹到這了,更多相關Python圖片批量轉(zhuǎn)素描圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python接單的過程記錄分享

    Python接單的過程記錄分享

    這篇文章主要介紹了Python接單的過程記錄分享,需要的朋友可以參考下
    2021-04-04
  • PyTorch?TensorFlow機器學習框架選擇實戰(zhàn)

    PyTorch?TensorFlow機器學習框架選擇實戰(zhàn)

    這篇文章主要為大家介紹了PyTorch?TensorFlow機器學習框架選擇實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • Python中的列表知識點匯總

    Python中的列表知識點匯總

    這篇文章主要總結了一些Python中的列表的知識點,來自于IBM官網(wǎng)技術文檔,需要的朋友可以參考下
    2015-04-04
  • python3監(jiān)控CentOS磁盤空間腳本

    python3監(jiān)控CentOS磁盤空間腳本

    這篇文章主要為大家詳細介紹了Python3監(jiān)控CentOS磁盤空間腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 用python制作個音樂下載器

    用python制作個音樂下載器

    這篇文章主要介紹了用python制作個音樂下載器,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python使用future處理并發(fā)問題方案詳解

    Python使用future處理并發(fā)問題方案詳解

    從Python3.2引入的concurrent.futures模塊,Python2.5以上需要在pypi中安裝futures包。future指一種對象,表示異步執(zhí)行的操作。這個概念的作用很大,是concurrent.futures模塊和asyncio包的基礎
    2023-02-02
  • 詳解sklearn?Preprocessing?數(shù)據(jù)預處理功能

    詳解sklearn?Preprocessing?數(shù)據(jù)預處理功能

    這篇文章主要介紹了sklearn?Preprocessing?數(shù)據(jù)預處理功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • python中關于對super()函數(shù)疑問解惑

    python中關于對super()函數(shù)疑問解惑

    Python中的super()是用于調(diào)用父類(或父類的父類...)方法的函數(shù),主要用于多繼承,單繼承問題不大,下面這篇文章主要給大家介紹了關于python中關于對super()函數(shù)疑問解惑的相關資料,需要的朋友可以參考下
    2022-08-08
  • Python基礎面向?qū)ο笾^承與派生詳解

    Python基礎面向?qū)ο笾^承與派生詳解

    這篇文章主要為大家介紹了Python基礎面向?qū)ο笾^承與派生詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Python 爬蟲學習筆記之單線程爬蟲

    Python 爬蟲學習筆記之單線程爬蟲

    本文給大家分享的是python使用requests爬蟲庫實現(xiàn)單線程爬蟲的代碼以及requests庫的安裝和使用,有需要的小伙伴可以參考下
    2016-09-09

最新評論

奉新县| 平罗县| 长阳| 江川县| 乌苏市| 永吉县| 东山县| 四川省| 化隆| 连城县| 磐安县| 沧源| 屏东县| 车险| 武冈市| 四会市| 长垣县| 鲁山县| 金山区| 阳城县| 长沙县| 茌平县| 上犹县| 河北省| 柘城县| 宜兴市| 阿克| 霞浦县| 潞西市| 桂平市| 汽车| 左贡县| 普宁市| 鄂伦春自治旗| 霞浦县| 龙州县| 方正县| 开阳县| 海伦市| 西昌市| 方山县|