0

I want to display a picture I already saved on the table img, but it gives me an error

cannot identify image file

When it try to open the file_like. Cursor and connection use the connection and password to mysql database. With the following code I wanted to display the picture. What's wrong with it, or is there even a better/easier way?

sql1='select * from img'
connection.commit()
cursor.execute(sql1)
data2=cursor.fetchall()

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

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

2 Answers 2

3

When using io.BytesIO instead of cstringIO it works fine, also without decoding and encoding. And I also changed type from blob to mediumblob, which allows bigger pictures.

import pymysql
import io
from PIL import Image

connection=pymysql.connect(host="localhost",
                 user="root",
                 passwd="root",
                 db="test")
cursor=connection.cursor()
sql1 = 'select * from table'
cursor.execute(sql1)
data2 = cursor.fetchall()

file_like2 = io.BytesIO(data2[0][0])

img1=Image.open(file_like2)
img1.show()
cursor.close()
connection.close()
Sign up to request clarification or add additional context in comments.

4 Comments

interesting. How big is your pic? A sidenote regarding your post: pls. use codesnippets only for HTML/JS/CSS. For all other code use code sample (the { } symbol), because you can't actually run Python in the browser.
Thanks for the tip, the picture is ~1.3MB
You can click on edit to change that. Also there's still the useless connection.commit().There's nothing to commit here. You only commit after executing insert or update statements.
This case is still a mystery for me. Now I checked it with a larger pics (yesterday I tested with a small pic of 38kB, now with 1.37MB and 4.71MB) (had to change column type to medium_blob). I still used cStringIO.StringIO as in my answer above and could read the image. I tested both cases, with encoding/decoding and without and both worked fine. So cStringIO.StringIO dosen't really seem to be the problem.
0

I tested your code and got the same error. So first I saved an image to my db. When I saved it I used base64 encoding and then got the same error when I tried to read. To save I used the code from Inserting and retrieving images into mysql through python, and your code also looks like you got it from the same question/answer.

In this case, the solution is simple. You have to decode the data, that's the part missing in the other answer. So do a base64.b64decode(data2[0][0]):

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

8 Comments

The proposed decoding gives an 'incorrect Padding' Error. Could this have something to do with the python 2.7?
I also used Python 2.7 to test your code and my answer. And I used the Python program as in the linked question to write the image with base64 encoding to the db. No idea what's wrong.
my whole answer was actually based on the assumption that you used the code in the linked question, including base64encoding. Was my assumption correct?
Yes i used the code from the answer, where it says, that it works fine. Just instead of mysql connector, I use pymysql.connect because of the picturesize.
But where is the base64 encoding when you load the picture into the table?
|

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.