OpenCV - imdecode() Function in Python Last Updated : 09 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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, decode it in color, and save it locally. Python import numpy as np import urllib.request import cv2 url = 'https://media.geeksforgeeks.org/wp-content/uploads/20211003151646/geeks14.png' with urllib.request.urlopen(url) as resp: i = np.asarray(bytearray(resp.read()), dtype="uint8") i = cv2.imdecode(i, cv2.IMREAD_COLOR) cv2.imwrite("result.jpg", i) Output:Explanation: The code fetches image data from a specified URL using urllib.request.urlopen(), converts it to a NumPy array, and decodes it using cv2.imdecode() with the cv2.IMREAD_COLOR flag to maintain the color information. The decoded image is then saved as a .jpg file using cv2.imwrite().Syntaxcv2.imdecode(buf,flags)Parameters:buf - It is the image data received in bytesflags - It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLORReturn Type: If the input byte data is a valid image, the function returns a NumPy ndarray, representing the image in the specified color mode.If buf given is not image data then NULL will be returned.Examples of imdecode() FunctionExample 1: Decoding and Saving an Image from URL in Grayscale If grayscale is required, then 0 can be used as flag. Python import numpy as np import urllib.request import cv2 url = 'https://media.geeksforgeeks.org/wp-content/uploads/20211003151646/geeks14.png' with urllib.request.urlopen(url) as resp: i = np.asarray(bytearray(resp.read()), dtype="uint8") i = cv2.imdecode(i, 0) cv2.imwrite("result.jpg", i) Output:Explanation: Similar to the first example, the image is fetched and converted into a NumPy array. However, in this case, cv2.imdecode() is used with the 0 flag, which decodes the image in grayscale. The resulting grayscale image is then saved as a .jpg file using cv2.imwrite().Example 2: Reading image from a fileThis code demonstrates how to read an image from a local file, decode it in grayscale, and display it using OpenCV.Input Image: Python import numpy as np import urllib.request import cv2 with open("image.jpg", "rb") as i: f = i.read() i = np.asarray(bytearray(f), dtype=np.uint8) i = cv2.imdecode(i, 0) cv2.imshow("output", i) cv2.waitKey(0) cv2.destroyAllWindows() Output:Explanation: The image is read from the local file image.jpg in binary mode, then converted into a NumPy array. The cv2.imdecode() function is used to decode the image into grayscale. The grayscale image is then displayed using cv2.imshow(), and the window remains open until a key is pressed. Comment More infoAdvertise with us Next Article OpenCV - imdecode() Function in Python B bhavyajain4641 Follow Improve Article Tags : Python OpenCV Python-OpenCV Practice Tags : python Similar Reads Python OpenCV - imencode() Function 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 librarie 2 min read Python OpenCV - moveWindow() Function When we show the image using the imshow() function output window will open at the center or default position of a computer screen. Even if there are multiple image windows all windows will be displayed at the same position and we have to move windows manually. If we want to show image windows at a s 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 numpy.mintypecode() function â Python numpy.mintypecode() function return the character for the minimum-size type to which given types can be safely cast. Syntax : numpy.mintypecode(typechars, typeset = 'GDFgdf', default = 'd') Parameters : typechars : [list of str or array_like] If a list of strings, each string should represent a dtyp 1 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 Python OpenCV - Canny() Function Canny edge detection algorithm is used in computer vision for identifying edges within an image. It helps in highlighting boundaries which are important for tasks like object detection and image segmentation. In this article, we will see how OpenCV's built-in Canny() function detects edges in an ima 3 min read ord() function in Python Python ord() function returns the Unicode code of a given single character. It is a modern encoding standard that aims to represent every character in every language.Unicode includes:ASCII characters (first 128 code points)Emojis, currency symbols, accented characters, etc.For example, unicode of 'A 2 min read Python OpenCV - getRotationMatrix2D() Function cv2.getRotationMatrix2D() function is used to make the transformation matrix M which will be used for rotating a image.Syntax: cv2.getRotationMatrix2D(center, angle, scale)Parameters: center: Center of rotationangle(θ): The angle of rotation in degrees. A positive value rotates the image anti-clockw 3 min read Like