如何将PIL裁剪后的图像保存到Django中的图像字段

8
我正在尝试将裁剪后的图像保存到模型中,但出现以下错误:
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

这是将裁剪后的图像保存到模型的正确过程吗?如果是,为什么会出现上述错误?
谢谢!
2个回答

9
函数应该长这样:
# The crop function looks like this:

from PIL import Image

from django.core.files.base import ContentFile

def cropper(original_image, crop_coords):
      img_io = StringIO.StringIO()
      original_image = Image.open(original_image)
      cropped_img = original_image.crop((0, 0, 165, 165))
      cropped_img.save(img_io, format='JPEG', quality=100)
      img_content = ContentFile(img_io.getvalue(), 'img5.jpg')
      return img_content

4

对于Python版本>=3.5

from io import BytesIO, StringIO()

img_io = StringIO() # or use BytesIO() depending on the type

除此之外,@phourxx的答案在技术方面表现很棒。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接