1

I receive longblob data from database.

and i try to convert blob data to image and read image using cv2.

So I tried converting blob data to base64 like below code but it failed.

img = base64.decodebytes(img_str)

How can i convert blob to image? Is there a converting feature for this problem in the cv2 package?

송준석
  • 991
  • 1
  • 16
  • 32

2 Answers2

1

You no need cv2 for convert blob to image, you need to store on disk the blob/image and show it. here and example of retrive from mysql blob to disk file..

Good luck!

Page URL referenced:URL

import mysql.connector
from mysql.connector import Error

def write_file(data, filename):
    # Convert binary data to proper format and write it on Hard Disk
    with open(filename, 'wb') as file:
        file.write(data)

def readBLOB(emp_id, photo, bioData):
    print("Reading BLOB data from python_employee table")

    try:
        connection = mysql.connector.connect(host='localhost',
                                             database='python_db',
                                             user='pynative',
                                             password='pynative@#29')

        cursor = connection.cursor()
        sql_fetch_blob_query = """SELECT photo from python_employee where id = %s"""

        cursor.execute(sql_fetch_blob_query, (emp_id,))
        record = cursor.fetchall()
        for row in record:
            print("Id = ", row[0], )
            print("Name = ", row[1])
            image = row[2]
            file = row[3]
            print("Storing employee image and bio-data on disk \n")
            write_file(image, photo)
            write_file(file, bioData)

    except mysql.connector.Error as error:
        print("Failed to read BLOB data from MySQL table {}".format(error))

    finally:
        if (connection.is_connected()):
            cursor.close()
            connection.close()
            print("MySQL connection is closed")

readBLOB(1, "D:\Python\Articles\my_SQL\query_output\eric_photo.png",
         "D:\Python\Articles\my_SQL\query_output\eric_bioData.txt")
readBLOB(2, "D:\Python\Articles\my_SQL\query_output\scott_photo.png",
         "D:\Python\Articles\my_SQL\query_output\scott_bioData.txt")
송준석
  • 991
  • 1
  • 16
  • 32
0

If you want to save the image, use the code from 'Danilo Mercado Oudalova'.

But if you want to use without save file, use the example below.

import mysql.connector
from mysql.connector import Error
from io import BytesIO #from io import StringIO.
import PIL.Image



try:
    connection = mysql.connector.connect(host='localhost',
                                         database='python_db',
                                         user='pynative',
                                         password='pynative@#29')

    cursor = connection.cursor()
    sql = "your query"

    cursor.execute(sql)
    result = cursor.fetchall()

except mysql.connector.Error as error:
    print("Failed to read BLOB data from MySQL table {}".format(error))

finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")

type(result[0][0])
#If the type is byte, use from io import BytesIOf. If str, use from io import StringIO.
file_like= BytesIO(result[0][0])
img=PIL.Image.open(file_like)
img.show()
송준석
  • 991
  • 1
  • 16
  • 32