我正在让自己发疯,试图让它与 Pillow 一起工作。这就是我的观点。
new_business = form.save(request.user, commit=False)
if 'image' in request.FILES:
from PIL import Image
img = Image.open(form.cleaned_data['image'])
if img.mode != "RGB":
img = img.convert("RGB")
filename = str(new_business.image)
filename = filename.rsplit(".", 1)[0]
filename = filename + '.jpg'
img.save(filename)
from django.core.files.base import ContentFile
djangofile = ContentFile(img.tobytes())
new_business.image.save(filename, djangofile)
new_business.save()
state = 'Service Business changes were successful.'
new_business.image 是一个 ImageField。这将在适当的文件夹中保存一个带有 .jpg 的文件,但它是一个损坏的文件!