从mysql中检索图像并在网页上显示

时间:2018-01-17 11:32:23

标签: python image display webpage-rendering

我使用了以下代码:

import MySQLdb
import base64
from PIL import Image
import cStringIO

db = MySQLdb.connect(host="localhost",
                     user="root",
                     passwd="root",
                     db="test")
# select statement with explicit select list and where clause instead of select * ...
sql1='select img from images where id=1'  
cursor = db.cursor()
cursor.execute(sql1)
data2=cursor.fetchall()
cursor.close()
db.close()

file_like=cStringIO.StringIO(base64.b64decode(data2[0][0]))

img1=Image.open(file_like,mode='r').convert('RGB')
img1.show()

一切都运作良好。 如何使用html文件显示此图像

你可以给我发送代码,如果有的话

1 个答案:

答案 0 :(得分:0)

您需要使用像Flask或Django这样的python Web框架来为Web创建视图。

然后你可以将这个模块导入到python web框架中,无论是烧瓶还是Django,都可以将图像提供给网页。