Python 3 OpenCV & Pillow Script to Convert Image to Byte Array

Python 3 OpenCV & Pillow Script to Convert Image to Byte Array There may be many doubts when it comes to this article. We hope that after reading this article all doubts will be resolved. Let’s go to the article.

Python 3 OpenCV & Pillow Script to Convert Image to Byte Array

Python 3 OpenCV & Pillow Script to Convert Image to Byte Array

pip install opencv-python
pip install pillow
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()
import io
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()

Read Also :Python 3 Script to Convert Time From 12 hour to 24 hour Format

Final Words

Python 3 OpenCV & Pillow Script to Convert Image to Byte Array We hope all your doubts about this topic will be resolved. See you again in the next article Thank you.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment