亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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圖片格式轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實例教程

    Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實例教程

    wxPython是一款集成了Python的圖形化類庫的工具,而py2exe是一款將Python程序轉換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來學習Windows中使用wxPython和py2exe開發(fā)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語言開發(fā)時提高其效率的工具,需要的朋友可以參考下
    2023-10-10
  • Python之py2exe打包工具詳解

    Python之py2exe打包工具詳解

    下面小編就為大家?guī)硪黄狿ython之py2exe打包工具詳解。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Python3.9新特性詳解

    Python3.9新特性詳解

    這篇文章主要介紹了Python3.9新特性詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10

最新評論