Python+PyQt5编写图片格式转换器

 更新时间:2023年07月19日 11:45:54   作者:W金刚葫芦娃W  
这篇文章主要为大家详细介绍了如何利用Python和PyQt5编写一个简单的图片格式转换器,文中的示例代码讲解详细,感兴趣的小伙伴可以动手尝试一下

功能:使用Python将任意图片格式转换成  .ico图标 格式

没错!!!就是看不惯某些资本,换个图片格式还收费!

一、使用到的模块

这里使用到两个模块:PyQt5和Image,前面这个需要手动安装,后面这个一般是自带得

pip install Imagepip install PyQt5

二、python代码

注意:

这里做了一个PyQt5的可视化界面方便使用,如果不想用PyQt5,也可以根据我的代码提示,将图片转换的那部分代码提出来做函数调用,也可以实现。

#图片格式转换器
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout, QPushButton,QMessageBox, QFileDialog,QDesktopWidget
from PyQt5.QtGui import QIcon
from PIL import Image
 
class ImageConverter(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('图片格式转换器')
        self.resize(500, 200)
        self.center()
 
        self.setWindowIcon(QIcon('icon.png'))
 
        self.input_label = QLabel('需要转换的图片:')
        self.output_label = QLabel('输出路径:')
 
        self.input_path_label = QLabel('')
        self.output_path_label = QLabel('')
 
        self.select_input_button = QPushButton('先择图片')
        self.select_output_button = QPushButton('先择输出路径')
        self.convert_button = QPushButton('开始转换')
 
        layout = QVBoxLayout()
        layout.addWidget(self.input_label)
        layout.addWidget(self.input_path_label)
        layout.addWidget(self.select_input_button)
        layout.addWidget(self.output_label)
        layout.addWidget(self.output_path_label)
        layout.addWidget(self.select_output_button)
        layout.addWidget(self.convert_button)
 
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)
 
        self.select_input_button.clicked.connect(self.select_input_image)
        self.select_output_button.clicked.connect(self.select_output_path)
        self.convert_button.clicked.connect(self.convert_image)
 
        self.setStyleSheet('''
            QLabel {
                font-size: 16px;
                margin-bottom: 10px;
            }
            QPushButton {
                font-size: 16px;
                padding: 10px;
            }
        ''')
    def center(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        # (屏幕的宽-窗口的宽)/2
        self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.height()) / 2))
 
    def select_input_image(self):
        file_dialog = QFileDialog()
        file_dialog.setNameFilter('Images (*.png *.jpg *.jpeg *.bmp *.gif)')
        file_dialog.setFileMode(QFileDialog.ExistingFile)
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.input_path_label.setText(selected_files[0])
 
    def select_output_path(self):
        file_dialog = QFileDialog()
        file_dialog.setAcceptMode(QFileDialog.AcceptSave)
        file_dialog.setDefaultSuffix('ico')
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.output_path_label.setText(selected_files[0])
     #这里是图片转换的部分,可以不加入PyQt5的模块,单独把下面的函数复制出去做修改也可以转换
    def convert_image(self): 
        input_path = self.input_path_label.text()  #这里是需要转换的图片的路径
        output_path = self.output_path_label.text()  #这里是转换好的图片输出路径
        if input_path and output_path:          #判断连个参数是否都存在
            image = Image.open(input_path)      #通过路径读取图片
             #保存到输出路径 ,并且格式为 “ICO”,大小为32X32
            image.save(output_path, format='ICO', sizes=[(32, 32)])
 
            self.input_path_label.setText('')
            self.output_path_label.setText('')
 
            self.show_message_dialog('Conversion Successful', 'Image converted to ICO format.')
 
    def show_message_dialog(self, title, message):
        msg_box = QMessageBox()
        msg_box.setWindowTitle(title)
        msg_box.setText(message)
        msg_box.exec_()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    converter = ImageConverter()
    converter.show()
    sys.exit(app.exec_())

三、运行结果样式

到此这篇关于Python+PyQt5编写图片格式转换器的文章就介绍到这了,更多相关Python图片格式转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python文本情感分类识别基于SVM算法Django框架实现

    Python文本情感分类识别基于SVM算法Django框架实现

    这篇文章主要为大家介绍了Python文本情感分类识别基于SVM算法Django框架实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 使用TensorFlow实现SVM

    使用TensorFlow实现SVM

    这篇文章主要为大家详细介绍了使用TensorFlow实现SVM的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • django model去掉unique_together报错的解决方案

    django model去掉unique_together报错的解决方案

    本文给大家分享的是在使用django model去掉unique_together时报错的解决思路和具体步骤,提供给大家参考下,希望对大家学习使用django能够有所帮助
    2016-10-10
  • python xlsxwriter创建excel图表的方法

    python xlsxwriter创建excel图表的方法

    这篇文章主要为大家详细介绍了python xlsxwriter创建excel图表的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Windows上安装tensorflow  详细教程(图文详解)

    Windows上安装tensorflow 详细教程(图文详解)

    这篇文章主要介绍了Windows上安装TENSORFLOW 详细教程,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • python2.7的flask框架之引用js&css等静态文件的实现方法

    python2.7的flask框架之引用js&css等静态文件的实现方法

    今天小编就为大家分享一篇python2.7的flask框架之引用js&css等静态文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python获取某一天是星期几的方法示例

    Python获取某一天是星期几的方法示例

    这篇文章主要介绍了Python获取某一天是星期几的方法,结合完整实例形式分析了Python针对日期与时间的相关计算技巧,需要的朋友可以参考下
    2017-01-01
  • 使用Python的xlrd和xlwt操作Excel详解

    使用Python的xlrd和xlwt操作Excel详解

    这篇文章主要介绍了使用Python的xlrd和xlwt对比操作Excel详解,xlrd和xlwt是python的第三方库,xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入,需要的朋友可以参考下
    2023-08-08
  • python回调函数用法实例分析

    python回调函数用法实例分析

    这篇文章主要介绍了python回调函数用法,较为详细的分析了常用的调用方式,并实例介绍了Python回调函数的使用技巧,需要的朋友可以参考下
    2015-05-05
  • 新年快乐! python实现绚烂的烟花绽放效果

    新年快乐! python实现绚烂的烟花绽放效果

    这篇文章主要为大家详细介绍了python利用可视化技巧实现烟花绽放效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01

最新评论