Django 实现前端图片压缩功能的方法
作者:HeJD 时间:2022-11-05 09:05:16
思路:
<img alt="" src="/img/图片真实地址" ></img>
上面是一个典型的HTML中的图片,在django中,src对应的path会经过url进行过滤处理,所以可以通过写view过滤器的方式来实现图片压缩,代码如下
url
url(r'^img/(?P<path>(.+))/$',img),
view
def img(request,path):
im=Image.open(settings.STATIC_ROOT+path)
x,y=im.size
im=im.resize((int(x/1.5),int(y/1.5)),Image.ANTIALIAS)
response = HttpResponse(mimetype="image/png")
#将PIL的image对象写入response中,通过response返回
im.save(response, "PNG")
return response
结论:通过类似于filter的方法可以实现图片压缩,且下一个请求该图片时,会直接调用缓存,并不会再次进行图片压缩。
注:对性能的影响会在验证完成后上传
来源:https://blog.csdn.net/hjd199464/article/details/54378977
标签:Django,前端,图片,压缩
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 图像处理之PIL库详解用法
2023-05-02 18:06:50
![](https://img.aspxhome.com/file/2023/1/90671_0s.jpg)
php注册系统和使用Xajax即时验证用户名是否被占用
2023-09-12 05:27:55
![](https://img.aspxhome.com/file/2023/4/55524_0s.png)
详解Python对某地区二手房房价数据分析
2022-04-07 04:47:30
![](https://img.aspxhome.com/file/2023/6/118766_0s.jpg)
Python用requests模块实现动态网页爬虫
2022-10-15 16:27:06
![](https://img.aspxhome.com/file/2023/2/127312_0s.gif)
跟老齐学Python之啰嗦的除法
2022-12-18 12:47:38
vue组件实现可搜索下拉框扩展
2024-05-28 15:41:57
![](https://img.aspxhome.com/file/2023/5/123205_0s.gif)
Python比较两个日期的两种方法详解
2023-12-25 03:52:17
![](https://img.aspxhome.com/file/2023/6/86396_0s.png)
Mootools 1.2教程(13)——正则表达式
2008-12-07 20:25:00
Pycharm 操作Django Model的简单运用方法
2022-05-09 14:07:15
![](https://img.aspxhome.com/file/2023/9/110259_0s.jpg)
python3.6使用urllib完成下载的实例
2023-08-03 21:21:13
Go实现文件上传和下载
2023-06-19 07:42:32
![](https://img.aspxhome.com/file/2023/6/86276_0s.png)
MSSQL 附加数据库提示“错误 823”数据恢复实操
2024-01-23 20:31:45
![](https://img.aspxhome.com/file/2023/3/99963_0s.png)
python使用PIL实现多张图片垂直合并
2023-10-28 12:19:45
Typecho插件实现添加文章目录的方法详解
2023-05-25 07:19:04
Python操作串口的方法
2021-11-24 07:09:10
详解python中的异常捕获
2021-11-21 00:52:20
python GUI库图形界面开发之PyQt5工具栏控件QToolBar的详细使用方法与实例
2021-08-14 20:28:28
![](https://img.aspxhome.com/file/2023/7/110457_0s.png)
centos6.4下python3.6.1安装教程
2021-11-03 05:44:03
![](https://img.aspxhome.com/file/2023/3/88983_0s.png)
Django之全局使用request.user.username的实例详解
2021-07-20 20:50:42
python正则表达式常见的知识点汇总
2023-10-02 18:56:21
![](https://img.aspxhome.com/file/2023/1/99651_0s.jpg)