Python+PyQT5實(shí)現(xiàn)手繪圖片生成器
手繪圖片生成器可以將導(dǎo)入的彩色圖片通過(guò)python分析光源、灰度等操作生成手繪圖片。

UI界面的整體部分代碼塊,UI界面的設(shè)計(jì)比較簡(jiǎn)單。效果在上面的圖片展示。
class HandImage(QWidget):
def __init__(self):
super(HandImage, self).__init__()
self.init_ui()
def init_ui(self):
'''
UI界面組件及布局
:return:
'''
self.setWindowTitle('手繪圖片生成器 公眾號(hào):[Python 集中營(yíng)]')
self.setWindowIcon(QIcon('手繪圖標(biāo).ico'))
self.setFixedWidth(500)
self.sou_im_path = QLineEdit()
self.sou_im_path.setReadOnly(True)
self.sou_im_path_btn = QPushButton()
self.sou_im_path_btn.setText('源圖片')
self.sou_im_path_btn.clicked.connect(self.sou_im_path_btn_clk)
self.dir_path = QLineEdit()
self.dir_path.setReadOnly(True)
self.dir_path_btn = QPushButton()
self.dir_path_btn.setText('存儲(chǔ)')
self.dir_path_btn.clicked.connect(self.dir_path_btn_clk)
self.start_btn = QPushButton()
self.start_btn.setText('開始繪制圖像')
self.start_btn.clicked.connect(self.start_btn_clk)
grid = QGridLayout()
grid.addWidget(self.sou_im_path, 0, 0, 1, 1)
grid.addWidget(self.sou_im_path_btn, 0, 1, 1, 1)
grid.addWidget(self.dir_path, 1, 0, 1, 1)
grid.addWidget(self.dir_path_btn, 1, 1, 1, 1)
grid.addWidget(self.start_btn, 2, 0, 1, 2)
self.thread_ = WorkThread(self)
self.thread_.finished.connect(self.finished)
self.setLayout(grid)
# UI界面上的槽函數(shù)
def sou_im_path_btn_clk(self):
'''
選擇源圖片并設(shè)置路徑
:return:
'''
im_path = QFileDialog.getOpenFileName(self, os.getcwd(), '打開圖片', 'Image File(*.jpg);;Image File(*.png)')
self.sou_im_path.setText(im_path[0])
def dir_path_btn_clk(self):
'''
選擇存儲(chǔ)路徑并設(shè)置路徑
:return:
'''
dir_path = QFileDialog.getExistingDirectory(self, os.getcwd(), '選擇路徑')
self.dir_path.setText(dir_path)
def start_btn_clk(self):
'''
開始按鈕綁定的槽函數(shù)
:return:
'''
self.start_btn.setEnabled(False)
self.thread_.start()
def finished(self, finished):
'''
用于子線程傳遞完成信號(hào)的槽函數(shù)
:param finished: 信號(hào)變量
:return:
'''
if finished is True:
self.start_btn.setEnabled(True)
其中繪圖用到的第三方庫(kù)只有兩個(gè),主要的還是Pillow圖像處理庫(kù),還有就是numpy科學(xué)計(jì)算庫(kù)用于一些數(shù)組計(jì)算等的操作。
將第三方的處理庫(kù)導(dǎo)入到代碼塊中
from PIL import Image # 圖像處理模塊 import numpy as np # 科學(xué)計(jì)算庫(kù) # PyQt5界面制作及樣式、核心組件 from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * # 應(yīng)用基礎(chǔ)操作相關(guān) import sys import os
創(chuàng)建用于專門手繪圖像的子線程類,將UI界面的處理邏輯和生成圖像的處理邏輯分開不至于產(chǎn)生無(wú)響應(yīng)的卡死狀態(tài)。
class WorkThread(QThread):
finished = pyqtSignal(bool)
def __init__(self, parent=None):
super(WorkThread, self).__init__(parent)
self.parent = parent
self.working = True
def __del__(self):
self.working = False
self.wait()
def run(self):
# 源圖片路徑
sou_im_path = self.parent.sou_im_path.text().strip()
# 存儲(chǔ)路徑
dir_path = self.parent.dir_path.text().strip()
if sou_im_path == '' or dir_path == '':
self.finished.emit(True)
return
# 打開需要進(jìn)行轉(zhuǎn)的圖像,并進(jìn)行參數(shù)設(shè)置,取出來(lái)的參數(shù)主要圖像的一些梯度值。最后進(jìn)行數(shù)組保存。
vals = np.asarray(Image.open(sou_im_path).convert('L')).astype('float')
'''圖像參數(shù)處理'''
depth = 12.0 # 設(shè)置初始化深度
gray_vals = np.gradient(vals) # 提取圖像灰度的梯度值
gray_x, gray_y = gray_vals # 單獨(dú)提取橫坐標(biāo)與縱坐標(biāo)的灰度值
print('當(dāng)前橫坐標(biāo)的灰度值:', gray_x)
print('當(dāng)前縱坐標(biāo)的灰度值:', gray_y)
# 重新設(shè)置橫坐標(biāo)合縱坐標(biāo)的灰度值
gray_x = gray_x * depth / 100.0
gray_y = gray_y * depth / 100.0
# 根據(jù)numpy.sqrt()函數(shù)計(jì)算橫坐標(biāo)和縱坐標(biāo)灰度值的平方根
gray_sqrt = np.sqrt(gray_x ** 2 + gray_y ** 2 + 1.0)
# 重新計(jì)算X軸、Y軸、Z軸的光源
light_x = gray_x / gray_sqrt
light_y = gray_y / gray_sqrt
light_z = 1.0 / gray_sqrt
# 計(jì)算光源的方位角度、俯視角度
agnle_el = np.pi / 2.2 # 俯視角度
agnle_az = np.pi / 4. # 方位角度
# 分別計(jì)算光源對(duì)X軸、Y軸、Z軸的影響
dx = np.cos(agnle_el) * np.cos(agnle_az) # 光源對(duì)x 軸的影響
dy = np.cos(agnle_el) * np.sin(agnle_az) # 光源對(duì)y 軸的影響
dz = np.sin(agnle_el) # 光源對(duì)z 軸的影響
# 設(shè)置光源歸一化處理
light = 255 * (dx * light_x + dy * light_y + dz * light_z)
light = light.clip(0, 255)
# 重新構(gòu)建圖像
image = Image.fromarray(light.astype('uint8'))
image.save(dir_path + '/手繪圖像.jpg')
self.finished.emit(True)
print('手繪圖像繪制完成!')主要代碼塊實(shí)現(xiàn)都在上面了,下面將展示完整的代碼
完整代碼
# -*- coding:utf-8 -*-
# @author Python 集中營(yíng)
# @date 2022/2/10
# @file test2.py
# done
# 手繪圖片生成器:以雪容融為例一鍵生成...
# 手繪圖片生成器可以將導(dǎo)入的彩色圖片通過(guò)python分析光源、灰度等操作生成手繪圖片。
# 其中繪圖用到的第三方庫(kù)只有兩個(gè),主要的還是Pillow圖像處理庫(kù),還有就是numpy科學(xué)計(jì)算庫(kù)用于一些數(shù)組計(jì)算等的操作。
# 將第三方的處理庫(kù)導(dǎo)入到代碼塊中
from PIL import Image # 圖像處理模塊
import numpy as np # 科學(xué)計(jì)算庫(kù)
# PyQt5界面制作及樣式、核心組件
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
# 應(yīng)用基礎(chǔ)操作相關(guān)
import sys
import os
# 創(chuàng)建用于專門手繪圖像的子線程類,將UI界面的處理邏輯和生成圖像的處理邏輯分開不至于產(chǎn)生無(wú)響應(yīng)的卡死狀態(tài)。
class WorkThread(QThread):
finished = pyqtSignal(bool)
def __init__(self, parent=None):
super(WorkThread, self).__init__(parent)
self.parent = parent
self.working = True
def __del__(self):
self.working = False
self.wait()
def run(self):
# 源圖片路徑
sou_im_path = self.parent.sou_im_path.text().strip()
# 存儲(chǔ)路徑
dir_path = self.parent.dir_path.text().strip()
if sou_im_path == '' or dir_path == '':
self.finished.emit(True)
return
# 打開需要進(jìn)行轉(zhuǎn)的圖像,并進(jìn)行參數(shù)設(shè)置,取出來(lái)的參數(shù)主要圖像的一些梯度值。最后進(jìn)行數(shù)組保存。
vals = np.asarray(Image.open(sou_im_path).convert('L')).astype('float')
'''圖像參數(shù)處理'''
depth = 12.0 # 設(shè)置初始化深度
gray_vals = np.gradient(vals) # 提取圖像灰度的梯度值
gray_x, gray_y = gray_vals # 單獨(dú)提取橫坐標(biāo)與縱坐標(biāo)的灰度值
print('當(dāng)前橫坐標(biāo)的灰度值:', gray_x)
print('當(dāng)前縱坐標(biāo)的灰度值:', gray_y)
# 重新設(shè)置橫坐標(biāo)合縱坐標(biāo)的灰度值
gray_x = gray_x * depth / 100.0
gray_y = gray_y * depth / 100.0
# 根據(jù)numpy.sqrt()函數(shù)計(jì)算橫坐標(biāo)和縱坐標(biāo)灰度值的平方根
gray_sqrt = np.sqrt(gray_x ** 2 + gray_y ** 2 + 1.0)
# 重新計(jì)算X軸、Y軸、Z軸的光源
light_x = gray_x / gray_sqrt
light_y = gray_y / gray_sqrt
light_z = 1.0 / gray_sqrt
# 計(jì)算光源的方位角度、俯視角度
agnle_el = np.pi / 2.2 # 俯視角度
agnle_az = np.pi / 4. # 方位角度
# 分別計(jì)算光源對(duì)X軸、Y軸、Z軸的影響
dx = np.cos(agnle_el) * np.cos(agnle_az) # 光源對(duì)x 軸的影響
dy = np.cos(agnle_el) * np.sin(agnle_az) # 光源對(duì)y 軸的影響
dz = np.sin(agnle_el) # 光源對(duì)z 軸的影響
# 設(shè)置光源歸一化處理
light = 255 * (dx * light_x + dy * light_y + dz * light_z)
light = light.clip(0, 255)
# 重新構(gòu)建圖像
image = Image.fromarray(light.astype('uint8'))
image.save(dir_path + '/手繪圖像.jpg')
self.finished.emit(True)
print('手繪圖像繪制完成!')
# UI界面的整體部分代碼塊,UI界面的設(shè)計(jì)比較簡(jiǎn)單。效果在下面的圖片展示。
class HandImage(QWidget):
def __init__(self):
super(HandImage, self).__init__()
self.init_ui()
def init_ui(self):
'''
UI界面組件及布局
:return:
'''
self.setWindowTitle('手繪圖片生成器 公眾號(hào):[Python 集中營(yíng)]')
self.setWindowIcon(QIcon('手繪圖標(biāo).ico'))
self.setFixedWidth(500)
self.sou_im_path = QLineEdit()
self.sou_im_path.setReadOnly(True)
self.sou_im_path_btn = QPushButton()
self.sou_im_path_btn.setText('源圖片')
self.sou_im_path_btn.clicked.connect(self.sou_im_path_btn_clk)
self.dir_path = QLineEdit()
self.dir_path.setReadOnly(True)
self.dir_path_btn = QPushButton()
self.dir_path_btn.setText('存儲(chǔ)')
self.dir_path_btn.clicked.connect(self.dir_path_btn_clk)
self.start_btn = QPushButton()
self.start_btn.setText('開始繪制圖像')
self.start_btn.clicked.connect(self.start_btn_clk)
grid = QGridLayout()
grid.addWidget(self.sou_im_path, 0, 0, 1, 1)
grid.addWidget(self.sou_im_path_btn, 0, 1, 1, 1)
grid.addWidget(self.dir_path, 1, 0, 1, 1)
grid.addWidget(self.dir_path_btn, 1, 1, 1, 1)
grid.addWidget(self.start_btn, 2, 0, 1, 2)
self.thread_ = WorkThread(self)
self.thread_.finished.connect(self.finished)
self.setLayout(grid)
# UI界面上的槽函數(shù)
def sou_im_path_btn_clk(self):
'''
選擇源圖片并設(shè)置路徑
:return:
'''
im_path = QFileDialog.getOpenFileName(self, os.getcwd(), '打開圖片', 'Image File(*.jpg);;Image File(*.png)')
self.sou_im_path.setText(im_path[0])
def dir_path_btn_clk(self):
'''
選擇存儲(chǔ)路徑并設(shè)置路徑
:return:
'''
dir_path = QFileDialog.getExistingDirectory(self, os.getcwd(), '選擇路徑')
self.dir_path.setText(dir_path)
def start_btn_clk(self):
'''
開始按鈕綁定的槽函數(shù)
:return:
'''
self.start_btn.setEnabled(False)
self.thread_.start()
def finished(self, finished):
'''
用于子線程傳遞完成信號(hào)的槽函數(shù)
:param finished: 信號(hào)變量
:return:
'''
if finished is True:
self.start_btn.setEnabled(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = HandImage()
main.show()
sys.exit(app.exec_())
以上就是Python+PyQT5實(shí)現(xiàn)手繪圖片生成器的詳細(xì)內(nèi)容,更多關(guān)于Python PyQT5手繪圖片生成器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?ttkbootstrap?制作賬戶注冊(cè)信息界面的案例代碼
ttkbootstrap 是一個(gè)基于 tkinter 的界面美化庫(kù),使用這個(gè)工具可以開發(fā)出類似前端 bootstrap 風(fēng)格的 tkinter 桌面程序。本文重點(diǎn)給大家介紹Python?ttkbootstrap?制作賬戶注冊(cè)信息界面的案例代碼,感興趣的朋友一起看看吧2022-02-02
Python如何使用Scapy實(shí)現(xiàn)端口探測(cè)
Scapy 是一款使用純Python編寫的跨平臺(tái)網(wǎng)絡(luò)數(shù)據(jù)包操控工具,它能夠處理和嗅探各種網(wǎng)絡(luò)數(shù)據(jù)包,本文主要介紹了Python如何使用使用Scapy實(shí)現(xiàn)端口探測(cè),有需要的可以參考下2023-10-10
python opencv設(shè)置攝像頭分辨率以及各個(gè)參數(shù)的方法
下面小編就為大家分享一篇python opencv設(shè)置攝像頭分辨率以及各個(gè)參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Tensorflow2.1 完成權(quán)重或模型的保存和加載
這篇文章主要為大家介紹了Tensorflow2.1 完成權(quán)重或模型的保存和加載,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例
這篇文章主要介紹了Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能,結(jié)合實(shí)例形式分析了Python+socket實(shí)現(xiàn)UDP協(xié)議廣播的客戶端與服務(wù)器端功能相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
django從請(qǐng)求到響應(yīng)的過(guò)程深入講解
這篇文章主要給大家介紹了關(guān)于django從請(qǐng)求到響應(yīng)的過(guò)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

