I am running two Flask instances, currently just locally on 'localhost', at two different ports. I would like for one Flask instance (I call it the "frontend" instance) to accept an image from the user via form upload, then send it to another Flask instance (I call it the "display" instance) where the image is displayed for the user without saving the file to the local file system as an intermediate step. Yes, I realize this can all be done within one Flask instance, but this is being done as a coding exercise for a non-realistic application.
I see from this question that Flask has a send_file()
function, but apparently that only works if the image is saved on disk, which it is not in this case.
I have also seen some questions recommend sending the image within a JSON object, which does seem to transmit the image data, but something with the photo encoding does not work - alas the receiving webpage shows a broken image. A comment on this post seems to suggest that in order to send images between Flask instances, we must use base64 encoding, which I have both tried and not tried (sent both encoded and un-encoded versions of the uploaded image). (This SO question seems closest to my problem, but they don't seem to use the "POST" method to send the image from one Flask instance to another)
Other things I've tried include this post which seems to just remind us about adding the mime encoding format when displaying the image, which I've added to no effect.
In summary, I can't seem to find any resources online that talks about sending images from one Flask instance to another, with the image coming from a form upload and not the filesystem, and not saving the file to disk/filesystem at any time during this process.
If there is a solution to this problem, it would be greatly appreciated.
EDIT: Following metatoaster's comment below, I've tried "Attempt 3", whereas the "Frontend" Flask instance sends the image as a file and the "Display" Flask instance attempts to open the file in the same way. (I added "Attempt 3" to the code snippets below). The image still does not display, however, I can see that on the "Frontend" instance, when I do print(value)
it shows <FileStorage: 'IMG_45A21537E24C-1.jpeg' ('image/jpeg')>
whereas the same print(value)
on the "Display" instance shows <FileStorage: 'image' (None)>
-- so I think the problem now is, why is the receiving Flask instance showing the file type as "None"? (Which may be a different question altogether). Tried this as well, receiving object type is still "None".
"Frontend" Flask Instance (running on Port 5000)
@webapp.route('/upload_image', methods=["GET", "POST"])
def upload_image():
if request.method == "POST":
uploaded_image = request.files['image']
# Attempt 1: Sending image in "data" payload with POST request
uploaded_image_b64 = base64.b64encode(uploaded_image.read()).decode('utf-8')
requests.post('http://localhost:5001/receive_image', data=value)
# Result: 400 Status Code on "Display" Flask instance, image not displayed
# Attempt 2: Sending image in json
uploaded_image_b64 = base64.b64encode(uploaded_image.read()).decode('utf-8')
requests.post('http://localhost:5001/receive_image', json={"text":"Testing", "image"=uploaded_image_b64})
# Result: I can see "Testing" show up and the "image" shows up as a "?" on the receiving webpage.
# 200 Status Code on "Display" Flask instance, so at least HTTP seems to have gone correctly.
# Attempt 3: Sending image through "files" in requests
requests.post('http://localhost:5001/receive_image', files={'image':uploaded_image})
Corresponding Webpage for "Frontend" Flask Instance where user can upload their photo
<h2>Upload your image here</h2>
<form method="POST" action="/upload_image" enctype="multipart/form-data">
<input class="form-control form-control-sm" name="image" type="file" accept="image/jpeg, image/png">
</form>
"Display" Flask Instance (running on Port 5001)
@webapp.route('/receive_image', methods=["GET", "POST"])
def receive_image():
if request.method == "POST":
content = request.json
print(content["text"])
file = content["image"]
# This comes from yet another SO post:
# https://stackoverflow.com/questions/60091029/python-requests-post-image-with-json-data
# It doesn't help the situation, but I've tested it as well
# image_original = base64.b64decode(file)
# img_as_np = np.frombuffer(image_original, dtype=np.uint8)
# img = cv2.imdecode(img_as_np, flags=1)
return render_template("receive.html", image=file)
# Attempt 3
received_image = request.files['image']
print(received_image)
# received_image = base64.b64encode(received_image.read()).decode('utf-8') -- tried adding this, both with and without doesn't help
return render_template("receive.html", image=received_image)
Corresponding Webpage for "Display" Flask Instance where uploaded photo is displayed
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<h1>Test Server Image Receiving Page</h1>
<!-- All three options below do not display the image -->
<img src="data:image/jpeg;base64, {{image}}">
<img src="data:image, {{image}}">
<img src="{{image}}">
</body>
</html>