Djangoフォームを介してアップロードされた画像ファイルのサイズを変更する最も簡単な方法は何ImageField
ですか?
34943 次
2 に答える
31
完全に機能する例が見つからず、あちこちでほんの少ししか見つからなかったのでイライラしました。私はなんとか自分で解決策をまとめることができました。これが完全な例です。これをフォームのclean()メソッドに入れます(まったく同じ方法で、モデルのsave()メソッドをオーバーライドすることもできます-ImageFieldのファイルプロパティを変更します)。
import StringIO
from PIL import Image
image_field = self.cleaned_data.get('image_field')
image_file = StringIO.StringIO(image_field.read())
image = Image.open(image_file)
w, h = image.size
image = image.resize((w/2, h/2), Image.ANTIALIAS)
image_file = StringIO.StringIO()
image.save(image_file, 'JPEG', quality=90)
image_field.file = image_file
于 2013-03-20T12:21:35.280 に答える
7
PIL を使用して、YourModel.save() メソッドで画像のサイズを変更できます。
例:
http://djangosaur.tumblr.com/post/422589280/django-resize-thumbnail-image-pil
http://davedash.com/2009/02/21/resizing-image-on-upload-in-django/
于 2013-03-20T09:33:25.827 に答える