前言
正常圖片轉(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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描
- python實(shí)現(xiàn)圖片彩色轉(zhuǎn)化為素描
- python opencv圖像處理(素描、懷舊、光照、流年、濾鏡 原理及實(shí)現(xiàn))
- python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫(huà)格式
- python實(shí)現(xiàn)圖片素描效果
- python如何將圖片轉(zhuǎn)換素描畫(huà)
- Python使用5行代碼批量做小姐姐的素描圖