简体   繁体   中英

How to keep uploaded image from user in memory with flask?

I am trying to build a website where the user uploads an image with text on it. The image should then be kept in memory. After that the website sholud get the text from the image and read it out.

Until now the user is only able to upload the image but nothing happens with it beacause I still have no submit button. So I basically only have an html code with an upload button.

For this I wanted to use python so maybe a flask app. But is it possible to somehow tell the flask app to keep the image in memory so I don't have to store it? If yes how? And if not do you maybe have any suggestions on how to solve this problem?

I am still a total beginner and am trying to learn so excuse me if there are any logical mistakes in my question. And thanks in advance for any answers or suggestions.

you can do this

from flask import Flask
from PIL import Image
import secrets
import os
app = Flask(__name__)

def save_image(index):
    rn = secrets.token_hex(8)
    _,f = os.path.splitext(index.filename)
    filename = rn + f
    path = os.path.join(app.root_path, "static/", filename)
    i = Image.open(index)
    i.save(path)
    return filename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM