我有一张用于插入图像的表。由此,我只需使用 MySQL 数据库和 Python 3 代码检索图像。我的代码执行得很好,但我的问题是,每当我检索图像时,它都会向我显示放置在表中的第一张图像,并且我不知道如何使用 for 循环逐个检索 blob。
这是我的代码:- 代码.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()
我已经尝试过,但我无法获得预期的输出..任何人都可以帮助我...
最佳答案
我已经尝试过了,现在就得到了预期的输出..
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()
关于python - 使用Python 3从MySQL数据库中一张一张地检索多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47236445/