python+pyqt5实现图片批量缩放工具实现图片批量缩放工具
批量修改图片大小好像用PS也可以,不过我不会,程序猿就用程序来解决。
这段时间学了下Python,很强大,之前一些不知道怎么处理的东西在Python里面都能找到解决方法。
工具界面如下图
这个工具需要用到第三方库 Pillow 和 Pyqt5,可通过命令行安装。
pip install pillow
pip install pyqt5
代码:
#!-*-coding:utf-8-*-
from PIL import Image
import hashlib, os, sys
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import (QFileDialog, QMessageBox,
QGridLayout, QLineEdit, QTextEdit, QLabel, QWidget, QToolTip,
QPushButton, QApplication, QInputDialog)
from PyQt5.QtCore import QCoreApplication
def makedir(path):
if not os.path.exists(path):
os.mkdir(path)
def image_resize(image_path, scale):
#获得新文件名称
out_path = os.path.splitext(image_path)[0] + "_after" + os.path.splitext(image_path)[-1] with Image.open( image_path ) as
im:
width = int(im.size[0]*scale)
height = int(im.size[1]*scale)
#resize 设置新尺寸
out = im.resize( (width, height) )
out.save(out_path)
def folder_resize(folder_path, scale):
dst_files = [] files = os.listdir( folder_path )
for file in files:
ext = os.path.splitext(file)[-1] #只支持jpg或png文件
if ext == ".jpg" or ext == ".png":
dst_files.append(file)
#输出路径
out_folder = os.path.join(folder_path, "after")
makedir(out_folder)
for item in dst_files:
try:
op = os.path.join( out_folder, item )
with Image.open( os.path.join(folder_path, item) ) as im:
width = int(im.size[0]*scale)