M
M
Maks Hate2018-08-19 11:36:41
Python
Maks Hate, 2018-08-19 11:36:41

How to open image from Postgresql table?

Good afternoon! I like to learn something in practice (I learn postgresql) and came up with a problem. The essence of the script is that it takes pictures of you and looks for similar pictures from a table in the database, and shows a picture and a photo from the database side by side in a separate window. Stuck in one place where you need to display a photo from the database, there is some kind of binary data (bytea format). The essence of the question is how to display the picture itself instead of these characters! Thanks in advance!
Ps: There may be an error in the code, I'll attach it below.

db = postgresql.open('pq://postgres:[email protected]:5432/mydb')
db.execute("CREATE TABLE faces (id SERIAL PRIMARY KEY, " "name VARCHAR(50), " "face BYTEA)")
face_descriptors = db.query("SELECT name, face FROM faces")

# Здесь он выводит имя
print("It is ", face_descriptors[i][0])

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
igorzakhar, 2018-08-19
@4elive8

Pillow library to help you:

from PIL import Image
import io

image_data = ... # байты картинки
image = Image.open(io.BytesIO(image_data))
image.show()

P
pfg21, 2018-08-19
@pfg21

1. get the byte stream from the postgre cell
2. feed that byte stream to the picture decoder.
3. display a picture
in almost the same way as displaying a picture from a file, except for the first paragraph :)

V
Vyacheslav Uspensky, 2018-08-21
@Kwisatz

For what purpose are you trying to shift the tasks of the FS to the database?

R
Roman Kitaev, 2018-08-19
@deliro

1. Understand what bytes are
2. Don't store photos in a database. Because
3. You are far from “looking for similar ones” if you don’t understand such elementary things

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question