简体   繁体   中英

Convert BLOB image to PNG, JPG with Python

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也无法读取图像

This code worked for me

import base64
with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(img))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM