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)文章
利用Python如何將數(shù)據(jù)寫(xiě)到CSV文件中
在數(shù)據(jù)分析中經(jīng)常需要從csv格式的文件中存取數(shù)據(jù)以及將數(shù)據(jù)寫(xiě)書(shū)到csv文件中。下面這篇文章主要給大家介紹了關(guān)于利用Python如何將數(shù)據(jù)寫(xiě)到CSV文件中的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-06-06
Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)FTP弱口令掃描器的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python代碼操作PowerPoint頁(yè)眉與頁(yè)腳的完整指南
在制作專(zhuān)業(yè)的 PowerPoint 演示文稿時(shí),頁(yè)眉和頁(yè)腳是重要的文檔元素,本文將介紹如何使用 Python 在 PowerPoint 演示文稿中 programmatically 添加和管理頁(yè)眉頁(yè)腳,有需要的小伙伴可以了解下2026-05-05
使用sublime text3搭建Python編輯環(huán)境的實(shí)現(xiàn)
這篇文章主要介紹了使用sublime text3搭建Python編輯環(huán)境的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python構(gòu)建一個(gè)文檔掃描器的實(shí)現(xiàn)
本文主要介紹了Python構(gòu)建一個(gè)文檔掃描器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
詳解Python中的函數(shù)參數(shù)傳遞方法*args與**kwargs
本文將討論P(yáng)ython的函數(shù)參數(shù)。我們將了解args和kwargs,/和的都是什么,雖然這個(gè)問(wèn)題是一個(gè)基本的python問(wèn)題,但是在我們寫(xiě)代碼時(shí)會(huì)經(jīng)常遇到,比如timm中就大量使用了這樣的參數(shù)傳遞方式2023-03-03
python 計(jì)算t分布的雙側(cè)置信區(qū)間
這篇文章主要介紹了python 計(jì)算t分布的雙側(cè)置信區(qū)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

