五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

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

發(fā)布日期:2022-02-14 10:32 | 文章來源:CSDN

正常圖片轉(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'))  # 重構(gòu)圖像
 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'))  # 重構(gòu)圖像
 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)

結(jié)果








總結(jié)

到此這篇關(guān)于利用Python將圖片批量轉(zhuǎn)化成素描圖的文章就介紹到這了,更多相關(guān)Python圖片批量轉(zhuǎn)素描圖內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國服務器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部