【问题标题】:Converting BLOB, stored on a database, to an image on an HTML website将存储在数据库中的 BLOB 转换为 HTML 网站上的图像
【发布时间】:2013-04-23 04:34:33
【问题描述】:

这是我的第一个问题。

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

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

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

现在只有二进制数据,显示了很多奇怪的符号。我认为这是 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()

【问题讨论】:

标签: python html database image blob


【解决方案1】:

这取决于你需要完成什么。

  1. HTML 页面上的单个图像。

    1.1 最好的方法是使用 Content-Type: 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 文件中图像标记的源

  2. 一个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>
    

    转换后就可以放置了。

     print(f"<img src="data:image/jpeg;base64,{blob}>")
    

注意:我使用 python3.8 作为第二个示例。我假设您正在使用 cgi 模块。

【讨论】:

    【解决方案2】:

    好吧,您可以返回一个 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()
    

    ...但这取决于您要达到的目标。

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

      • 我尝试了 Alok 的建议,但是当我从数据库中检索 BLOB 时,将其编码为 base 64 并使用您建议的 HTML。谢谢
      • 很酷,如果它有效,那么请为这个答案投票,以便其他人在遇到同样问题时能够使用它。
      【解决方案4】:

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

      image.decode('base64')
      

      这会将您的 blob 转换为图像

      【讨论】:

      • 我试了一下,但解码不起​​作用,但是当我从数据库中检索 BLOB 时,将其编码为 base 64 并使用 Reptilcus 建议的 HTML。谢谢
      猜你喜欢
      • 2015-11-24
      • 1970-01-01
      • 2021-12-05
      • 2011-11-21
      • 2015-10-16
      • 1970-01-01
      • 2018-05-24
      • 2018-05-30
      • 2011-04-27
      相关资源
      最近更新 更多