python file对象属性_使用PIL时,“ JpegImageFile”对象没有属性“ ...

我正在使用PIL压缩上传的图像(FileField).但是我遇到一个错误,我认为这是双重保存的问题? (保存我的图像,然后保存包括该图像的整个表单).我要在保存图像时执行commit = False,但似乎没有可能.这是我的代码:

...

if form_post.is_valid():

instance = form_post.save(commit=False)

instance.user = request.user

if instance.image:

filename = instance.image

instance.image = Image.open(instance.image)

instance.image.thumbnail((220, 130), Image.ANTIALIAS)

instance.image.save(filename, quality=60)

instance.save()

返回’JpegImageFile’对象的最后一行没有属性’_committed’错误(instance.save())

有人可以找出问题所在吗? -知道我该如何解决吗?

完整回溯:

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner

41. response = get_response(request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response

187. response = self.process_exception_by_middleware(e, request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response

185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view

23. return view_func(request, *args, **kwargs)

File "/Users/zorgan/Desktop/project/site/post/views.py" in post

68. if uploaded_file_type(instance) is True:

File "/Users/zorgan/Desktop/project/site/functions/helper_functions.py" in uploaded_file_type

12. f = file.image.read(1024)

Exception Type: AttributeError at /post/

Exception Value: 'JpegImageFile' object has no attribute 'read'

完整型号:

class Post(models.Model):

user = models.ForeignKey(User, blank=True, null=True)

title = models.TextField(max_length=95)

image = models.FileField(null=True, blank=True)

以及随附的PostForm:

class PostForm(forms.ModelForm):

title = forms.TextInput(attrs={'placeholder': 'title'})

class Meta:

model = Post

fields = [

'user',

'title',

'image',

]

views.py

def post(request):

if request.user.is_authenticated():

form_post = PostForm(request.POST or None, request.FILES or None)

if form_post.is_valid():

instance = form_post.save(commit=False)

if instance.image:

filename = instance.image

instance.image = Image.open(instance.image)

instance.image.thumbnail((220, 130), Image.ANTIALIAS)

instance.image.save(filename, quality=60)

instance.save()

return HttpResponseRedirect('/home/')

else:

form_post = PostForm()

context = {

'form_post': form_post,

}

return render(request, 'post/post.html', context)

else:

return HttpResponseRedirect("/accounts/signup/")

下面的代码:

if instance.image:

im = Image.open(instance.image)

print("Filename:", im.filename) #doesn't print anything

thumb = im.thumbnail((220, 130), Image.ANTIALIAS)

thumb.save(im.filename, quality=60)

返回AttributeError:’NoneType’对象没有属性’save’.我相信这是因为im.filename不输出任何内容.知道为什么吗?

另一种方法:

if instance.image:

im = Image.open(instance.image)

thumb = im.thumbnail((220, 130), Image.ANTIALIAS)

thumb_io = BytesIO()

thumb.save(thumb_io, im.format, quality=60)

instance.image.save(im.filename, ContentFile(thumb_io.get_value()), save=False)

还返回AttributeError:’NoneType’对象在此行上没有属性’save’:thumb.save(thumb_io,im.format,quality = 60).不知道为什么吗?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值