Ai
1 Star 0 Fork 0

alphardex/python-gadgets

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
batch_compress_image.py 665 Bytes
一键复制 编辑 原始数据 按行查看 历史
alphardex 提交于 2020-11-13 17:06 +08:00 . update
"""
批量压缩图片
"""
from pathlib import Path
from PIL import Image
ext = 'jpg'
thumb_size = (240, 120)
thumb_ratio = 10
images = [path for path in Path('.').glob(f'*.{ext}')]
def compress_fixed_size():
for image in images:
im = Image.open(image)
im.thumbnail(thumb_size)
im.save(image)
def compress_fixed_ratio():
for (i, image) in enumerate(images):
im = Image.open(image)
w, h = im.size
print(w, h)
nw, nh = int(w/thumb_ratio), int(h/thumb_ratio)
print(nw, nh)
thumbnail = im.resize((nw, nh), Image.ANTIALIAS)
thumbnail.save(f'{i}.jpg')
compress_fixed_ratio()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/alphardex/python-gadgets.git
git@gitee.com:alphardex/python-gadgets.git
alphardex
python-gadgets
python-gadgets
master

搜索帮助