python图片高清缩小_python批量对图片文件进行尺寸缩小处理

# -*- coding: utf-8 -*-

"""

当前文件夹下图片压缩尺寸到400*225大小以内,生成到outpath下

可在虚拟环境下生成exe执行文件使用

# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Image

# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller

# pyinstaller -F img.py

"""

from PIL import Image

import os

def cut_image(file, outpath):

"""

图片压缩尺寸到400*225大小以内,生成到outpath下

"""

img = Image.open(file)

print(img.size)

(image_x,image_y) = img.size

if not (image_x <= 400 and image_y <= 225):

if (image_x/image_y) > (400/225):

new_image_x = 400

new_image_y = 400 * image_y // image_x

else:

new_image_y = 225

new_image_x = 225 * image_x // image_y

else:

new_image_x = image_x

new_image_y = image_y

new_image_size = (new_image_x,new_image_y)

print(new_image_size)

new_img = img.resize(new_image_size,Image.ANTIALIAS)

new_img.save(outpath+file)

if __name__ == "__main__":

# 当前路径下的所有文件

path = '.'

files = os.listdir(path)

# 生成当以下文件夹下

outpath = './small/'

isExists=os.path.exists(outpath)

if not isExists:

os.makedirs(outpath)

# 对图片文件逐一处理

for file in files:

filename,filetype = os.path.splitext(file)

# print(filename,filetype)

if filetype == '.jpeg' or filetype == '.jpg' or filetype == '.png':

print(file)

cut_image(file, outpath)

# exe生成完后,控制台暂停下

os.system('pause')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值