Sometimes, we want an in-momery jpg or png image that is represented as binary data. But often, what we have got is image in numpy ndarray or PIL Image format. In this post, I describe how to convert numpy image or PIL Image object to binary data without saving the underlying image.
If the image file is saved on disk, we can read it directly in binary format
open() method by using the
with open('test.jpg', 'rb') as f: byte_im = f.read()
Now the image will be read from disk to memory and is still in binary format.
But if we want to resize the original image and convert it to binary data without saving the resized image and re-read it from the hard disk, how should we do it?
Convert image to bytes
We can do it with the help of OpenCV or PIL.
This is how to achieve that in OpenCV:
import cv2 im = cv2.imread('test.jpg') im_resize = cv2.imresize(im, (500, 500)) is_success, im_buf_arr = cv2.imencode(".jpg", im_resize) byte_im = im_buf_arr.tobytes() # or using BytesIO # io_buf = io.BytesIO(im_buf_arr) # byte_im = io_buf.getvalue()
A little explanation here.
will encode the numpy ndarray in the specified format. This method will return
two values, the first is whether the operation is successful and the second is
the encoded image contained in an one-dimension array.
Then you can convert the returned array to real bytes either with the
io.BytesIO(). We can
finally get the
byte_im. It is the same with saving the resized image and
then reading it in binary format. But the saving step is removed and all the
operation is done in memory.
If you like to use PIL for image processing. You can use the following code:
from PIL import Image im = Image.open('test.jpg') im_resize = im.resize((500, 500)) buf = io.BytesIO() im_resize.save(buf, format='JPEG') byte_im = buf.getvalue()
In the above code, we save the
im_resize Image object into
buf. Note that in this case, you have to specify the saving
The bytes string can be retrieved with
getvalue() method of
License CC BY-NC-ND 4.0