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程序的實例教程
wxPython是一款集成了Python的圖形化類庫的工具,而py2exe是一款將Python程序轉換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來學習Windows中使用wxPython和py2exe開發(fā)Python的GUI程序的實例教程:2016-07-07python?plotly設置go.Scatter為實線實例
這篇文章主要為大家介紹了python?plotly設置go.Scatter為實線線條的樣式實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10