【问题标题】:MySQL Server storing imagesMySQL 服务器存储图像
【发布时间】:2020-06-30 22:04:20
【问题描述】:

我需要一个可以使用 python 访问并且可以存储许多图片的服务器。我读到 MySQL-Server 可以工作。首先,这是最好的选择还是其他更好的选择? (我想用图像训练 AI)
现在我构建了一些 python 代码,可以将图像保存在我的 MySQL 服务器中,但它们太大了。在我的硬盘上,它们需要 4KB,在 MySQL-Server 中,它们都需要 160KB,因此每个图像需要 80KB。
那是存储空间的 20 倍!有没有更好的存储方式?
我在 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')

我正在使用 python 3.8 和 MySQL-Server 8.2。

【问题讨论】:

    标签: python mysql python-3.x image storage


    【解决方案1】:

    您可以将图像存储在其他位置,例如您的计算机或网络服务器(如果用于网站),同时将它们的链接存储在数据库中。

    【讨论】:

    • 什么样的网络服务器最好?
    • 有很多选择,hostgator 很受欢迎,但对于一个小项目来说,为网络服务器付费并不值得。查看有关如何上传网站以获取更多服务器建议的视频,但我会首先尝试将它们存储在一个文件夹中并使用数据库每次从该文件夹中获取特定图像
    猜你喜欢
    • 2011-11-10
    • 2021-08-11
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2018-07-20
    • 1970-01-01
    • 2014-05-09
    相关资源
    最近更新 更多