0
我重寫的ModelAdmin的save_model
方法將圖像尺寸調整到其正在通過管理頁面上傳650:Django的Python的PIL保存圖像 - 破碎的形象
def save_model(self, request, obj, form, change):
basewidth = 650
img = PIL.Image.open(form.cleaned_data['image_file'])
if img.size[0] > basewidth:
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
img_filefield = getattr(obj, 'image_file')
random_image_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(30)) + '.jpeg'
img.save(random_image_name)
img_filefield.save(random_image_name, ContentFile(img))
obj.save()
else:
obj.save()
它是保存圖像,但圖像如果我打開它,只是一個黑色的圖像"invalid image"
。
我在做什麼錯在上面的代碼?