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图片格式转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Windows中使用wxPython和py2exe开发Python的GUI程序的实例教程

    Windows中使用wxPython和py2exe开发Python的GUI程序的实例教程

    wxPython是一款集成了Python的图形化类库的工具,而py2exe是一款将Python程序转换为exe可执行文件的程序,二者搭配可以轻松地在Windows中创建图形化程序,这里我们就来学习Windows中使用wxPython和py2exe开发Python的GUI程序的实例教程:
    2016-07-07
  • Python Flask实现后台任务轻松构建高效API应用

    Python Flask实现后台任务轻松构建高效API应用

    本文介绍如何使用Python Flask框架实现后台任务,以快速构建高效的API应用。通过实例演示,读者将学会如何利用Flask框架搭建后台任务,实现异步处理和多线程操作等高级功能,提升应用性能和用户体验
    2023-04-04
  • 详解python eval函数的妙用

    详解python eval函数的妙用

    这篇文章主要介绍了详解python eval函数的妙用,详细介绍了python eval函数的具体用法和实例,有兴趣的可以了解一下
    2017-11-11
  • python 文件操作api(文件操作函数)

    python 文件操作api(文件操作函数)

    总是记不住API。昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧,方便需要的朋友
    2016-08-08
  • 使用pandas 将DataFrame转化成dict

    使用pandas 将DataFrame转化成dict

    今天小编就为大家分享一篇使用pandas 将DataFrame转化成dict,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Python 列表反转显示的四种方法

    Python 列表反转显示的四种方法

    这篇文章主要介绍了Python 列表反转显示的四种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • python plotly设置go.Scatter为实线实例

    python plotly设置go.Scatter为实线实例

    这篇文章主要为大家介绍了python plotly设置go.Scatter为实线线条的样式实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • pycharm安装django框架详细图文教程(指定版本)

    pycharm安装django框架详细图文教程(指定版本)

    这篇文章主要给大家介绍了关于pycharm安装django框架(指定版本)的相关资料,PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,需要的朋友可以参考下
    2023-10-10
  • Python之py2exe打包工具详解

    Python之py2exe打包工具详解

    下面小编就为大家带来一篇Python之py2exe打包工具详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Python3.9新特性详解

    Python3.9新特性详解

    这篇文章主要介绍了Python3.9新特性详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10

最新评论