Python 使用Werkzeug创建内存中的图像以进行测试

Python 使用Werkzeug创建内存中的图像以进行测试,python,post,python-imaging-library,werkzeug,Python,Post,Python Imaging Library,Werkzeug,我目前正在尝试生成一个内存中的图像文件,我想将其上载到服务器。我正在为使用Werkzeug编写的文件上传方法编写一个测试用例。Werkzeug支持文件,我可以通过request.files访问文件,Werkzeug有测试实用程序,可以指定要上传的类似文件的对象: builder = EnvironBuilder(path="/someurl", method='POST', data={'foo': 'this is some text',

我目前正在尝试生成一个内存中的图像文件,我想
将其上载到服务器。我正在为使用
Werkzeug
编写的文件上传方法编写一个测试用例。Werkzeug支持文件,我可以通过
request.files
访问文件,Werkzeug有测试实用程序,可以指定要上传的类似文件的对象:

builder = EnvironBuilder(path="/someurl", method='POST',
                         data={'foo': 'this is some text',
                               'file': (StringIO('my file contents'), 'test.txt'),
                               'img': (Helper.create_image_file(), "img.png"),
                         })
存储文件的代码(缩写):

现在我搞不清楚的是,我必须传递什么作为键
文件
,才能真正获得处理方法的图像。我目前的做法如下

@classmethod
def create_image_file(cls, height=200, width=200):
    """ Create an in-memory image-file with the given height and width """
    from PIL import Image       
    size = (height, width)
    color = (255,255,0,0)
    img = Image.new("RGBA", size, color)        
    return StringIO(img.tostring())

这只会导致磁盘上的文件不可读。那么,我必须如何将内存中的图像传递给werkzeug,以便数据实际上是正确的并正确存储?

Btw:您可以使用
request.files['img'].保存(dest)
。谢谢,在中编辑它。关于其他问题有什么提示吗?:)
@classmethod
def create_image_file(cls, height=200, width=200):
    """ Create an in-memory image-file with the given height and width """
    from PIL import Image       
    size = (height, width)
    color = (255,255,0,0)
    img = Image.new("RGBA", size, color)        
    return StringIO(img.tostring())