More specifically, I want to change the filetype of an image uploaded through a Django ImageField.
更具体地说,我想更改通过Django ImageField上传的图像的文件类型。
My current thinking is to created a custom ImageField and overwrite the save method to manipulate the file.
我目前的想法是创建一个自定义的ImageField并覆盖save方法来操作文件。
I've having trouble getting an in memory file to because a PIL Image instance.
因为有一个PIL映像实例,所以我在内存文件中遇到了麻烦。
Thanks for the help.
谢谢你的帮助。
2 个解决方案
#1
12
Note that Django's ImageField
inherits the open
method from FieldFile
. This returns a stream object that can be passed to PIL's Image.open
(the standard factory method for creating Image
objects from an image stream):
注意,Django的ImageField从FieldFile继承了open方法。它返回一个流对象,可以传递给PIL的映像。打开(从图像流创建图像对象的标准工厂方法):
stream = imagefield.open()
image = Image.open(stream)
stream.close()
# ... and then save image with: image.save(outfile, format, options)
看到公益诉讼图像文档。
#2
18
Have you tried StringIO ?
你试过弦音吗?
see the docs http://effbot.org/imagingbook/introduction.htm#more-on-reading-images
看到文档http://effbot.org/imagingbook/introduction.htm more-on-reading-images
#Reading from a string
import StringIO
im = Image.open(StringIO.StringIO(buffer))
#1
12
Note that Django's ImageField
inherits the open
method from FieldFile
. This returns a stream object that can be passed to PIL's Image.open
(the standard factory method for creating Image
objects from an image stream):
注意,Django的ImageField从FieldFile继承了open方法。它返回一个流对象,可以传递给PIL的映像。打开(从图像流创建图像对象的标准工厂方法):
stream = imagefield.open()
image = Image.open(stream)
stream.close()
# ... and then save image with: image.save(outfile, format, options)
看到公益诉讼图像文档。
#2
18
Have you tried StringIO ?
你试过弦音吗?
see the docs http://effbot.org/imagingbook/introduction.htm#more-on-reading-images
看到文档http://effbot.org/imagingbook/introduction.htm more-on-reading-images
#Reading from a string
import StringIO
im = Image.open(StringIO.StringIO(buffer))