Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将存储在数据库中的BLOB转换为HTML网站上的图像_Python_Html_Database_Image_Blob - Fatal编程技术网

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

Python 将存储在数据库中的BLOB转换为HTML网站上的图像,python,html,database,image,blob,Python,Html,Database,Image,Blob,这是我的第一个问题 我让用户上传他们自己的图像到数据库。 该图像存储为一个BLOB 我成功地做到了这一点。 我正在使用MySQL作为数据库 我遇到的问题是,当它被调用时,在网站上以图像的形式显示它 现在只显示二进制数据,很多奇怪的符号。我认为这是HTTP头的问题。现在,它在: print "Content-Type: text/html" 我试过: print "Content-Type: image/jpeg" 我正在使用Python连接数据库并编写HTML 编辑:代码: def show

这是我的第一个问题

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

我成功地做到了这一点。 我正在使用MySQL作为数据库

我遇到的问题是,当它被调用时,在网站上以图像的形式显示它

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

print "Content-Type: text/html"
我试过:

print "Content-Type: image/jpeg"
我正在使用Python连接数据库并编写HTML

编辑:代码:

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()

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

image.decode('base64')

这将把blob转换为图像

,具体取决于它的编码方式,您也可以为图像使用一个数据URI。如果它们被编码为base64 PNG,那么类似的东西可能会起作用

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

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

您可以返回一个HTML响应,并使用现有答案的组合,也可以只返回一个图像/jpeg响应,并将BLOB直接转储到stdout,如下所示

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()

…但这取决于你想要实现什么。

这取决于你需要完成什么

HTML页面上的单个图像

1.1最好的方法是将其与内容类型一起使用:image/jpeg(如果是jpeg)

import sys

def showFile(blob):
    print "Content-Type: image/jpeg\r\n"
    sys.stdout.flush()
    print sys.stdout.buffer.write(image)

def getFile():
    conn, cursor = getConnectionAndCursor()
    sql = 
    """
        select data
        from upload 
        where id=1
    """
    cursor.execute(sql)
    data = cursor.fetchone()
    blob = data[0]

    return blob

if __name__ == "__main__":

    form = cgi.FieldStorage()

    if "show_file" in form: 
        image = getFile()
        showFile(image)
为什么是最好的方法?因为您可以使用触发此脚本的请求url作为html文件中图像标记的源

一个html页面上有多个图像

2.1转换base64中的blob

 import base64

 blob = base64.b64encode(blob).decode('utf-8')
 # You need to decode it to get pure string and use it later in <img>
转换后,可以放置它


注意:对于第二个示例,我使用python3.8。我假设您使用的是cgi模块。

您为什么不能将图像的文件路径存储在数据库中?你需要包含更多你正在使用的脚本中的代码。我尝试了一下,但解码不起作用,但当我从数据库检索到BLOB时,将其编码为base 64并使用爬行动物建议的HTML。谢谢,我尝试了Alok的建议,但是当我从数据库中检索到BLOB时,将其编码为base 64并使用您建议的HTML工作。谢谢你,酷,如果它起作用,那么请投票给这个答案,这样其他人就可以使用它,如果他们有同样的问题。
 print(f"<img src="data:image/jpeg;base64,{blob}>")