Convert BLOB image to PNG, JPG with Python

11,815

Solution 1

This code worked for me

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

Solution 2

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")
Share:
11,815
L F
Author by

L F

I am a mathematician from UNI-Perú, I like algebra , some topology and number Theory Interested in number theory and Riemann hypothesis. Actually working on artificial intelligence & doing a lot of data science. Reading papers, and then implementing on python. Interested on electronics oriented on laptops and apple devices (microsoldering, motherboard schematic), I like mountain climbing, biking, trekking, play Knight Online, and cloud technology (actually working with AWS) I think this site has a large percentage of people who are very wrong when deciding which question is closed or not, which answer is closed or not., full of pedantic people (reputation wh-word).

Updated on June 06, 2022

Comments

  • L F
    L F almost 2 years

    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