Skip to main content
  1. Posts/

Convert PIL or OpenCV Image to Bytes without Saving to Disk

··409 words·2 mins·
Table of Contents


Sometimes, we may want an in-memory jpg or png image that is represented as binary data. But often, what we have got is image in OpenCV (Numpy ndarray) or PIL Image format. In this post, I will share how to convert Numpy image or PIL Image object to binary data without saving the underlying image to disk.

If the image file is saved on disk, we can read it directly in binary format with open() method by using the b flag:

with open('test.jpg', 'rb') as f:
    byte_im =

Now the image will be read from disk to memory and is still in binary format.

What 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.resize(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. imencode() 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 in a one-dimension Numpy array.

Then you can convert the returned array to real bytes either with the tobytes() method or io.BytesIO(). We can finally get the byte_im. It is the same with saving the resized image in hard disk 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:

import io
from PIL import Image

im ='test.jpg')
im_resize = im.resize((500, 500))
buf = io.BytesIO(), format='JPEG')
byte_im = buf.getvalue()

In the above code, we save the im_resize Image object into BytesIO object buf. Note that in this case, you have to specify the saving image format because PIL does not know the image format in this case. The bytes string can be retrieved using getvalue() method of buf variable.



Conversion between base64 and OpenCV or PIL Image
··382 words·2 mins
How to Resize, Pad Image to Square Shape and Keep Its Aspect Ratio in Python
··491 words·3 mins
Pillow/PIL 缩放索引图像时的一个问题
·343 words·2 mins