私は現在、サーバーに送りたいメモリ内の画像ファイルを生成しようとしupload
ています。で書かれたファイルアップロードメソッドのテストケースを書いていWerkzeug
ます。Werkzeug はファイルをサポートしており、request.files
アップロードするファイルのようなオブジェクトを指定できるようにする werkzeug hast testing-utilities を介してファイルにアクセスできます。
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"),
})
ファイルを保存するコード (短縮形):
path = request.files['img'].filename
request.files['img'].save(path)
今、私が理解できないのはfile
、実際に画像を処理メソッドに取得するためのキーとして渡す必要があるものです。私の現在のアプローチは次のとおりです
@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 に渡すにはどうすればよいですか?