使用python3从MySQL数据库中逐个检索多个图像

2024-10-05 10:48:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张用来插入图像的表格。从中,我只需使用MySQL数据库和python3代码检索图像。我的代码执行得很好,但我的问题是每当我检索图像时,它都会显示放在表中的第一个图像,我不知道如何使用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()

我试过了,但是我没能得到预期的结果。谁能帮我。。。在


Tags: 代码from图像imageimportfordbpil
1条回答
网友
1楼 · 发布于 2024-10-05 10:48:49

我已经试过了,我刚刚得到了预期的产量。。在

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

相关问题 更多 >

    热门问题