Currently I have a simple Flask server that once the user hits the /
endpoint it renders an HTML form where she can select an image for upload. Upon clicking on "submit", the server receives the image as a POST request, performs some processing on it and returns a JSON with the resulting analysis, for instance:
{
"status": "ok",
"coins found": 10
}
Everything works fine from the browser. I even managed to use Postman to bypass the form-filling phase and send the POST request directly to the server with an image attached and got back the expected response.
In the next phase I want to see how changing my image-processing pipeline is going to affect the server throughput. For that I intend to use Vegeta, and now I need to have my payload as a text file so I can tell Vegeta how to create the requests to perform its attacks.
What I really want to know is the following: "How do I store a jpg Image in a plain text file so vegeta can pick it up and the Flask server decode it properly".
For now I've tried reading the image, encoding the byte stream to base64 and storing it in the file, for instance:
------xx
Content-Disposition: form-data; name="image"; filename="sweden-swedish-flag-national-cycling-jersey.jpg"
Content-Type: image/jpeg
/9j/4AAQSkZJRgABAQEASABIAAD...
------xx--
However the Flask app fails to open the image with the following error: OSError: cannot identify image file <_io.BytesIO object at 0x12c17bc50>
.
For completeness, this is the code that I'm using to open images and is working fine for requests from the browser and Postman:
import io
from PIL import Image
bytestream = io.BytesIO(request.files['image'])
image = Image.open(bytestream)