欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+PyQt5編寫(xiě)圖片格式轉(zhuǎn)換器

 更新時(shí)間:2023年07月19日 11:45:54   作者:W金剛葫蘆娃W  
這篇文章主要為大家詳細(xì)介紹了如何利用Python和PyQt5編寫(xiě)一個(gè)簡(jiǎn)單的圖片格式轉(zhuǎn)換器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下

功能:使用Python將任意圖片格式轉(zhuǎn)換成  .ico圖標(biāo) 格式

沒(méi)錯(cuò)!?。【褪强床粦T某些資本,換個(gè)圖片格式還收費(fèi)!

一、使用到的模塊

這里使用到兩個(gè)模塊:PyQt5和Image,前面這個(gè)需要手動(dòng)安裝,后面這個(gè)一般是自帶得

pip install Imagepip install PyQt5

二、python代碼

注意:

這里做了一個(gè)PyQt5的可視化界面方便使用,如果不想用PyQt5,也可以根據(jù)我的代碼提示,將圖片轉(zhuǎn)換的那部分代碼提出來(lái)做函數(shù)調(diào)用,也可以實(shí)現(xiàn)。

#圖片格式轉(zhuǎn)換器
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('圖片格式轉(zhuǎn)換器')
        self.resize(500, 200)
        self.center()
 
        self.setWindowIcon(QIcon('icon.png'))
 
        self.input_label = QLabel('需要轉(zhuǎn)換的圖片:')
        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('開(kāi)始轉(zhuǎn)換')
 
        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])
     #這里是圖片轉(zhuǎn)換的部分,可以不加入PyQt5的模塊,單獨(dú)把下面的函數(shù)復(fù)制出去做修改也可以轉(zhuǎn)換
    def convert_image(self): 
        input_path = self.input_path_label.text()  #這里是需要轉(zhuǎn)換的圖片的路徑
        output_path = self.output_path_label.text()  #這里是轉(zhuǎn)換好的圖片輸出路徑
        if input_path and output_path:          #判斷連個(gè)參數(shù)是否都存在
            image = Image.open(input_path)      #通過(guò)路徑讀取圖片
             #保存到輸出路徑 ,并且格式為 “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_())

三、運(yùn)行結(jié)果樣式

到此這篇關(guān)于Python+PyQt5編寫(xiě)圖片格式轉(zhuǎn)換器的文章就介紹到這了,更多相關(guān)Python圖片格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

    wxPython是一款集成了Python的圖形化類庫(kù)的工具,而py2exe是一款將Python程序轉(zhuǎn)換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來(lái)學(xué)習(xí)Windows中使用wxPython和py2exe開(kāi)發(fā)Python的GUI程序的實(shí)例教程:
    2016-07-07
  • Python?Flask實(shí)現(xiàn)后臺(tái)任務(wù)輕松構(gòu)建高效API應(yīng)用

    Python?Flask實(shí)現(xiàn)后臺(tái)任務(wù)輕松構(gòu)建高效API應(yīng)用

    本文介紹如何使用Python?Flask框架實(shí)現(xiàn)后臺(tái)任務(wù),以快速構(gòu)建高效的API應(yīng)用。通過(guò)實(shí)例演示,讀者將學(xué)會(huì)如何利用Flask框架搭建后臺(tái)任務(wù),實(shí)現(xiàn)異步處理和多線程操作等高級(jí)功能,提升應(yīng)用性能和用戶體驗(yàn)
    2023-04-04
  • 詳解python eval函數(shù)的妙用

    詳解python eval函數(shù)的妙用

    這篇文章主要介紹了詳解python eval函數(shù)的妙用,詳細(xì)介紹了python eval函數(shù)的具體用法和實(shí)例,有興趣的可以了解一下
    2017-11-11
  • python 文件操作api(文件操作函數(shù))

    python 文件操作api(文件操作函數(shù))

    總是記不住API。昨晚寫(xiě)的時(shí)候用到了這些,但是沒(méi)記住,于是就索性整理一下吧,方便需要的朋友
    2016-08-08
  • 使用pandas 將DataFrame轉(zhuǎn)化成dict

    使用pandas 將DataFrame轉(zhuǎn)化成dict

    今天小編就為大家分享一篇使用pandas 將DataFrame轉(zhuǎn)化成dict,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python 列表反轉(zhuǎn)顯示的四種方法

    Python 列表反轉(zhuǎn)顯示的四種方法

    這篇文章主要介紹了Python 列表反轉(zhuǎn)顯示的四種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python?plotly設(shè)置go.Scatter為實(shí)線實(shí)例

    python?plotly設(shè)置go.Scatter為實(shí)線實(shí)例

    這篇文章主要為大家介紹了python?plotly設(shè)置go.Scatter為實(shí)線線條的樣式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • pycharm安裝django框架詳細(xì)圖文教程(指定版本)

    pycharm安裝django框架詳細(xì)圖文教程(指定版本)

    這篇文章主要給大家介紹了關(guān)于pycharm安裝django框架(指定版本)的相關(guān)資料,PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語(yǔ)言開(kāi)發(fā)時(shí)提高其效率的工具,需要的朋友可以參考下
    2023-10-10
  • Python之py2exe打包工具詳解

    Python之py2exe打包工具詳解

    下面小編就為大家?guī)?lái)一篇Python之py2exe打包工具詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Python3.9新特性詳解

    Python3.9新特性詳解

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

最新評(píng)論