In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python+PyQT5 to achieve hand-drawn picture generator", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python+PyQT5 to achieve a hand-drawn picture generator.
The whole part of the UI interface code block, UI interface design is relatively simple. The effect is shown in the picture above.
Class HandImage (QWidget): def _ _ init__ (self): super (HandImage Self). _ init__ () self.init_ui () def init_ui (self):''UI interface components and layout: return:''self.setWindowTitle (' hand-painted picture generator official account: [Python concentration camp]') self.setWindowIcon (QIcon ('hand-drawn icon .ico') self. SetFixedWidth self.sou_im_path = QLineEdit () self.sou_im_path.setReadOnly (True) self.sou_im_path_btn = QPushButton () self.sou_im_path_btn.setText ('source picture') 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 ('store') self.dir_path_btn.clicked.connect (self.dir_path_btn_clk) self.start_btn = QPushButton () self.start_btn.setText ('start drawing') 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) grid.addWidget (self.dir_path, 1,0,1,1) grid.addWidget (self.dir_path_btn, 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) # slot function def sou_im_path_btn_clk (self) on the UI interface:''select the source image and set the path: return:' 'im_path = QFileDialog.getOpenFileName (self, os.getcwd (),' Open Picture' 'Image File (* .jpg) Image File (* .png)') self.sou_im_path.setText (im_path [0]) def dir_path_btn_clk (self): 'Select the storage path and set the path: return:' 'dir_path = QFileDialog.getExistingDirectory (self, os.getcwd () 'Select a path') self.dir_path.setText (dir_path) def start_btn_clk (self):''start the slot function bound by the button: return:' 'self.start_btn.setEnabled (False) self.thread_.start () def finished (self Finished):''slot function for child threads to pass completion signals: param finished: signal variable: return:''if finished is True: self.start_btn.setEnabled (True)
Among them, there are only two third-party libraries used in drawing, the main one is Pillow image processing library, and the other is numpy scientific computing library for some array calculation and other operations.
Import a third-party processing library into a code block
From PIL import Image # Image processing Module import numpy as np # Scientific Computing Library # PyQt5 Interface creation and style, Core component from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * # Application basic Operation related import sysimport os
Create a subthread class for special hand-drawn images to separate the processing logic of the UI interface from the processing logic of the generated image so as not to produce an unresponsive jam state.
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): # Source image path sou_im_path = self.parent.sou_im_path.text (). Strip () # Storage path dir_path = Self.parent.dir_path.text () .strip () if sou_im_path =''or dir_path = ='': self.finished.emit (True) return # Open the image that needs to be transferred And carry on the parameter setting, take out some gradient values of the main image of the parameters. Finally, the array is saved. Vals = np.asarray (Image.open (sou_im_path). Convert ('L')). Astype ('float')' Image Parameter processing''depth = 12.0 # set the initialization depth gray_vals = np.gradient (vals) # to extract the grayscale gray_x of the image Gray_y = gray_vals # extract the gray value of Abscissa and ordinate separately print ('gray value of current Abscissa:', gray_x) print ('grayscale of current ordinate:' Gray_y) # reset the gray value of Abscissa and ordinate gray_x = gray_x * depth / 100.0 gray_y = gray_y * depth / 100.0 # calculate the square root of Abscissa and ordinate gray values according to numpy.sqrt () function gray_sqrt = np.sqrt (gray_x * * 2 + gray_y * * 2 + 1.0) # recalculate the X axis, Y-axis and Z-axis light sources light_x = gray_x / gray_sqrt light_y = gray_y / gray_sqrt light_z = 1.0 / gray_sqrt # calculate the azimuth angle and the overlooking angle agnle_el = np.pi / 2.2 # overlooking angle agnle_az = np.pi / 4. # calculate the azimuth angle of the light source to the X axis, Y axis, Influence of Z-axis dx = np.cos (agnle_el) * np.cos (agnle_az) # influence of light source on x-axis dy = np.cos (agnle_el) * np.sin (agnle_az) # influence of light source on y-axis dz = np.sin (agnle_el) # influence of light source on z-axis # normalization of light source light = 255* (dx * light_x + dy * light_y + dz * light_z) light = light.clip (0 Image = Image.fromarray (light.astype ('uint8')) image.save (dir_path +' / hand-drawn image .jpg') self.finished.emit (True) print ('hand-drawn image complete!')
The main code block implementations are all above, and the complete code will be shown below
Complete code
#-*-coding:utf-8-*-# @ author Python concentration Camp # @ date 2022 hand-painted picture generator: take Xuerong Rong as an example, one-click generation. # hand-painted picture generator can generate hand-painted pictures from imported color images through python analysis of light sources, grayscale and other operations. # among them, there are only two third-party libraries used for drawing, the main one is Pillow image processing library, and the other is numpy scientific computing library for some array calculation and other operations. # Import the third-party processing library into the code block from PIL import Image # Image processing module import numpy as np # Scientific Computing Library # PyQt5 interface creation and style, core component from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * # apply basic operation related import sysimport os# to create subthread classes for specialized hand-drawn images Separating the processing logic of the UI interface from the processing logic of the image generation will not result in an unresponsive jam state. 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): # Source image path sou_im_path = self.parent.sou_im_path.text (). Strip () # Storage path dir_path = Self.parent.dir_path.text () .strip () if sou_im_path =''or dir_path = ='': self.finished.emit (True) return # Open the image that needs to be transferred And carry on the parameter setting, take out some gradient values of the main image of the parameters. Finally, the array is saved. Vals = np.asarray (Image.open (sou_im_path). Convert ('L')). Astype ('float')' Image Parameter processing''depth = 12.0 # set the initialization depth gray_vals = np.gradient (vals) # to extract the grayscale gray_x of the image Gray_y = gray_vals # extract the gray value of Abscissa and ordinate separately print ('gray value of current Abscissa:', gray_x) print ('grayscale of current ordinate:' Gray_y) # reset the gray value of Abscissa and ordinate gray_x = gray_x * depth / 100.0 gray_y = gray_y * depth / 100.0 # calculate the square root of Abscissa and ordinate gray values according to numpy.sqrt () function gray_sqrt = np.sqrt (gray_x * * 2 + gray_y * * 2 + 1.0) # recalculate the X axis, Y-axis and Z-axis light sources light_x = gray_x / gray_sqrt light_y = gray_y / gray_sqrt light_z = 1.0 / gray_sqrt # calculate the azimuth angle and the overlooking angle agnle_el = np.pi / 2.2 # overlooking angle agnle_az = np.pi / 4. # calculate the azimuth angle of the light source to the X axis, Y axis, Influence of Z-axis dx = np.cos (agnle_el) * np.cos (agnle_az) # influence of light source on x-axis dy = np.cos (agnle_el) * np.sin (agnle_az) # influence of light source on y-axis dz = np.sin (agnle_el) # influence of light source on z-axis # normalization of light source light = 255* (dx * light_x + dy * light_y + dz * light_z) light = light.clip (0 Image = Image.fromarray (light.astype ('uint8')) image.save (dir_path +' / hand-drawn image .jpg') self.finished.emit (True) print ('hand-drawn image complete!') # the whole part of the UI interface code block, UI interface design is relatively simple. The effect is shown in the picture below. Class HandImage (QWidget): def _ _ init__ (self): super (HandImage Self). _ init__ () self.init_ui () def init_ui (self):''UI interface components and layout: return:''self.setWindowTitle (' hand-painted picture generator official account: [Python concentration camp]') self.setWindowIcon (QIcon ('hand-drawn icon .ico') self. SetFixedWidth self.sou_im_path = QLineEdit () self.sou_im_path.setReadOnly (True) self.sou_im_path_btn = QPushButton () self.sou_im_path_btn.setText ('source picture') 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 ('store') self.dir_path_btn.clicked.connect (self.dir_path_btn_clk) self.start_btn = QPushButton () self.start_btn.setText ('start drawing') 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) grid.addWidget (self.dir_path, 1,0,1,1) grid.addWidget (self.dir_path_btn, 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) # slot function def sou_im_path_btn_clk (self) on the UI interface:''select the source image and set the path: return:' 'im_path = QFileDialog.getOpenFileName (self, os.getcwd (),' Open Picture' 'Image File (* .jpg) Image File (* .png)') self.sou_im_path.setText (im_path [0]) def dir_path_btn_clk (self): 'Select the storage path and set the path: return:' 'dir_path = QFileDialog.getExistingDirectory (self, os.getcwd () 'Select a path') self.dir_path.setText (dir_path) def start_btn_clk (self):''start the slot function bound by the button: return:' 'self.start_btn.setEnabled (False) self.thread_.start () def finished (self Finished):''slot function for child threads to pass completion signals: param finished: signal variable: 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_ ()) so far I believe that everyone on "how to use Python+PyQT5 to achieve hand-drawn picture generator" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.