python怎么在上传图片后压缩图片大小
PHPz
PHPz 2017-05-18 10:52:27
[Python讨论组]

我用的是flask框架,图片处理用的是pillow

一般上传都是在循环files,然后逐个file.save()
我希望在save完成后,执行pillow的压缩逻辑。

但是似乎save是一个I/O操作,存在延迟性,如果直接在file.save()下面直接调用pillowImage.open,会出错,因为图片数据还没有写入图片。

咋办?

PHPz
PHPz

学习是最好的投资!

全部回复(2)
阿神

其实这类图片处理,直接使用阿里云的OSS或者七牛等类似的存储功能更好,直接将图片上传到OOS中,然后调用特别的后缀进行指定的图片处理,未来也访问OSS上处理后的地址。这样既可以规避用自己服务器处理图片的负荷,而且也降低了访问的压力,对于降低程序的复杂度也是大有好处的。

某草草

楼主看看Image.open 的fp参数,也可以A filename (string), pathlib.Path object or a file object PIL.Image.open(fp, mode='r')

你直接传file给Image.open(file)就可以了吧!

PIL.Image.open(fp, mode='r')
Opens and identifies the given image file.

This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method). See new().

Parameters:    
fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
mode – The mode. If given, this argument must be “r”.
Returns:    
An Image object.

Raises:    
IOError – If the file cannot be found, or the image cannot be opened and identified.
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号