欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+PyQT5實現(xiàn)手繪圖片生成器

 更新時間:2022年02月15日 08:34:51   作者:Python 集中營  
這篇文章主要介紹了利用Python PyQT5制作一個手繪圖片生成器,可以將導(dǎo)入的彩色圖片通過python分析光源、灰度等操作生成手繪圖片。感興趣的可以跟隨小編一起了解一下

手繪圖片生成器可以將導(dǎo)入的彩色圖片通過python分析光源、灰度等操作生成手繪圖片。

file

UI界面的整體部分代碼塊,UI界面的設(shè)計比較簡單。效果在上面的圖片展示。

class HandImage(QWidget):
    def __init__(self):
        super(HandImage, self).__init__()
        self.init_ui()

    def init_ui(self):
        '''
        UI界面組件及布局
        :return:
        '''
        self.setWindowTitle('手繪圖片生成器   公眾號:[Python 集中營]')
        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('存儲')
        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):
        '''
        選擇存儲路徑并設(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):
        '''
        用于子線程傳遞完成信號的槽函數(shù)
        :param finished: 信號變量
        :return:
        '''
        if finished is True:
            self.start_btn.setEnabled(True)

其中繪圖用到的第三方庫只有兩個,主要的還是Pillow圖像處理庫,還有就是numpy科學(xué)計算庫用于一些數(shù)組計算等的操作。

將第三方的處理庫導(dǎo)入到代碼塊中

from PIL import Image  # 圖像處理模塊
import numpy as np  # 科學(xué)計算庫

# 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)生無響應(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()
        # 存儲路徑
        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è)置,取出來的參數(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  # 單獨提取橫坐標(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ù)計算橫坐標(biāo)和縱坐標(biāo)灰度值的平方根
        gray_sqrt = np.sqrt(gray_x ** 2 + gray_y ** 2 + 1.0)

        # 重新計算X軸、Y軸、Z軸的光源
        light_x = gray_x / gray_sqrt
        light_y = gray_y / gray_sqrt
        light_z = 1.0 / gray_sqrt

        # 計算光源的方位角度、俯視角度
        agnle_el = np.pi / 2.2  # 俯視角度
        agnle_az = np.pi / 4.  # 方位角度

        # 分別計算光源對X軸、Y軸、Z軸的影響
        dx = np.cos(agnle_el) * np.cos(agnle_az)  # 光源對x 軸的影響
        dy = np.cos(agnle_el) * np.sin(agnle_az)  # 光源對y 軸的影響
        dz = np.sin(agnle_el)  # 光源對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('手繪圖像繪制完成!')

主要代碼塊實現(xiàn)都在上面了,下面將展示完整的代碼

完整代碼

# -*- coding:utf-8 -*-
# @author Python 集中營
# @date 2022/2/10
# @file test2.py

# done

# 手繪圖片生成器:以雪容融為例一鍵生成...

# 手繪圖片生成器可以將導(dǎo)入的彩色圖片通過python分析光源、灰度等操作生成手繪圖片。

# 其中繪圖用到的第三方庫只有兩個,主要的還是Pillow圖像處理庫,還有就是numpy科學(xué)計算庫用于一些數(shù)組計算等的操作。

# 將第三方的處理庫導(dǎo)入到代碼塊中
from PIL import Image  # 圖像處理模塊
import numpy as np  # 科學(xué)計算庫

# 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)生無響應(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()
        # 存儲路徑
        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è)置,取出來的參數(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  # 單獨提取橫坐標(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ù)計算橫坐標(biāo)和縱坐標(biāo)灰度值的平方根
        gray_sqrt = np.sqrt(gray_x ** 2 + gray_y ** 2 + 1.0)

        # 重新計算X軸、Y軸、Z軸的光源
        light_x = gray_x / gray_sqrt
        light_y = gray_y / gray_sqrt
        light_z = 1.0 / gray_sqrt

        # 計算光源的方位角度、俯視角度
        agnle_el = np.pi / 2.2  # 俯視角度
        agnle_az = np.pi / 4.  # 方位角度

        # 分別計算光源對X軸、Y軸、Z軸的影響
        dx = np.cos(agnle_el) * np.cos(agnle_az)  # 光源對x 軸的影響
        dy = np.cos(agnle_el) * np.sin(agnle_az)  # 光源對y 軸的影響
        dz = np.sin(agnle_el)  # 光源對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è)計比較簡單。效果在下面的圖片展示。

class HandImage(QWidget):
    def __init__(self):
        super(HandImage, self).__init__()
        self.init_ui()

    def init_ui(self):
        '''
        UI界面組件及布局
        :return:
        '''
        self.setWindowTitle('手繪圖片生成器   公眾號:[Python 集中營]')
        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('存儲')
        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):
        '''
        選擇存儲路徑并設(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):
        '''
        用于子線程傳遞完成信號的槽函數(shù)
        :param finished: 信號變量
        :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實現(xiàn)手繪圖片生成器的詳細(xì)內(nèi)容,更多關(guān)于Python PyQT5手繪圖片生成器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python進(jìn)程間通信方式

    Python進(jìn)程間通信方式

    這篇文章主要介紹了Python進(jìn)程間通信方式,進(jìn)程彼此之間互相隔離,要實現(xiàn)進(jìn)程間通信,主要通過隊列方式,下文更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • 基于web管理OpenVPN服務(wù)的安裝使用詳解

    基于web管理OpenVPN服務(wù)的安裝使用詳解

    這篇文章主要為大家介紹了基于web管理OpenVPN服務(wù)的安裝使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼

    Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼

    ttkbootstrap 是一個基于 tkinter 的界面美化庫,使用這個工具可以開發(fā)出類似前端 bootstrap 風(fēng)格的 tkinter 桌面程序。本文重點給大家介紹Python?ttkbootstrap?制作賬戶注冊信息界面的案例代碼,感興趣的朋友一起看看吧
    2022-02-02
  • Python如何使用Scapy實現(xiàn)端口探測

    Python如何使用Scapy實現(xiàn)端口探測

    Scapy 是一款使用純Python編寫的跨平臺網(wǎng)絡(luò)數(shù)據(jù)包操控工具,它能夠處理和嗅探各種網(wǎng)絡(luò)數(shù)據(jù)包,本文主要介紹了Python如何使用使用Scapy實現(xiàn)端口探測,有需要的可以參考下
    2023-10-10
  • Python批量安裝卸載1000個apk的方法

    Python批量安裝卸載1000個apk的方法

    這篇文章主要介紹了Python批量安裝卸載1000個apk的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • python opencv設(shè)置攝像頭分辨率以及各個參數(shù)的方法

    python opencv設(shè)置攝像頭分辨率以及各個參數(shù)的方法

    下面小編就為大家分享一篇python opencv設(shè)置攝像頭分辨率以及各個參數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Tensorflow2.1 完成權(quán)重或模型的保存和加載

    Tensorflow2.1 完成權(quán)重或模型的保存和加載

    這篇文章主要為大家介紹了Tensorflow2.1 完成權(quán)重或模型的保存和加載,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Python+Socket實現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例

    Python+Socket實現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例

    這篇文章主要介紹了Python+Socket實現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能,結(jié)合實例形式分析了Python+socket實現(xiàn)UDP協(xié)議廣播的客戶端與服務(wù)器端功能相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • 詳解Python time庫的使用

    詳解Python time庫的使用

    這篇文章主要介紹了Python time庫的使用,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • django從請求到響應(yīng)的過程深入講解

    django從請求到響應(yīng)的過程深入講解

    這篇文章主要給大家介紹了關(guān)于django從請求到響應(yīng)的過程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08

最新評論