主頁 > 知識庫 > 利用Python將圖片批量轉(zhuǎn)化成素描圖的過程記錄

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

熱門標簽:智能手機 網(wǎng)站文章發(fā)布 鐵路電話系統(tǒng) 呼叫中心市場需求 服務器配置 美圖手機 檢查注冊表項 銀行業(yè)務

前言

正常圖片轉(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實現(xiàn)把圖片轉(zhuǎn)換成素描
  • python實現(xiàn)圖片彩色轉(zhuǎn)化為素描
  • python opencv圖像處理(素描、懷舊、光照、流年、濾鏡 原理及實現(xiàn))
  • python實現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
  • python實現(xiàn)圖片素描效果
  • python如何將圖片轉(zhuǎn)換素描畫
  • Python使用5行代碼批量做小姐姐的素描圖

標簽:樂山 新疆 上海 河南 滄州 沈陽 紅河 長治

巨人網(wǎng)絡通訊聲明:本文標題《利用Python將圖片批量轉(zhuǎn)化成素描圖的過程記錄》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266