Reputation: 598
I have several blob images in Oracle, so I read them with python. I can correctly read and convert images from a certain table1 with my code, but when changing to table2 I cannot execute the same code because I get the following error.
cannot identify image file <_io.BytesIO object at 0x000000000C4520A0>
This is my code:
import pandas as pd
import cx_Oracle
from PIL import Image
#[connection to database with connecting string `conn`]
#[query to acces 1 single image]
result = pd.read_sql(query, conn) #connection to db
img = result["IMAGE"][0].read() # reading the first BLOB result
pre_img = io.BytesIO(img)
Image.open(pre_img)
This code works well, so the only problem is when I try to read images from table1 . Also at SQL developer I can previsualize the photos from table1, but not with table2. The type of data is BLOB as describe(table) says in Oracle.
value of img
can be found here
Upvotes: 4
Views: 24795
Reputation: 6120
Install Pillow module
py -m pip install Pillow
Use the module to show or save the file from the blob
from PIL import Image
file = request.files['file']
img = Image.open(file.stream)
img.show()
img.save("imagefile.jpg")
Upvotes: 2
Reputation: 598
This code worked for me
import base64
with open("imageToSave.png", "wb") as fh:
fh.write(base64.decodebytes(img))
Upvotes: 2