解决方案

使用Python+PyQt5编写图片(.ico)格式转换器

seo靠我 2023-09-24 16:29:41

功能:使用Python将任意图片格式转换成  .ico图标 格式

没错!!!就是看不惯某些资本,换个图片格式还收费!

一、使用到的模块

这里使用到两个模块:PyQt5和Image,前面这个需要手动安装,后面SEO靠我这个一般是自带得 pip install Image pip install PyQt5

二、python代码

注意:

        这里做了一个PyQt5的可视化界面方便使用,如果不想用SEO靠我PyQt5,也可以根据我的代码提示,将图片转换的那部分代码提出来做函数调用,也可以实现。

#图片格式转换器 import sys from PyQt5.QtWidgetsSEO靠我 import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout, QPushButton,QMessageBox, QFileDialoSEO靠我g,QDesktopWidget from PyQt5.QtGui import QIcon from PIL import Image class SEO靠我ImageConverter(QMainWindow): def __init__(self): super().__init__() self.iniSEO靠我tUI() def initUI(self): self.setWindowTitle(图片格式转换器) self.resize(500, 200) SEO靠我 self.center() self.setWindowIcon(QIcon(icon.png)) self.input_label = QLabeSEO靠我l(需要转换的图片:) self.output_label = QLabel(输出路径:) self.input_path_label = QLabel() SEO靠我 self.output_path_label = QLabel() self.select_input_button = QPushButton(先择图片) SEO靠我self.select_output_button = QPushButton(先择输出路径) self.convert_button = QPushButton(开始转换) SEO靠我 layout = QVBoxLayout() layout.addWidget(self.input_label) layout.addWidget(selfSEO靠我.input_path_label) layout.addWidget(self.select_input_button) layout.addWidget(self.SEO靠我output_label) layout.addWidget(self.output_path_label) layout.addWidget(self.select_SEO靠我output_button) layout.addWidget(self.convert_button) widget = QWidget() widSEO靠我get.setLayout(layout) self.setCentralWidget(widget) self.select_input_button.clickeSEO靠我d.connect(self.select_input_image) self.select_output_button.clicked.connect(self.select_outSEO靠我put_path) self.convert_button.clicked.connect(self.convert_image) self.setStyleSheeSEO靠我t( QLabel { font-size: 16px; margin-bottom: 10px; } QPushButSEO靠我ton { font-size: 16px; padding: 10px; } ) def center(self): SEO靠我 screen = QDesktopWidget().screenGeometry() size = self.geometry() # (屏幕的宽-窗口SEO靠我的宽)/2 self.move(int((screen.width() - size.width()) / 2), int((screen.height() - size.heightSEO靠我()) / 2)) def select_input_image(self): file_dialog = QFileDialog() file_diSEO靠我alog.setNameFilter(Images (*.png *.jpg *.jpeg *.bmp *.gif)) file_dialog.setFileMode(QFileDiaSEO靠我log.ExistingFile) if file_dialog.exec_(): selected_files = file_dialog.selectedFilesSEO靠我() self.input_path_label.setText(selected_files[0]) def select_output_path(self): SEO靠我 file_dialog = QFileDialog() file_dialog.setAcceptMode(QFileDialog.AcceptSave) SEO靠我 file_dialog.setDefaultSuffix(ico) if file_dialog.exec_(): selected_files = file_diaSEO靠我log.selectedFiles() self.output_path_label.setText(selected_files[0]) #这里是图片转换的部分,可以SEO靠我不加入PyQt5的模块,单独把下面的函数复制出去做修改也可以转换 def convert_image(self): input_path = self.input_paSEO靠我th_label.text() #这里是需要转换的图片的路径 output_path = self.output_path_label.text() #这里是转换好的图片输出路径 SEO靠我 if input_path and output_path: #判断连个参数是否都存在 image = Image.open(input_path) #通过路径读取图片 SEO靠我 #保存到输出路径 ,并且格式为 “ICO”,大小为32X32 image.save(output_path, format=ICO, sizes=[(32, 32)]) SEO靠我 self.input_path_label.setText() self.output_path_label.setText() self.showSEO靠我_message_dialog(Conversion Successful, Image converted to ICO format.) def show_message_diaSEO靠我log(self, title, message): msg_box = QMessageBox() msg_box.setWindowTitle(title) SEO靠我 msg_box.setText(message) msg_box.exec_() if __name__ == __main__: app SEO靠我= QApplication(sys.argv) converter = ImageConverter() converter.show() sys.eSEO靠我xit(app.exec_())

三、运行结果样式

“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2