如何在 django 模型中使用 PIL
我喜欢在最终存储之前调整上传的图像(ImageField)的大小,我听说 python 有一个名为 PIL 的图像库,我想用它来完成该任务,但我不确定如何开始。
关于如何做有什么建议吗?
谢谢
I like to resize an uploaded image(ImageField) before finally storing, I heard that python has an image library called PIL and I would like to use it do that task but I'm not sure on how to start.
Any suggestions on how to do it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以覆盖模型的保存功能,您可以在其中打开文件并调整其大小(不推荐,因为每次保存模型时它都会调整其大小),您可以在文件上传后调整大小(例如在 form.save() 之前/期间),
但恕我直言更好的解决方案是为此使用专用应用程序,我最喜欢的是 sorl-thumbnails
you can override model's save function where you can open file and resize it (not recommended, as it will resize it each time you save a model), you an resize after file upload (e.g. before/during form.save())
but IMHO a far better solution is to use a dedicated app for this, my favourite is sorl-thumbnails
我刚刚知道如何做到这一点,但有没有办法简化它?我是 python 和 django 的新手,所以我不确定这是否是正确的方法。
下面是我的代码:
谢谢
I've just found out on how to do it but is there a way to simplify it? I'm new in python and django so I'm not sure if this is the proper way to do it.
below is my code:
Thanks