在保存时调整图像大小 [英] resize image on save

查看:126
本文介绍了在保存时调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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ðmundurH:
这不会工作,因为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