PYTQ + ico图标制作
2019-07-11 本文已影响16人
时尚灬IT男
PYTQ + ico图标制作
在使用QT,或者pyqt的过程中,经常会因为没有图标而烦恼。(把自己喜欢的图片转换成ico格式,用在程序的图标显示上)
先看下效果图:
image.png
于是就用pyqt制作一个转ico的工具,方便使用。
这里使用到PythonMagick库文件:这里介绍了这个库文件的安装
接下来就是上代码了:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'imageToIco.ui'
#
# Created by: PyQt5 UI code generator 5.7.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
import PythonMagick
global_path = "C:/"
global_ico = []
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(758, 591)
MainWindow.setMinimumSize(QtCore.QSize(758, 591))
MainWindow.setMaximumSize(QtCore.QSize(758, 591))
MainWindow.setStyleSheet("background-color: rgb(203, 242, 255);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(19, 79, 721, 481))
self.groupBox.setStyleSheet("background-color: rgb(249, 249, 249)")
self.groupBox.setTitle("")
self.groupBox.setObjectName("groupBox")
self.lineEdit = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit.setGeometry(QtCore.QRect(110, 30, 481, 21))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(30, 30, 72, 21))
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.groupBox)
self.pushButton.setGeometry(QtCore.QRect(610, 30, 71, 21))
self.pushButton.setStyleSheet("background-color: rgb(203, 242, 255);")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(610, 70, 71, 21))
self.pushButton_2.setStyleSheet("background-color: rgb(203, 242, 255);")
self.pushButton_2.setObjectName("pushButton_2")
self.tableWidget = QtWidgets.QTableWidget(self.groupBox)
self.tableWidget.setGeometry(QtCore.QRect(30, 111, 661, 351))
self.tableWidget.setStyleSheet("background-color: rgb(255, 255, 255);")
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
self.label_3 = QtWidgets.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(30, 70, 72, 21))
self.label_3.setObjectName("label_3")
self.lineEdit_w = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_w.setGeometry(QtCore.QRect(110, 70, 61, 21))
self.lineEdit_w.setObjectName("lineEdit_w")
self.label_4 = QtWidgets.QLabel(self.groupBox)
self.label_4.setGeometry(QtCore.QRect(180, 70, 31, 21))
self.label_4.setObjectName("label_4")
self.lineEdit_h = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_h.setGeometry(QtCore.QRect(200, 70, 61, 21))
self.lineEdit_h.setObjectName("lineEdit_h")
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(20, 10, 721, 61))
self.groupBox_2.setTitle("")
self.groupBox_2.setObjectName("groupBox_2")
self.label_2 = QtWidgets.QLabel(self.groupBox_2)
self.label_2.setGeometry(QtCore.QRect(230, 10, 271, 41))
self.label_2.setObjectName("label_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.openFile)
self.pushButton_2.clicked.connect(self.toIco)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "WB_转ico"))
MainWindow.setWindowIcon(QtGui.QIcon("wb.ico"))
self.label.setText(_translate("MainWindow", "PDF文件:"))
self.pushButton.setText(_translate("MainWindow", "选取文件"))
self.pushButton_2.setText(_translate("MainWindow", "开始转换"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "源文件"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "尺寸"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "备注"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "下载"))
self.label_3.setText(_translate("MainWindow", "尺 寸:"))
self.lineEdit_w.setText(_translate("MainWindow", "128"))
self.label_4.setText(_translate("MainWindow", "X"))
self.lineEdit_h.setText(_translate("MainWindow", "128"))
self.tableWidget.horizontalHeader().setStyleSheet("QHeaderView::section {background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,\
stop:0 #00007f, stop: 0.5 #00007f,stop: 0.6 #00007f, stop:1 #00007f);color: white;}")
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:20pt; font-weight:600; color:#55aa00;\">图片转ico</span></p></body></html>"))
self.tableWidget.setColumnWidth(0, 300)
self.tableWidget.setColumnWidth(1, 100)
self.tableWidget.setColumnWidth(2, 100)
def openFile(self):
# fileName = QtWidgets.QFileDialog.getOpenFileName(self, "选择文件", self.directory, "Text Files(*.xml)")
global global_path
fileName, filetype = QtWidgets.QFileDialog.getOpenFileName(self.centralwidget,
"选取文件",
global_path,
"PNG Files(*.png);;JPEG Files(*.jpg)") # 设置文件扩展名过滤,注意用双分号间隔
if fileName == '':
print('取消')
return
# print(os.path.basename( fileName) ) # 返回文件名
# print(os.path.dirname(fileName)) # 返回目录路径
# print(os.path.split(fileName)) # 分割文件名与路径
# print(os.path.join(os.path.dirname(fileName), 'runoob.txt')) # 将目录和文件名合成一个路径
global_path = os.path.dirname(fileName)
self.lineEdit.setText(fileName)
def toIco(self):
global global_ico
if self.lineEdit.text() == "":
QtWidgets.QMessageBox.warning(self.centralwidget, "提示", "请选取转换文件")
return
if not os.path.exists(self.lineEdit.text()):
QtWidgets.QMessageBox.warning(self.centralwidget, "提示", "文件不存在!")
return
if self.lineEdit_w.text() == '' or self.lineEdit_h.text() == '':
QtWidgets.QMessageBox.warning(self.centralwidget, "提示", "请输入尺寸!")
return
path = self.lineEdit.text()
size = self.lineEdit_w.text() + "x" + self.lineEdit_h.text()
index = self.tableWidget.rowCount()
try:
img = PythonMagick.Image(path)
# 这里要设置一下尺寸,不然会报ico尺寸异常错误
img.sample(size)
global_ico.append(img)
self.tableWidget.setRowCount(index + 1)
self.tableWidget.setItem(index, 0, QtWidgets.QTableWidgetItem(path))
self.tableWidget.setItem(index, 1, QtWidgets.QTableWidgetItem(size))
self.tableWidget.setItem(index, 2, QtWidgets.QTableWidgetItem("成功"))
item1 = QtWidgets.QPushButton(self.centralwidget)
item1.setObjectName(str(index))
item1.clicked.connect(self.save_ico)
item1.setText("下载")
self.tableWidget.setCellWidget(self.tableWidget.rowCount() - 1, 3, item1)
except:
self.tableWidget.setRowCount(index + 1)
self.tableWidget.setItem(index, 0, QtWidgets.QTableWidgetItem(path))
self.tableWidget.setItem(index, 1, QtWidgets.QTableWidgetItem(size))
self.tableWidget.setItem(index, 2, QtWidgets.QTableWidgetItem("失败"))
QtWidgets.QMessageBox.warning(self.centralwidget, "提示", "转换失败!")
def save_ico(self):
global global_ico
global global_path
directory = QtWidgets.QFileDialog.getExistingDirectory(self.centralwidget,
"保存路径",
global_path) # 起始路径
if directory == '':
print('取消')
return
new_path = directory
index = int(self.tableWidget.sender().objectName())
name = os.path.basename( self.tableWidget.item(index,0).text()) # 返回文件名
name_list = name.split('.')
name = name_list[0] + '.ico'
global_ico[index].write(os.path.join(new_path , name))
if __name__ =='__main__':
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
如果代码都不想看,直接下载打包好的程序:ico图标制作
提取码:2bam