利用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)文章
PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn)
這篇文章主要為大家介紹了PyTorch?TensorFlow機(jī)器學(xué)習(xí)框架選擇實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Python使用future處理并發(fā)問(wèn)題方案詳解
從Python3.2引入的concurrent.futures模塊,Python2.5以上需要在pypi中安裝futures包。future指一種對(duì)象,表示異步執(zhí)行的操作。這個(gè)概念的作用很大,是concurrent.futures模塊和asyncio包的基礎(chǔ)2023-02-02
詳解sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能
這篇文章主要介紹了sklearn?Preprocessing?數(shù)據(jù)預(yù)處理功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
python中關(guān)于對(duì)super()函數(shù)疑問(wèn)解惑
Python中的super()是用于調(diào)用父類(或父類的父類...)方法的函數(shù),主要用于多繼承,單繼承問(wèn)題不大,下面這篇文章主要給大家介紹了關(guān)于python中關(guān)于對(duì)super()函數(shù)疑問(wèn)解惑的相關(guān)資料,需要的朋友可以參考下2022-08-08
Python 爬蟲(chóng)學(xué)習(xí)筆記之單線程爬蟲(chóng)
本文給大家分享的是python使用requests爬蟲(chóng)庫(kù)實(shí)現(xiàn)單線程爬蟲(chóng)的代碼以及requests庫(kù)的安裝和使用,有需要的小伙伴可以參考下2016-09-09

