保存时调整图像大小 [英] resize image on save
问题描述
在 Django 中上传图像后,如何轻松调整图像大小?我使用的是 Django 1.0.2 并且我已经安装了 PIL.
How can I easily resize an image after it has been uploaded in Django? I am using Django 1.0.2 and I've installed PIL.
我正在考虑覆盖模型的 save() 方法来调整它的大小,但我真的不知道如何开始并覆盖它.
I was thinking about overriding the save() method of the Model to resize it, but I don't really know how to start out and override it.
有人能指出我正确的方向吗?谢谢:-)
Can someone point me in the right direction? Thanks :-)
@Guðmundur H:这不起作用,因为 django-stdimage 包在 Windows 上不起作用:-(
@Guðmundur H: This won't work because the django-stdimage package does not work on Windows :-(
推荐答案
您应该使用一种方法来处理上传的文件,如 Django 文档.
You should use a method to handle the uploaded file, as demonstrated in the Django documentation.
在这种方法中,您可以连接变量中的块(而不是直接将它们写入磁盘),从该变量创建一个 PIL 图像,调整图像大小并将其保存到磁盘.
In this method, you could concatenate the chunks in a variable (rather than writing them to disk directly), create a PIL Image from that variable, resize the image and save it to disk.
在 PIL 中,您应该查看 Image.fromstring
和 Image.resize
.
In PIL, you should look at Image.fromstring
and Image.resize
.
这篇关于保存时调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!