Reading blob object in Python using wand library
This article is about reading a blob object using wand library in Python. But before doing that, we need to define what a blob object is.
BLOB stands for Binary Large Object. Blob objects are typically images, videos or other forms of multimedia stored in the form of binary strings in a database.
Wand is a binding of ImageMagick software used for image processing. So by using wand, we can use all the functionalities of ImageMagick. We need to install both the packages in our system. For that, run the following two commands in your terminal or command prompt:
pip install Wand apt-get install libmagickwand-dev
Python program to Read a blob object
On successful installation of both the packages, we can test it by the following line of Python code:
from wand.image import Image
This image class is used to open the images we want to work with. Make sure you have your image file in the working directory of your Python file. To read our image file, we will execute the following lines:
with open("random_image.png", "rb") as f: image_blob = f.read()
blob parameter of the Image constructor is used to read the binary string of the image. It can be done as:
with Image(blob = image_blob) as img: print("width = ", img.width) print("height = ", img.height)
width = 1920
height = 1080
So, reading a binary string version of an image should be a cake walk using Image class of wand library. If you encounter a problem in following the article and reading your blob object, feel free to comment below.
Also, refer to these articles: