(如想转载,请联系博主或贴上本博地址)

此为学习pyqt5的第一个程序,图像压缩工具。

因为比较简单,下面直接贴上代码。

效果图如下:

# -*- coding: utf-8 -*-
import sys
#import resource 图标资源可不要
from os import path
from PIL import Image
from glob import glob
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore,QtGui
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QFileDialog
class Ui_Form(QMainWindow):
def __init__(self):
super(QtWidgets.QMainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self) def setupUi(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") def retranslateUi(self,MainWindow):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("MainWindow", "图像压缩")) def openfile(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,无需处理,已忽略")
except OSError:
print(file + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes | QMessageBox.No) def openfileZ(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,无需处理,已忽略")
except OSError:
print(file + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes | QMessageBox.No) def opendic(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 in files:
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
except OSError:
print(infile + "文件错误,忽略")
QMessageBox.information(self,"恭喜,成功了!", "已成功压缩图片到原有目录",QMessageBox.Yes | QMessageBox.No) def opendicZ(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 in files:
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
except OSError:
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_())

python之pyqt5-第一个pyqt5程序-图像压缩工具-小记的更多相关文章

  1. python练习册 每天一个小程序 第0013题

    # -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...

  2. python练习册 每天一个小程序 第0001题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生 ...

  3. python练习册 每天一个小程序 第0000题

    PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...

  4. python练习册 每天一个小程序 第0007题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但 ...

  5. python练习册 每天一个小程序 第0010题

    # -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...

  6. python练习册 每天一个小程序 第0009题

    1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with ope ...

  7. python练习册 每天一个小程序 第0008题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 一个HTML文件,找出里面的正文. 6 7 思路: 8 利用Beautiful ...

  8. python练习册 每天一个小程序 第0006题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...

  9. python练习册 每天一个小程序 第0005题

    1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目说明: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小 ...

  10. python练习册 每天一个小程序 第0012题

    # -*-coding:utf-8-*- def test(content): text = content flag = 0 with open('filtered_words.txt') as f ...

随机推荐

  1. 使用chrome自带的实时字幕功能

    https://www.youtube.com/watch?v=upm9qCMT96Q

  2. CF79D 题解

    题解 传送门 你有 \(n\) 个灯泡,一开始都未点亮. 同时你有 \(l\) 个长度,分别为 \(a_1 \sim a_l\). 每次你可以选择一段连续的子序列,且长度为某个 \(a_i\),并将这 ...

  3. tomcat 2 - 默认连接器精简版

    tomcat 将一个包中所有类使用的错误信息存储在 properties 文件中,每个包有一个  properties 文件.每个 properties 文件都是用 org.apache.catali ...

  4. py打包工具

    库地址: auto-py-to-exe https://pypi.org/project/auto-py-to-exe/ Gooey https://pypi.org/project/Gooey/ 为 ...

  5. 手动实现IOC

    spring官方实现的ioc是通过反射和xml技术实现的,下面我们可以根据这个思路简单实现一下IOC,此处省略构建项目,项目的整体结构如图 第一步,在resources目录下创建beans.xml文件 ...

  6. spider_object_01使用正则爬取百度贴吧所有内容保存成html

    """本案例不涉及数据提取,仅指导 网页分页爬取的两种方式 思路非第一种:利用while Ture,传参,然后在设定一个判断条件,案例中用的是如果找不到下一页,循环退出( ...

  7. CF1561D Up the Strip

    Up the Strip 题意 你现在在 \(n\) 号格子,你需要跳到 \(1\) 号格子,你可以有两种跳法: 你可以做减法,即选择一个数 \(k\in [1,n)\) ,从 \(n\) 跳到 \( ...

  8. git拉取远程主支内容,在本地进行修改后,重新提交到新建分支的过程

    git拉取远程主支内容,在本地进行修改后,重新提交到新建分支的过程 在本地找一个干净的文件夹 git  init  进行初始化 git clone 复制拉取远程的地址 在文件夹中打开,进入复制下来的项 ...

  9. Windows如何生成公钥和私钥

    Windows如何生成公钥和私钥 方法一)使用git命令 一. 首先安装git二. 桌面上右键 Git Bash Here三. 命令ssh-keygen -t rsa然后 一直enter 四. 将公钥 ...

  10. 1903021126-申文骏 实验一 19信计java-Markdown排版

    项目 内容 课程班级博客链接 19级信计班 作业要求链接 实验一 课程学习目标 大致学会Markdown排版 任务1:在博客园平台注册个人博客账号和加入班级博客 注册了博客园的个人账号,提交了博客申请 ...