Python 将存储在数据库中的 BLOB 转换为 HTML 网站上的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16279856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:12:03  来源:igfitidea点击:

Converting BLOB, stored on a database, to an image on an HTML website

pythonhtmldatabaseimageblob

提问by user2332223

This is my first question.

这是我的第一个问题。

I am having users upload their own image to a database. That image is stored as a BLOB.

我让用户将他们自己的图像上传到数据库。该图像存储为 BLOB。

I was able to do this successfully. I am using MySQL for the database.

我能够成功地做到这一点。我使用 MySQL 作为数据库。

The part I am having trouble with is displaying that BLOB as an image on the website when its called upon.

我遇到问题的部分是在调用时将该 BLOB 作为图像显示在网站上。

Right now only the Binary data, lots of weird symbols are being displayed. I think its a problem with the HTTP header. Right now its in :

现在只有二进制数据,正在显示许多奇怪的符号。我认为这是 HTTP 标头的问题。现在它在:

print "Content-Type: text/html"

I've tried:

我试过了:

print "Content-Type: image/jpeg"

I am using Python to connect with the database and write the HTML.

我正在使用 Python 连接数据库并编写 HTML。

Edit: Code:

编辑: 代码:

def showFile():

    # do SQL to retrieve blob where filename
    conn, cursor = getConnectionAndCursor()
    sql = """
    select data
    from upload 
    where id=1
    """
    cursor.execute(sql)
    data = cursor.fetchone()
    blob = data[0]

    print "<hr>"
    print "This is what I'm trying"
    print """<img  src="data:image/jpeg;base64,%s/>""" % data

######################################################################
if __name__ == "__main__":

    form = cgi.FieldStorage()

    if "show_file" in form:
        print "Content-Type: text/html"
        print 
        printHeaders("Image upload example")
        showFile()
        printFooter()

采纳答案by Alok Agarwal

image is stored in database in binary format so once it comes to server using decode function to get it back to image

图像以二进制格式存储在数据库中,因此一旦使用解码功能到达服务器将其恢复为图像

image.decode('base64')

this will convert your blob to image

这会将您的 blob 转换为图像

回答by reptilicus

Depending on how its encoded, you can also possibly just use a Data URI for the image. Something like this might work if they are encoded as base64 PNGs.

根据它的编码方式,您也可以只使用图像的数据 URI。如果它们被编码为 base64 PNG,这样的东西可能会起作用。

<img  src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

As @Alok says, you might need to first convert it from binary blob to base64, then use the Data URI.

正如@Alok 所说,您可能需要先将其从二进制 blob 转换为 base64,然后使用数据 URI。

回答by Aya

Well, you can either return an HTML response, and use a combination of the existing answers, or you can just return an image/jpegresponse, and dump the BLOB directly to stdout, with something like this...

好吧,你可以返回一个 HTML 响应,并使用现有答案的组合,或者你可以只返回一个image/jpeg响应,然后将 BLOB 直接转储到标准输出,像这样......

def showFile():

    # do SQL to retrieve blob where filename
    conn, cursor = getConnectionAndCursor()
    sql = """
    select data
    from upload 
    where id=1
    """
    cursor.execute(sql)
    data = cursor.fetchone()
    blob = data[0]

    print blob

if __name__ == "__main__":

    form = cgi.FieldStorage()

    if "show_file" in form:
        print "Content-Type: image/jpeg"
        print 
        showFile()

...but it depends on what you're trying to achieve.

...但这取决于您要实现的目标。