首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python 图像文件压缩,使用PIL库对图像进行等比例压缩。

Python 图像文件压缩,使用PIL库对图像进行等比例压缩。

作者头像
繁依Fanyi
发布2023-05-07 19:42:39
发布2023-05-07 19:42:39
9350
举报

题目

图像文件压缩。使用PIL库对图像进行等比例压缩,无论压缩前文件大小如何,压缩后文件大小小于10KB。

代码

代码语言:javascript
复制
from PIL import Image
import os
from tkinter import filedialog
import tkinter

f_path = filedialog.askopenfilename()
image = Image.open(f_path)
size = os.path.getsize(f_path)/1024
width, height = image.size
while True:
    if size>10:
        width,height = round(width*0.9), round(height*0.9)
        image = image.resize((width,height),Image.ANTIALIAS)
        image.save(f_path)
        size = os.path.getsize(f_path)/1024
    else:
        break
str_info = "压缩完成,现在大小是{}KB".format(size)
print(str_info)

运行后效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 代码
  • 运行后效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档