Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert BLOB image to PNG, JPG with Python

Tags:

python

blob

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

sql can't read the image neither

like image 955
luisfelipe18 Avatar asked Nov 01 '25 18:11

luisfelipe18


1 Answers

This code worked for me

import base64
with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(img))
like image 171
luisfelipe18 Avatar answered Nov 04 '25 11:11

luisfelipe18



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!