0
from flask import Flask, render_template, url_for, request, redirect
from pymongo import MongoClient
from PIL import Image
import io
from matplotlib import pyplot as plt
app = Flask(__name__)

client = MongoClient("localhost", 27017)
db = client.data

todos = db.images


@app.route("/", methods=["POST", "GET"])
def home():
    if request.method == "POST":
        imagedata = request.files["file"]
        
        image_bytes = io.BytesIO()
        imagedata.save(image_bytes, format="JPEG")
        image = {
            image: image_bytes.getvalue()
        }
        todos.insert_one(image)
        return redirect(url_for("home"))

    return render_template("index1.html")


if __name__ == "__main__":

enter image description here

I am trying to store image file in mongoDB using flask and pymongo but i don't understand what was the problem

1 Answers1

0

Your issue right here is that you're using the save() method of a FileStorage object.

So, removing the line imagedata.save(image_bytes, format="JPEG") would help.

Then if your image is more than 16MB, be aware that MongoDB will hit a limitation here.

Er...
  • 526
  • 4
  • 10