效果展示
原始效果圖
素描效果圖
相關依賴包
# 超美觀的列印庫
from pprint import pprint
# 影象處理庫
from PIL import Image
# 科學計算庫
import numpy as np
# GUI檔案開啟視窗
import tkinter.filedialog
製作檔案開啟視窗
# 建立根視窗
root = tkinter.Tk().withdraw()
# 檔案選擇對話視窗,返回檔案物件
file_ = tkinter.filedialog.askopenfilename()
pprint("1、讀取原始影象成功")
素描圖轉換
# 加入異常處理
try:
# 定義顏色深度(0~100,值越大顏色越深)
depth = 20
# 獲取照片灰度的梯度值
image_grad = np.gradient(np.asarray(Image.open(file_).convert('L')).astype('int'))
pprint("2、獲取影象梯度值成功")
# 分別獲取X,Y方向的梯度值,然後使用顏色深度進行處理
grad_x, grad_y = image_grad[0] * depth / 100., image_grad[1] * depth / 100.
pprint("3、顏色深度處理成功")
# 降噪基
base_ = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
a, b, c = grad_x / base_, grad_y / base_, 1. / base_
# 光源的俯視角度值和方位角度值
sce_z, sce_x = np.pi / 2.1, np.pi / 3
# 光源對x,y,z 軸的影響
dx, dy, dz = np.cos(sce_z) * np.cos(sce_x), np.cos(sce_z) * np.sin(sce_x), np.sin(sce_z)
# 光源歸一化
Normalized = 255 * (dx * a + dy * b + dz * c).clip(0, 255)
pprint("4、光源處理成功")
# 重新構造影象
img = Image.fromarray(Normalized.astype('uint8'))
pprint("5、影象重構成功")
# 儲存轉換後的照片
img.save('素描圖.jpg')
pprint("6、儲存轉換後的影象成功")
except Exception:
print('對不起,影象轉換失敗!')
exe檔案打包
-F 引數代表打包檔案,trans_image.py 是自己的.py檔案路徑
pyinstaller -F trans_image.py
【粉絲福利】關注公眾號,獲取全套視訊資料,喜歡小編點個 '關注' 吧!
本文由微信公眾號【python 集中營】釋出,更多精彩文章、視訊資料即可領取!