将枕头图像保存到模型中

时间:2019-02-10 21:29:31

标签: python django django-models django-views python-imaging-library

我正在尝试将枕头图像保存到模型中,以便模型可以处理。这是我的模型的样子:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    avatar = models.ImageField(storage=OverwriteStorage(), upload_to=create_user_image_path, validators=[validate_avatar],
                               default="default/avatars/avatar.jpg")

    def save(self, *args, **kwargs):
        super(Profile, self).save(*args, **kwargs)
        if self.avatar:
            image = Image.open(self.avatar)
            height, width = image.size

            if height == 200 and width == 200:
                image.close()
                return

            image = image.resize((200, 200), Image.ANTIALIAS)
            image.save(self.avatar.path)
            image.close()

模型将调整图像大小并相应保存。我正在使用allauth通过适配器获取已注册社交帐户的图像,如下所示:

    avatar_url = sociallogin.account.get_avatar_url()
    avatar = create_avatar(avatar_url)
    profile.avatar = avatar
    profile.save()

def create_avatar(avatar_url):
    response = requests.get(avatar_url)
    avatar = Image.open(BytesIO(response.content))
    return avatar

但是,我收到一条错误消息:AttributeError: 'JpegImageFile' object has no attribute '_committed'我在做什么错了,我该如何解决?

0 个答案:

没有答案