利用Python將圖片批量轉(zhuǎn)化成素描圖的過(guò)程記錄
前言
正常圖片轉(zhuǎn)化成素描圖片無(wú)非對(duì)圖片像素的處理,矩陣變化而已。目前很多拍照修圖App都有這一功能,核心代碼不超30行。如下利用 Python 實(shí)現(xiàn)讀取一張圖片并將其轉(zhuǎn)化成素描圖片。至于批處理也簡(jiǎ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) # 光源對(duì)x 軸的影響 dy = np.cos(vec_el) * np.sin(vec_az) # 光源對(duì)y 軸的影響 dz = np.sin(vec_el) # 光源對(duì)z 軸的影響 b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # 光源歸一化 b = b.clip(0, 255) im = Image.fromarray(b.astype('uint8')) # 重構(gòu)圖像 im.save(out_picture) print("轉(zhuǎn)換成功,請(qǐng)查看 : ", 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) # 光源對(duì)x 軸的影響 dy = np.cos(vec_el) * np.sin(vec_az) # 光源對(duì)y 軸的影響 dz = np.sin(vec_el) # 光源對(duì)z 軸的影響 b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # 光源歸一化 b = b.clip(0, 255) im = Image.fromarray(b.astype('uint8')) # 重構(gòu)圖像 im.save(out_picture) print("轉(zhuǎn)換成功,請(qǐng)查看 : ", 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)
結(jié)果
總結(jié)
到此這篇關(guān)于利用Python將圖片批量轉(zhuǎn)化成素描圖的文章就介紹到這了,更多相關(guān)Python圖片批量轉(zhuǎn)素描圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中類創(chuàng)建和實(shí)例化的過(guò)程詳解
這篇文章主要介紹了Python中類創(chuàng)建和實(shí)例化過(guò)程,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06Python基于Matplotlib庫(kù)簡(jiǎn)單繪制折線圖的方法示例
這篇文章主要介紹了Python基于Matplotlib庫(kù)簡(jiǎn)單繪制折線圖的方法,涉及Python Matplotlib庫(kù)的相關(guān)使用技巧,需要的朋友可以參考下2017-08-08python連接MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了python連接操作MySQL、MongoDB、Redis、memcache等數(shù)據(jù)庫(kù)的方法,大家可以參考使用2013-11-11python關(guān)于多值參數(shù)的實(shí)例詳解
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python關(guān)于多值參數(shù)的實(shí)例詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-07-07python3通過(guò)gevent.pool限制協(xié)程并發(fā)數(shù)量的實(shí)現(xiàn)方法
這篇文章主要介紹了python3通過(guò)gevent.pool限制協(xié)程并發(fā)數(shù)量的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Python中input和raw_input的一點(diǎn)區(qū)別
這篇文章主要介紹了Python中input和raw_input的一點(diǎn)區(qū)別,它們都是用來(lái)讀取控制臺(tái)輸入的函數(shù),需要的朋友可以參考下2014-10-10Python使用Selenium WebDriver的入門介紹及安裝教程(最新推薦)
這篇文章主要介紹了Python使用Selenium WebDriver的入門介紹及安裝教程,本文使用環(huán)境為python3.11+win10 64位+firefox瀏覽器,所以本文使用的瀏覽器驅(qū)動(dòng)是Firefox的geckodriver ,如果你使用的是其他瀏覽器,那么選擇自己對(duì)應(yīng)的瀏覽器驅(qū)動(dòng)程序即可,需要的朋友可以參考下2023-04-04Python中使用實(shí)現(xiàn)輸出哈沙德數(shù)的多種方法小結(jié)
哈沙德數(shù)(Harshad?Number),又稱Niven數(shù),是指一個(gè)自然數(shù),它可以被它的各位數(shù)字之和整除,本文將探討如何使用多種不同的方法來(lái)判斷一個(gè)數(shù)字是否是哈沙德數(shù),感興趣的可以了解下2024-01-01python中sys.argv參數(shù)用法實(shí)例分析
這篇文章主要介紹了python中sys.argv參數(shù)用法,實(shí)例分析了python中sys.argv參數(shù)的功能、定義及使用技巧,需要的朋友可以參考下2015-05-05