前段时间写了个比较简单的批量水印增加的python实现形式,将某个文件夹上面的图片全副增加上水印。

【浏览全文】

明天正好有工夫就做了一个UI利用的封装,这样不须要晓得python间接下载exe的应用程序应用即可。

有须要'批量图片水印增加器'的敌人能够间接跳过到文章开端获取下载方式,下载.exe的可执行利用间接应用即可,上面次要来介绍一下实现过程。

首先,还是老规矩介绍一下在开发过程中须要用到的python非标准库,因为这些库都是之前应用过的。

所以这里就间接导入到代码块中,如果没有的话间接应用pip的形式进行装置即可。

# It imports all the classes, attributes, and methods of the PyQt5.QtCore module into the global symbol table.from PyQt5.QtCore import *# It imports all the classes, attributes, and methods of the PyQt5.QtWidgets module into the global symbol table.from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QTextBrowser, QLineEdit, QPushButton, \    QFormLayout, QFileDialog, QLabel# It imports all the classes, attributes, and methods of the PyQt5.QtGui module into the global symbol table.from PyQt5.QtGui import QIcon, QFont, QTextCursor# It imports the sys module.import sys# It imports the os module.import os# It imports the logger from the loguru module.from loguru import logger# It imports the add_mark function from the marker module in the watermarker package.from watermarker.marker import add_mark

以上导入的python库就是在整个UI桌面利用开发过程中须要用到的,实现间接咱们新建UI类PicWaterUI专门用来写一些对于桌面利用的布局。

其中包含按钮、输入框等组件,此外将组件关联的槽函数也写入到这个类中,这样有利于对立治理,代码量比拟多有须要的敌人急躁浏览。

# This class is a widget that contains a QLabel and a QPushButtonclass PicWaterUI(QWidget):    def __init__(self):        """        A constructor. It is called when an object is created from a class and it allows the class to initialize the        attributes of a class.        """        super(PicWaterUI, self).__init__()        self.init_ui()    def init_ui(self):        """        This function initializes the UI.        """        self.setWindowTitle('批量图片水印增加器  公众号:Python 集中营!')        self.setWindowIcon(QIcon('water.ico'))        self.brower = QTextBrowser()        self.brower.setFont(QFont('宋体', 8))        self.brower.setReadOnly(True)        self.brower.setPlaceholderText('解决过程展现区域...')        self.brower.ensureCursorVisible()        self.pic_file_path = QLineEdit()        self.pic_file_path.setPlaceholderText('源批量图片门路')        self.pic_file_path.setReadOnly(True)        self.pic_file_path_btn = QPushButton()        self.pic_file_path_btn.setText('源图片门路')        self.pic_file_path_btn.clicked.connect(self.pic_file_path_btn_click)        self.new_pic_file_path = QLineEdit()        self.new_pic_file_path.setPlaceholderText('新图片存储门路')        self.new_pic_file_path.setReadOnly(True)        self.new_pic_file_path_btn = QPushButton()        self.new_pic_file_path_btn.setText('保留门路')        self.new_pic_file_path_btn.clicked.connect(self.new_pic_file_path_btn_click)        self.water_current_label = QLabel()        self.water_current_label.setText('水印内容:')        self.water_current_in = QLineEdit()        self.water_current_in.setPlaceholderText('Python 集中营')        self.water_angle_label = QLabel()        self.water_angle_label.setText('水印角度:')        self.water_angle_in = QLineEdit()        self.water_angle_in.setPlaceholderText('30')        self.water_back_label = QLabel()        self.water_back_label.setText('水印透明度:')        self.water_back_in = QLineEdit()        self.water_back_in.setPlaceholderText('0.3')        self.water_font_label = QLabel()        self.water_font_label.setText('水印字体大小:')        self.water_font_in = QLineEdit()        self.water_font_in.setPlaceholderText('30')        self.water_space_label = QLabel()        self.water_space_label.setText('水印距离:')        self.water_space_in = QLineEdit()        self.water_space_in.setPlaceholderText('40')        self.water_color_label = QLabel()        self.water_color_label.setText('水印色彩:')        self.water_color_in = QLineEdit()        self.water_color_in.setPlaceholderText('#8B8B1B')        self.start_btn = QPushButton()        self.start_btn.setText('开始增加水印')        self.start_btn.clicked.connect(self.start_btn_click)        hbox = QHBoxLayout()        hbox.addWidget(self.brower)        fbox = QFormLayout()        fbox.addRow(self.pic_file_path, self.pic_file_path_btn)        fbox.addRow(self.new_pic_file_path, self.new_pic_file_path_btn)        fbox.addRow(self.water_current_label, self.water_current_in)        fbox.addRow(self.water_angle_label, self.water_angle_in)        fbox.addRow(self.water_back_label, self.water_back_in)        fbox.addRow(self.water_font_label, self.water_font_in)        fbox.addRow(self.water_space_label, self.water_space_in)        fbox.addRow(self.water_color_label, self.water_color_in)        v_vbox = QVBoxLayout()        v_vbox.addWidget(self.start_btn)        vbox = QVBoxLayout()        vbox.addLayout(fbox)        vbox.addLayout(v_vbox)        hbox.addLayout(vbox)        self.thread_ = PicWaterThread(self)        self.thread_.message.connect(self.show_message)        self.thread_.finished.connect(self.finshed)        self.setLayout(hbox)    def show_message(self, text):        """        It shows a message        :param text: The text to be displayed        """        cursor = self.brower.textCursor()        cursor.movePosition(QTextCursor.End)        self.brower.append(text)        self.brower.setTextCursor(cursor)        self.brower.ensureCursorVisible()    def pic_file_path_btn_click(self):        """        It opens a file dialog box and allows the user to select a file.        """        pic_file_path = QFileDialog.getExistingDirectory(self, '抉择文件夹', os.getcwd())        self.pic_file_path.setText(pic_file_path)    def new_pic_file_path_btn_click(self):        """        This function opens a file dialog box and allows the user to select a file to save the output to.        """        new_pic_file_path = QFileDialog.getExistingDirectory(self, '抉择文件夹', os.getcwd())        self.new_pic_file_path.setText(new_pic_file_path)    def start_btn_click(self):        """        A function that is called when the start button is clicked.        """        self.thread_.start()        self.start_btn.setEnabled(False)    def finshed(self, finished):        """        :param finished: A boolean value that is True if the download is finished, False otherwise        """        if finished is True:            self.start_btn.setEnabled(True)

