Python OpenCV - imencode() Function Last Updated : 03 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Python OpenCV imencode() function converts (encodes) image formats into streaming data and stores it in-memory cache. It is mostly used to compress image data formats in order to make network transfer easier. Basic example of imencode() FunctionExample 1: We began by importing the necessary libraries, which are OpenCV and NumPy. After importing libraries, we use the imread() function to load the image, using the image path as an argument. After loading the image, we begin encoding it with the imencode() method, passing the extension of the image to be encoded as well as the loaded image as parameters. The outcome will differ depending on the format. If you notice, we are only saving the data of the first index of the imencode() method since it produces two outputs: whether the operation was successful or not at the zero index, and the encoded image at the first index. Now we'll convert the encoded image to a NumPy array so we can use it. Finally, we convert this NumPy array to bytes so that it can be easily transferred. Used image: gfg.png Code: Python3 # This code demonstrates encoding of image. import numpy as np import cv2 as cv # Passing path of image as parameter img = cv.imread('/content/gfg.png') # If the extension of our image was # '.jpg' then we have passed it as # argument instead of '.png'. img_encode = cv.imencode('.png', img)[1] # Converting the image into numpy array data_encode = np.array(img_encode) # Converting the array to bytes. byte_encode = data_encode.tobytes() print(byte_encode) Output: (Because the output was lengthy, only a portion of it is displayed here) b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01,\x00\x00\x00\xa0\x08\x02\x00\x00\x009\x1a\xc65\x00\ ............ Example 2: Used image: openCV.png Code: Python3 import numpy as np import cv2 as cv img = cv.imread('/content/OpenCV.png') img_encode = cv.imencode('.jpg', img)[1] data_encode = np.array(img_encode) byte_encode = data_encode.tobytes() print(byte_encode) Output: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x02\........ Comment More infoAdvertise with us Next Article Python OpenCV - imencode() Function S sp3768546 Follow Improve Article Tags : Python OpenCV Python-OpenCV Practice Tags : python Similar Reads OpenCV - imdecode() Function in Python cv2.imdecode() function is used to read image data from a memory cache and convert it into image format. This is generally used for loading the image efficiently from the internet. Example: Decoding and Saving an Image from URL in ColorThis example demonstrates how to download an image from a URL, d 2 min read Python OpenCV - namedWindow() Function Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen. Created windows are referred by their names and 3 min read Python OpenCV - haveImageReader() function In this article, we are going to learn about the haveImageReader() function of the OpenCV library. The haveImageReader() function is used to check whether specified images can be decoded or read successfully by OpenCV or not. Sometimes we need to detect if the specified image file is being correctl 1 min read Python OpenCV - haveImageWriter() function In this article, we are going to learn about the haveImageWriter() function of the OpenCV library. haveImageWriter() function Sometimes we need to detect if the specified image file is being correctly written or not before continuing further, In such a case we can use OpenCV which helps us to proces 2 min read Python OpenCV - getWindowImageRect() Function Python OpenCV getWindowImageRect() Function returns the client screen coordinates, along with the width and height of the window containing the picture. Syntax of cv2.getWindowImageRect() Syntax: cv2.getWindowImageRect(window_name) Parameter: window_name - Name of the window displaying image/video 3 min read Like