#-*- coding: utf-8 -*-
importsys#importresource 图标资源可不要from os importpathfrom PIL importImagefrom glob importglobfrom PyQt5 importQtWidgetsfrom PyQt5.QtGui importQIconfrom PyQt5 importQtCore,QtGuifrom PyQt5.QtWidgets importQMessageBoxfrom PyQt5.QtWidgets importQMainWindowfrom PyQt5.QtWidgets importQFileDialogclassUi_Form(QMainWindow):def __init__(self):
super(QtWidgets.QMainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)defsetupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(368, 260)
MainWindow.setFixedSize(368, 260)
MainWindow.setWindowTitle('图像压缩工具')
MainWindow.setWindowIcon(QIcon(':/1.png'))#label标签
self.label =QtWidgets.QLabel(MainWindow)
self.label.setGeometry(QtCore.QRect(10, 30, 341, 31))
self.label.setObjectName("label")
self.label.setText("注:轻度压缩3-6倍左右,重度压缩6-10倍左右")
font=QtGui.QFont()
font.setFamily('微软雅黑')
font.setPointSize(12)
font.setBold(True)
font.setWeight(85)
self.label.setFont(font)
self.pushButton=QtWidgets.QPushButton(MainWindow)
self.pushButton.setGeometry(QtCore.QRect(40, 140, 101, 41))
self.pushButton.setCheckable(False)
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("选择图片重度压缩")
self.pushButton.clicked.connect(self.openfileZ)
self.pushButton_2=QtWidgets.QPushButton(MainWindow)
self.pushButton_2.setGeometry(QtCore.QRect(180, 140, 151, 41))
self.pushButton_2.setCheckable(False)
self.pushButton_2.setText("选择文件夹重度压缩")
self.pushButton_2.clicked.connect(self.opendicZ)
self.pushButton_3=QtWidgets.QPushButton(MainWindow)
self.pushButton_3.setGeometry(QtCore.QRect(40, 90, 101, 41))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_3.setText("选择图片轻度压缩")
self.pushButton_3.clicked.connect(self.openfile)
self.pushButton_4=QtWidgets.QPushButton(MainWindow)
self.pushButton_4.setGeometry(QtCore.QRect(180, 90, 151, 41))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_4.setText("选择文件夹轻度压缩")
self.pushButton_4.clicked.connect(self.opendic)
self.label=QtWidgets.QLabel(MainWindow)
self.label.setGeometry(QtCore.QRect(70, 140, 61, 21))
self.label.setText("")
self.label.setObjectName("label")defretranslateUi(self,MainWindow):
_translate=QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "图像压缩"))defopenfile(self):
filename ,filetype= QFileDialog.getOpenFileName(self,"选择文件","./","All Files (*);;Image Files (*.jpg)")if filename != '':
path=filename.replace(r'/',r'\\')
size= (1024,600)#定义要调整成为的尺寸(PIL会自动根据原始图片的长宽比来缩放适应设置的尺寸)
try:
img= Image.open(path) #打开图片文件
if img.width>5:
img.thumbnail(size, Image.ANTIALIAS)#使用抗锯齿模式生成缩略图(压缩图片)
f=os.path.splitext(path)
newname=f[0] + '-已压缩'newname=newname+f[1]
img.save(newname,"JPEG") #保存成与原文件名一致的文件,会自动覆盖源文件
else:print(file + "宽度小于1200px,无需处理,已忽略")exceptOSError:print(file + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes |QMessageBox.No)defopenfileZ(self):
filename ,filetype= QFileDialog.getOpenFileName(self,"选择文件","./","All Files (*);;Image Files (*.jpg)")if filename != '':
path=filename.replace(r'/',r'\\')
size= (835,470)try:
img=Image.open(path)if img.width>5:
img.thumbnail(size, Image.ANTIALIAS)
f=os.path.splitext(path)
newname=f[0] + '-已压缩'newname=newname+f[1]
img.save(newname,"JPEG")else:print(file + "宽度小于1200px,无需处理,已忽略")exceptOSError:print(file + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes |QMessageBox.No)defopendic(self):
dic= QFileDialog.getExistingDirectory(self,"选择文件夹", "./")if dic != '':
path=dic.replace(r'/',r'\\')
size= (1024, 600)
files= glob( path + "**/*.JPG", recursive=True) + glob(path + "**/*.jpg", recursive=True)
total=len(files)
cur= 1
for infile infiles:try:print("进度:" + str(cur) + "/" + str(total) + " " +infile)
img=Image.open(infile)if img.width>5:
img.thumbnail(size, Image.ANTIALIAS)
f=os.path.splitext(infile)
newname=f[0] + '-已压缩'newname=newname+f[1]
img.save(newname,"JPEG")else:print(infile + "宽度小于1200px,无需处理,已忽略")
cur= cur + 1
exceptOSError:print(infile + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes |QMessageBox.No)defopendicZ(self):
dic= QFileDialog.getExistingDirectory(self,"选择文件夹", "./")if dic != '':
path=dic.replace(r'/',r'\\')
size= (835, 470)#glob.glob()用来进行模糊查询,增加参数recursive=True后可以使用**/来匹配所有子目录
files = glob( path + "**/*.JPG", recursive=True) + glob(path + "**/*.jpg", recursive=True)
total=len(files)
cur= 1
for infile infiles:try:print("进度:" + str(cur) + "/" + str(total) + " " +infile)
img=Image.open(infile)if img.width>5:
img.thumbnail(size, Image.ANTIALIAS)#分离文件名和后缀
f=os.path.splitext(infile)
newname=f[0] + '-已压缩'newname=newname+f[1]
img.save(newname,"JPEG")else:print(infile + "宽度小于1200px,无需处理,已忽略")
cur= cur + 1
exceptOSError:print(infile + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes |QMessageBox.No)if __name__ == '__main__':
app=QtWidgets.QApplication(sys.argv)
MainWindow=QtWidgets.QMainWindow()
ui=Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())