页面布局及组件局部实现之后就是业务的具体实现局部了,业务就是实现批量增加水印的成果。

这里新建了一个PicWaterThread类作为UI桌面利用的子线程专门将业务实现的局部写到这个类中。

业务局部和主线程间接拆散时,一来从代码层面上看起来比拟明了,二来在子线程执行业务比较慢的状况下不至于导致主线程呈现卡死的状况产生。

为了达到业务和界面拆散的成果,上面PicWaterThread子线程的run函数外面就是具体的业务实现局部。

# This class is a subclass of QThread, and it's used to watermark picturesclass PicWaterThread(QThread):    # A signal that is emitted when a message is received.    message = pyqtSignal(str)    # A signal that is emitted when the download is finished.    finished = pyqtSignal(bool)    def __init__(self, parent=None):        """        A constructor that initializes the class.        :param parent: The parent widget        """        super(PicWaterThread, self).__init__(parent)        self.working = True        self.parent = parent    def __del__(self):        """        A destructor. It is called when the object is destroyed.        """        self.working = True        self.wait()    def run(self) -> None:        """        > This function runs the program        """        try:            directory = self.parent.pic_file_path.text().strip()            water_name = self.parent.water_current_in.text().strip()            new_directory = self.parent.new_pic_file_path.text().strip()            water_angle_in = self.parent.water_angle_in.text().strip()            water_back_in = self.parent.water_back_in.text().strip()            water_font_in = self.parent.water_font_in.text().strip()            water_space_in = self.parent.water_space_in.text().strip()            color = self.parent.water_color_in.text().strip()            self.message.emit('源文件门路:{}'.format(directory))            self.message.emit('水印内容:{}'.format(water_name))            self.message.emit('保留文件门路:{}'.format(new_directory))            self.message.emit('水印角度:{}'.format(water_angle_in))            self.message.emit('水印透明度:{}'.format(water_back_in))            self.message.emit('水印字体大小:{}'.format(water_font_in))            self.message.emit('水印距离:{}'.format(water_space_in))            self.message.emit('水印色彩:{}'.format(color))            if directory is None or water_name is None:                logger.info('文件夹地址或水印名称不能为空!')                return            for file_name in os.listdir(directory):                logger.info('以后文件名称:{0},行将开始增加水印操作!'.format(file_name))                self.message.emit('以后文件名称:{0},行将开始增加水印操作!'.format(file_name))                add_mark(file=os.path.join(directory, file_name), out=new_directory, mark=water_name,                         opacity=float(water_back_in), angle=int(water_angle_in), space=int(water_space_in),                         size=int(water_font_in), color=color)                self.message.emit('以后文件名称:{0},曾经实现增加水印操作!'.format(file_name))                logger.info('以后文件名称:{0},曾经实现增加水印操作!'.format(file_name))            self.finished.emit(True)        except Exception as e:            self.message.emit('文件内容读取或格式化产生异样!')            self.finished.emit(True)

实现业务以及页面利用的开发之后,咱们应用main函数将整个桌面利用调起来,这种范式基本上每个桌面利用的应用是一样的。

如果须要难看一些的话还能够加上咱们之前提到过的各种款式主题的利用,在公众号主页上进行搜寻就能够找到之前发表的相干的文章。

if __name__ == '__main__':    app = QApplication(sys.argv)    main = PicWaterUI()    main.show()    sys.exit(app.exec_())

最初,咱们找了两张斗罗大陆'唐三'的照片测试一下成果如何,应用下面的main函数启动整个利用之后是怎么的。

大家能够间接在利用下面抉择须要批量增加水印的图片门路以及增加实现后须要保留的中央。

并且能够在生成时在桌面利用上调整水印相干的各种参数,包含水印的大小、尺寸、距离、色彩等等,这样就能够依据本人的须要对批量图片制作属于的水印成果了。

上面是将'唐三'的照片通过该页面转换当前的成果了,基本上满足我对少量图片增加雷同的水印的要求了。

接下来看一下'修罗唐三'被咱们增加了'Python 集中营'的水印当前变成什么样了。

有须要.exe可执行利用的敌人在公众号内间接回复'批量图片水印增加器'获取网盘的下载链接,利用咱们曾经打包好了有需要的话间接下载即可。

当前字符串中的字符提取校验就用这个了,成果不错!

为不便数据分析,实现Python对象与DataFrame数据的互相转换!

python数据分析透视表,定制你的剖析计算需要!