将图像作为文件传递到ImageField而不保存

2024-04-26 20:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

{{cd1>有一个类型的图像。在

在将图像添加到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,但是没有任何效果。还有别的主意吗?在


Tags: name图像image类型foopngsavewith