Python 将图像作为文件传递到ImageField而不保存
我有一个模型,我们叫它Python 将图像作为文件传递到ImageField而不保存,python,django,django-models,wand,magickwand,Python,Django,Django Models,Wand,Magickwand,我有一个模型,我们叫它FooModel,它有一个类型为models.Imagefield的图像字段 在将图像添加到FooModel.image字段之前,我还使用wand.image对图像进行一些转换和调整大小 所以我有这样的想法: foo = FooModel() with Image(file='path/to/image.png') as tmpImage: # some transformation on tmpImage file_name = 'newTmpFile.png'
FooModel
,它有一个类型为models.Imagefield
的图像字段
在将图像添加到FooModel.image
字段之前,我还使用wand.image
对图像进行一些转换和调整大小
所以我有这样的想法:
foo = FooModel()
with Image(file='path/to/image.png') as tmpImage:
# some transformation on tmpImage
file_name = 'newTmpFile.png'
tmpImage.save(filename=file_name)
f = open(file_name, 'rb')
foo.image.save('new_file_name.png', File(f), save=True)
所以我基本上进行转换,将图像保存到一个临时位置,再次打开它并将其传递到模型的字段。这似乎是非常冗余和不必要的,但我没有设法将tmpImage
传递给FooModel
实例
我尝试了File(tmpImage)
,只是tmpImage
,但没有任何效果。还有其他想法吗