本篇內容介紹了“基于Python如何實現簡易的動漫圖片轉換器”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
效果圖

UI界面的制作使用的還是pyqt5,因為用習慣了使用起來相當方便,接下來還是先將使用到的python非標準庫列舉一下。
# PyQt5相關模塊 from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * # 動漫圖片制作的業務模塊 import cv2 import sys import os # 日志模塊 from loguru import logger
制作UI界面,創建CartoonUI類,用于初始化圖片轉換器的應用。CartoonUI類繼承自QWidget,因為這里只需要制作一個單頁面的應用,因此,這里只繼承了QWidget。
class CartoonUI(QWidget):
def __init__(self):
'''
初始化UI界面應用
'''
super(CartoonUI, self).__init__()
self.init_ui()
def init_ui(self):
'''
自定義實現的ui應用函數
:return:
'''
self.setWindowTitle('動漫圖片轉換器 公眾號:Python 集中營')
self.setWindowIcon(QIcon('ico.png'))
self.setFixedWidth(500)
hbox = QHBoxLayout()
self.input_image_path = QLineEdit()
self.input_image_path.setPlaceholderText('源圖片路徑')
self.input_image_path.setReadOnly(True)
self.input_image_btn = QPushButton()
self.input_image_btn.setText('導入源圖片')
self.input_image_btn.clicked.connect(self.input_image_btn_click)
self.generate_btn = QPushButton()
self.generate_btn.setText('一鍵生成動漫圖片')
self.generate_btn.clicked.connect(self.generate_btn_click)
hbox.addWidget(self.input_image_path)
hbox.addWidget(self.input_image_btn)
hbox.addWidget(self.generate_btn)
self.thread_ = WorkThread(self)
self.thread_.finished.connect(self.finished)
self.setLayout(hbox)
def input_image_btn_click(self):
'''
input_image_btn按鈕綁定的槽函數,
用于實現打開文件瀏覽項
:return:
'''
im_path = QFileDialog.getOpenFileName(self, os.getcwd(), '打開圖片',
'Image File(*.jpg);;Image File(*.png);;Image File(*.jpeg)')
self.input_image_path.setText(im_path[0])
def generate_btn_click(self):
'''
generate_btn按鈕綁定的槽函數,
用于啟動業務子線程
:return:
'''
self.thread_.start()
self.generate_btn.setEnabled(False)
def finished(self,finished):
'''
接收子線程中finished變量,
判定子線程業務是否執行完成,若執行完成則將按鈕狀態改變為可點擊狀態
:param finished:
:return:
'''
if finished is True:
self.generate_btn.setEnabled(True)創建WorkThread類,繼承自QThread子線程,子線程這里專門用來實現對普通圖片的準換過程。之所以使用子線程來完成業務實現,是因為通常直接在主線程中完成業務會導致主線程異??ㄋ赖那闆r。
單獨使用子線程來完成業務會保證PyQt5的主線程正常的運行,將業務實現和界面應用分離開來。
class WorkThread(QThread):
finished = pyqtSignal(bool)
def __init__(self, parent=None):
'''
子線程初始化函數
:param parent:
'''
super(WorkThread, self).__init__(parent)
self.parent = parent
self.working = True
def __del__(self):
'''
子線程停止函數
:return:
'''
self.working = False
self.wait()
def run(self):
'''
子線程執行函數
:return:
'''
try:
input_picture_name = os.path.basename(self.parent.input_image_path.text().strip())
logger.info(input_picture_name)
output_picture_name = 'cartoon_' + input_picture_name
num_down = 2 # 縮減像素采樣的數目
num_bilateral = 7 # 定義雙邊濾波的數目
img_rgb = cv2.imread(input_picture_name) # 讀取圖片
# 用高斯金字塔降低取樣
img_color = img_rgb
for _ in range(num_down):
img_color = cv2.pyrDown(img_color)
# 重復使用小的雙邊濾波代替一個大的濾波
for _ in range(num_bilateral):
img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
# 升采樣圖片到原始大小
for _ in range(num_down):
img_color = cv2.pyrUp(img_color)
# 轉換為灰度并且使其產生中等的模糊
img_gray = cv2.cvtColor(img_color, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)
# 檢測到邊緣并且增強其效果
img_edge = cv2.adaptiveThreshold(img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
blockSize=9,
C=2)
# 轉換回彩色圖像
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
img_cartoon = cv2.bitwise_and(img_color, img_edge)
# 保存轉換后的圖片
cv2.imwrite(output_picture_name, img_cartoon)
logger.info('動漫圖片轉換完成!')
self.finished.emit(True)
except Exception as e:
logger.error(e)實現完成上述所有的業務之后,需要使用main函數將PyQt5應用加入到主體循環中,這樣整個UI應用就直接拉起了。
if __name__ == '__main__': app = QApplication(sys.argv) main = CartoonUI() main.show() sys.exit(app.exec_())
“基于Python如何實現簡易的動漫圖片轉換器”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。