Python+PyQt5編寫(xiě)圖片格式轉(zhuǎn)換器
功能:使用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í)例教程
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-07Python?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使用pandas 將DataFrame轉(zhuǎn)化成dict
今天小編就為大家分享一篇使用pandas 將DataFrame轉(zhuǎn)化成dict,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12python?plotly設(shè)置go.Scatter為實(shí)線實(shí)例
這篇文章主要為大家介紹了python?plotly設(shè)置go.Scatter為實(shí)線線條的樣式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10pycharm安裝django框架詳細(xì)圖文教程(指定版本)
這篇文章主要給大家介紹了關(guān)于pycharm安裝django框架(指定版本)的相關(guān)資料,PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語(yǔ)言開(kāi)發(fā)時(shí)提高其效率的工具,需要的朋友可以參考下2023-10-10