0

I have one table for inserting images. From that, I just retrieve images using MySQL database and Python 3 code. My code is executed well, but my problem is whenever I retrieve images it is showing me first image which is placed in my table and I don't know how can I retrieve blobs one by one using for loop.

Here is my code:- 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()

I have tried, but I can't get my expected output..Can anyone help me...

2
  • args = (blob) is missing a comma. It should be args = (blob,) in order to make it a 1-tuple. Commented Nov 11, 2017 at 10:15
  • Thank u :)- i just removed it when i execute my code Commented Nov 11, 2017 at 10:21

1 Answer 1

1

I have tried and i got my expected output just now..

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.