0

I need to save the images as Large Binary and then convert them to an image again and output in an html template. I save it as follows:

        file = request.files['file']

        image = Photos(photo=file.read())

        db_sess = db_session.create_session()
        db_sess.add(image)
        db_sess.commit()

class with LargeBinary column:

class Photos(SqlAlchemyBase):
    __tablename__ = 'photos'

    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True, autoincrement=True)
    photo = sqlalchemy.Column(sqlalchemy.LargeBinary, nullable=True)

I have not found a solution to this problem, I will be very grateful for your help

update:

I did this:

    image = db_session.create_session().query(Photos).filter(Photos.id == id_topic).first()
    img = b64encode(image)

    return render_template('topic.html', topics=topic, content=img)

and

<img src="data:;base64,{{ content }}"/>

It seems I'm on the right track, but I'm getting "TypeError: a bytes-like object is required, not 'NoneType'"

Traceback

[2022-04-23 18:50:11,989] ERROR in app: Exception on /11/f [GET]
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\flask_login\utils.py", line 272, in decorated_view
    return func(*args, **kwargs)
  File "C:\Users\user\PycharmProjects\rep\views.py", line 102, in sometopic
    img = b64encode(image)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'NoneType'

  • Does it answer your question? https://stackoverflow.com/questions/55873174/how-do-i-return-an-image-in-fastapi – kosciej16 Apr 22 '22 at 21:27
  • I am afraid scenario you are trying to achieve can be impossible. As far I know in html you can only put img source, which can be static file (so you need to save that img on disc) or link for downloading it (which the URL I posted should help). – kosciej16 Apr 22 '22 at 21:36
  • @kosciej16 here [link](https://stackoverflow.com/questions/31849494/serve-image-stored-in-sqlalchemy-largebinary-column) the situation is similar to mine, but the solution does not fit, I get a **"AttributeError: type object 'Photos' has no attribute 'query'"**. I was trying to find a way to use BytesIO to convert data from a LargeBinary column into a photo and send it to an html template. Is it possible and how to do it? Thank you in advance – BoggyDoggyBoo Apr 23 '22 at 13:26
  • Something is `None` when you are expecting it to be data. Probably `image` but I can't tell without the full traceback. – noslenkwah Apr 23 '22 at 14:59
  • @noslenkwah I added a traceback, please take a look – BoggyDoggyBoo Apr 23 '22 at 16:00
  • The stacktrace is saying your image is None - that means there is no row with id_topic (or the associated `photo` column is empty) – kosciej16 Apr 23 '22 at 19:28

0 Answers0