Django管理面板图片上传优化

时间:2017-07-25 08:11:38

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

我想优化用户从管理网站上传的图片,并存储原始图片以及优化图片

def save_model(self, request, obj, form, change):
    if obj.image:
        filename = obj.image.name.split('.')[:-1]
        obj.save()
        img = Image.open(obj.image)
        print "Before Size: ", img.size
        w, h = img.size
        size = min(800.0 / w, 800.0 / h)
        w, h = int(w * size), int(h * size)
        print "After Size:", w, h
        img = img.resize((w, h), Image.BICUBIC)
        path = obj.image.path.split('\\')[:-1]
        new_path = "\\".join(path) + "\\{}.jpeg".format("".join(filename))
        img.save('{}'.format(new_path))
        obj.image = img
        obj.save()

收到错误

 AttributeError: _committed 

任何有用的建议都将不胜感激!

0 个答案:

没有答案