我正在尝试将裁剪后的图像保存到模型中,但出现以下错误:
Traceback (most recent call last): File "/mypath/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/mypath/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view return view_func(request, *args, **kwargs) File "/mypath/views.py", line 236, in player_edit player.save() File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 734, in save force_update=force_update, update_fields=update_fields) File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 762, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 824, in _save_table for f in non_pks] File "/mypath/lib/python2.7/site-packages/django/db/models/fields/files.py", line 313, in pre_save if file and not file._committed: File "/mypath/lib/python2.7/site-packages/PIL/Image.py", line 512, in getattr raise AttributeError(name) AttributeError: _committed
处理表单提交的视图如下:
裁剪功能长这样:
这是将裁剪后的图像保存到模型的正确过程吗?如果是,为什么会出现上述错误?
谢谢!
Traceback (most recent call last): File "/mypath/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/mypath/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view return view_func(request, *args, **kwargs) File "/mypath/views.py", line 236, in player_edit player.save() File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 734, in save force_update=force_update, update_fields=update_fields) File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 762, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/mypath/lib/python2.7/site-packages/django/db/models/base.py", line 824, in _save_table for f in non_pks] File "/mypath/lib/python2.7/site-packages/django/db/models/fields/files.py", line 313, in pre_save if file and not file._committed: File "/mypath/lib/python2.7/site-packages/PIL/Image.py", line 512, in getattr raise AttributeError(name) AttributeError: _committed
处理表单提交的视图如下:
if request.method == 'POST':
form = PlayerForm(request.POST, request.FILES, instance=current_player)
if form.is_valid():
temp_image = form.cleaned_data['profile_image2']
player = form.save()
cropped_image = cropper(temp_image, crop_coords)
player.profile_image = cropped_image
player.save()
return redirect('player')
裁剪功能长这样:
from PIL import Image
import Image as pil
def cropper(original_image, crop_coords):
original_image = Image.open(original_image)
original_image.crop((0, 0, 165, 165))
original_image.save("img5.jpg")
return original_image
这是将裁剪后的图像保存到模型的正确过程吗?如果是,为什么会出现上述错误?
谢谢!