How to convert blob into byteArray in flask python?

python blob to image
python read blob data
extract blob from mysql
retrieve blob file from mysql
python mysql insert binary data
storing blob in mysql
how to insert blob data in mysql
how to use blob in mysql

A customer sends a multipart file to me, I read it with:

    blob = request.files['file'].read()

I have a blob file in hand. Now I have to send this file with requests like open('file.png', 'rb'). How can I convert blob to something like rb mode. With open I could not open blob directly.

The below code did not work:

opened_file = open(request.files['file'], 'rb')

And I got the following error:

TypeError: coercing to Unicode: need string or buffer, FileStorage found

Is there a way to do that without saving it on file system? I send my requests to somewhere else:

files = dict({'file': byteArrayFile})
r =, files=files, headers=headers)

request.files['file'] is already a file-like object (it's a FileStorage instance), so you can use that directly; you don't need to open it.

r =, files={"file": request.files["file"]}, headers=headers)

You can think of a file-like object as an open file. They behave the same way.

Flask serving binary data example � GitHub, I had to add as_attachment=True to send_file to get it to work. But thanks! Comment on gist. Sign in to comment or sign up to join this conversation on GitHub. In this article, I will let you know how to insert and retrieve a file stored as a BLOB in the SQLite table using Python’s built-in module sqlite3. Use SQLite BLOB data type to store any binary data into the SQLite table using Python. Binary can be a file, image, video, or a media; Read BLOB data from the SQLite table in Python.

try using

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img =

it worked for me and for testing if you wish to have a look on the images try

Python SQLite BLOB to Insert and Retrieve file and images, We need to convert our files and images into binary data i.e., byte array in Python to store and retrieve from SQLite database. The SQLite table� Like other programming languages (e.g. Java), we can also convert an image to a string representation in Python. Converting in Python is pretty straightforward, and the key part is using the "base64" module which provides standard data encoding an decoding. Convert Image to String. Here is the code for converting an image to a string.

If your image is stored in a Blob format (i.e. in a database) you can use the same technique explained by Billal Begueradj to convert your image from Blobs to a byte array.

In my case, I needed my images where stored in a blob column in a db table:

def select_all_X_values(conn):
    cur = conn.cursor()
    cur.execute("SELECT ImageData from PiecesTable")    
    rows = cur.fetchall()    
    return rows

I then created a helper function to change my dataset into np.array:

X_dataset = select_all_X_values(conn)
imagesList = convertToByteIO(np.array(X_dataset))

def convertToByteIO(imagesArray):
    # Converts an array of images into an array of Bytes
    imagesList = []

    for i in range(len(imagesArray)):  
        img =[i])).convert("RGB")
        imagesList.insert(i, np.array(img))

    return imagesList

After this, I was able to use the byteArrays in my Neural Network.


Python, bytearray() method returns a bytearray object which is an array of given bytes. errors parameters, bytearray() converts the string to bytes using str.encode(). Go ahead, download it and let’s get Python rolling! (I’m assuming that the name of the image is deer.gif.) The first thing we have to do in order to use Base64 in Python is to import the base64 module: import base64. In order to encode the image, we simply use the function base64.encodestring(s). Python mentions the following regarding this

Python: Convert Image to String, Convert String to Image, Java: convert image to byte array, convert byte array to image. Category >> Python. If you want someone to read your code, please� Note that in Python 3 print is a function which has an argument called end that defaults to “ ”, which is appended at the end of the input to print [5]. Thus, if we specify this argument as a space (” “), all the bytes of each object will be printed with a space between them, rather that each one being printed in a newline.

How to convert BLOB to Byte Array in java?, You can contents of a blob into a byte array using the getBytes() method. Example. import java.awt.Image; import java.awt.image. Using Sqlite3 to store Blob data. Insert binary file in SQLite database with Python. pysqlite - how to save images. Python, Sqlite3 - How to convert a list to a BLOB cell. Writing blob from SQLite to file using Python. Storing BLOBs in a SQLite DB with Python/pysqlite. y_serial - warehouse compressed Python objects with SQLite.

Converting BLOB file data to image from sql database, Hi there, I have made a mini database on a web server for testing. I am able to connect to it remotely successfully. The next step is to read the� The json module enables you to convert between JSON and Python Objects. Related course: Data Analysis with Python Pandas. JSON conversion examples. Convert JSON to Python Object (Dict) To convert JSON to a Python dict use this: