SebNik
SebNik

Reputation: 997

MySQL Server storing images

I need a server which I can access with python and where I can store many pictures. I read that MySQL-Server could work. First of all is this the best option or is something else better ? (I want to train an AI with the images)
Now I build some python code which can save images in my MySQL-Server but they are to big. On my hard disk they need both 4KB and in the MySQL-Server both together 160KB so 80KB per image.
That is 20x times the storage!! Is there a better way to store than ?
My Code in python:

def insert_image(self, photo_path, name, table_name, id):
    binary_photo = self.convert_to_binary_data(photo_path)
    insert_blob_query = "INSERT INTO {} (id, name, photo) VALUES ('{}','{}',{})".format(table_name, id, name,
                                                                                        str(base64.b64encode(
                                                                                            binary_photo))[1:])
    self.connection.execute(insert_blob_query)

def read_blob_data_by_id(self, id, table_name, path):
    sql_fetch_blob_query = "SELECT * from {} where id = '{}'".format(table_name, id)
    result = self.connection.execute(sql_fetch_blob_query)
    for row in result:
        id_query = row[0]
        name = row[1]
        photo = base64.b64decode(row[2])
    file_like = io.BytesIO(photo)
    img = Image.open(file_like)
    img.save(path + '\\' + name + '.jpg')

I am using python 3.8 and an MySQL-Server 8.2.

Upvotes: 0

Views: 198

Answers (1)

Matthieu
Matthieu

Reputation: 51

You could store the images in another location such as your computer or a webserver if its for a website while storing their links in the database.

Upvotes: 1

Related Questions