功能:使用Python将任意图片格式转换成 .ico图标 格式
没错!!!就是看不惯某些资本,换个图片格式还收费!
一、使用到的模块
这里使用到两个模块:PyQt5和Image,前面这个需要手动安装,后面这个一般是自带得
pip install Image
pip 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_())
三、运行结果样式
![](https://img-blog.csdnimg.cn/0469ba9680594ea79aa349696e07bf97.png)
|