使用Python从MySQL数据库中检索多个图像3
问题描述:
我有一张用于插入图像的表格。从那以后,我只使用MySQL数据库和Python 3代码来检索图像。我的代码执行得很好,但我的问题是每当我检索图像时,它会向我显示放置在我的表格中的第一个图像,我不知道如何使用for循环逐个检索blob。使用Python从MySQL数据库中检索多个图像3
这里是我的代码: - code.py:
from PIL import Image
import pymysql
from api import mysql
db=pymysql.connect(host="localhost",user="root",passwd="root",db="votelist")
image = Image.open('D:/mine project/face_reg/media/a.jpg')
blob = open('D:/mine project/face_reg/media/a.jpg', 'rb').read()
sql = 'INSERT INTO imo(image) VALUES(%s)'
args = (blob)
cursor=db.cursor()
cursor.execute(sql,args)
sql1='select image from imo'
# i=sql1
# for image in i:
# print(i)
# else:
# print("no")
db.commit()
cursor.execute(sql1)
data=cursor.fetchall()
print ('Inserted')
file_like=io.BytesIO(data[0][0])
img=PIL.Image.open(file_like)
img.show()
print('retrieved')
db.close()
我都试过了,但我不能让我的预期output..Can谁能帮我...
答
我有尝试,我得到了我的预期输出刚刚..
import io
from io import BytesIO
import PIL.Image
# import cStringIO
from PIL import Image
import pymysql
from api import mysql
db=pymysql.connect(host="localhost",user="root",passwd="root",db="votelist")
image = Image.open('D:/mine project/face_reg/media/a.jpg')
blob = open('D:/mine project/face_reg/media/a.jpg', 'rb').read()
sql = 'INSERT INTO imo(image) VALUES(%s)'
args = (blob)
cursor=db.cursor()
cursor.execute(sql,args)
sql1='select image from imo'
conn=mysql.connect()
cursor=conn.cursor()
cursor.execute("select * from imo")
data=cursor.fetchall()
for a in data:
file_like = io.BytesIO(a[0])
img = PIL.Image.open(file_like)
img.show()
'args =(blob)'缺少一个逗号。它应该是'args =(blob,)'为了使它成为一个元组。 –
谢谢你:) - 我刚刚删除它时,我执行我的代码 – shruthi