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如何将数据写到CSV文件中

    利用Python如何将数据写到CSV文件中

    在数据分析中经常需要从csv格式的文件中存取数据以及将数据写书到csv文件中。下面这篇文章主要给大家介绍了关于利用Python如何将数据写到CSV文件中的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-06-06
  • 探索Python列表合并技术提高代码灵活性

    探索Python列表合并技术提高代码灵活性

    本文将深入研究Python中列表合并的几种方法,通过详细的示例代码和细致的解释,呈现一场关于列表操作的精彩探险,无论是初学者还是有经验的开发者,通过学习本文,将更加熟练地运用这些方法,提升代码的效率和可读性
    2024-01-01
  • Python实现FTP弱口令扫描器的方法示例

    Python实现FTP弱口令扫描器的方法示例

    这篇文章主要介绍了Python实现FTP弱口令扫描器的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Python代码操作PowerPoint页眉与页脚的完整指南

    Python代码操作PowerPoint页眉与页脚的完整指南

    在制作专业的 PowerPoint 演示文稿时,页眉和页脚是重要的文档元素,本文将介绍如何使用 Python 在 PowerPoint 演示文稿中 programmatically 添加和管理页眉页脚,有需要的小伙伴可以了解下
    2026-05-05
  • python采集博客中上传的QQ截图文件

    python采集博客中上传的QQ截图文件

    这篇文章主要介绍了python采集博客中上传的QQ截图文件,因为文件名包含中文会在某些时候乱码,例如这件的文件名QQ截图20120926174732,所以需要采集出来修改掉,需要的朋友可以参考下
    2014-07-07
  • 使用sublime text3搭建Python编辑环境的实现

    使用sublime text3搭建Python编辑环境的实现

    这篇文章主要介绍了使用sublime text3搭建Python编辑环境的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python构建一个文档扫描器的实现

    Python构建一个文档扫描器的实现

    本文主要介绍了Python构建一个文档扫描器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 详解Python中的函数参数传递方法*args与**kwargs

    详解Python中的函数参数传递方法*args与**kwargs

    本文将讨论Python的函数参数。我们将了解args和kwargs,/和的都是什么,虽然这个问题是一个基本的python问题,但是在我们写代码时会经常遇到,比如timm中就大量使用了这样的参数传递方式
    2023-03-03
  • Python中支持向量机SVM的使用方法详解

    Python中支持向量机SVM的使用方法详解

    这篇文章主要为大家详细介绍了Python中支持向量机SVM的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • python 计算t分布的双侧置信区间

    python 计算t分布的双侧置信区间

    这篇文章主要介绍了python 计算t分布的双侧置信区间,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04

最新评论