imreadmulti() in Python Opencv
Last Updated :
24 Apr, 2025
OpenCV (Open Source Computer Vision) library primarily focuses on image and video processing. To load a single image through OpenCV, imread() function is used. However, to load multiple images in a single object, imreadmulti() function is used. Images within a file are considered as a page. Although this method can handle single-page files, it frequently works with multi-page or multi-frame image files.
Some of the file types supported by imreadmulti() are BMP (Windows bitmap), DIB (Device-independent bitmap), JPEG or JPG (Joint Photographic Experts Group), JP2 (JPEG 2000), PNG (Portable Network Graphics), PBM (Portable bitmap), PGM (Portable gray map), PPM (Portable Pixmap), TIFF (Tagged Image File Format) or TIF (Tagged Image Format), PFM (Portable float map), WEBP (Web Picture). Some file types such as EXR (OpenEXR) require codecs.
The result is loaded in a vector of Mat objects. In OpenCV, the cv::Mat class is used to represent a multi-dimensional array. It can be used to store image data in many different formats, including 32-bit floating-point numbers (CV 32F) 2-channel floating-point array, 8-bit unsigned integers (CV 8U) 8-bit single-channel array, and 16-bit signed integers (CV 16S).
Syntax: cv2.imreadmulti()
Syntax:
cv2.imreadmulti( filename, mats, flags, start(optional), count(optional))
Parameters:
- filename : The path of the .tiff files
- mats : The list/vector where loaded images will be stored
- flags : Default is cv2.IMREAD_ANYCOLOR . Other possible flags such as cv2.IMREAD_GRAYSCALE etc can be found here.
- start (optional) : To set the start index of the image frame to be loaded from the file. Default is 0.
- count (optional): The count of images to be added in mats.
Returns:
- ret : Boolean value for successful execution.
- mats : The loaded images list/vector.
Example 1:
- In the mentioned code, the Python OpenCV Library is imported.
- The path variable stores the image file path of the .tiff image file from the local machine.
- The images list variable is declared to store the loaded images.
- The Boolean value and Mat object returned by cv2.imreadmulti() function are stored in ret and images variable.
- The loaded images are displayed on the screen by cv2.imshow() function.
- The windows are then destroyed after waitKey is pressed.
Python3
# Importing Opencv
import cv2
# Path to the tiff file
path = "gfg.tiff"
# List to store the loaded image
images = []
ret, images = cv2.imreadmulti(mats=images,
filename=path,
start=0,
count=2,
flags=cv2.IMREAD_ANYCOLOR)
# Show the images
if len(images) > 1:
for i in range(len(images)):
# Dynamic name
name = 'Image'+str(i)
# Shape of Image
print(name, 'Shape :', images[i].shape)
# Displaying the image
cv2.imshow(name, images[i])
# Save the images
cv2.imwrite(name+'.jpg', images[i])
# Waiting for user to press any key to stop displaying
cv2.waitKey()
# Destroying all windows
cv2.destroyAllWindows()
Output:
Image0 Shape : (512, 512, 3)
Image1 Shape : (515, 570, 3)
Image0:
first Output image
Image1:
2nd Output imageExample 2:
The imreadmulti() function offers optional parameters that let users specify the starting frame and the number of loaded images.
The second and third images from a 3-page tiff file are shown in this example. You can set the count option to 1 to only show one image.
Python3
# Importing Opencv
import cv2
# Path to the tiff file
path = "gfg_2.tiff"
# List to store the loaded image
images = []
ret, images = cv2.imreadmulti(mats=images,filename=path,start=1, count=2,
flags=cv2.IMREAD_COLOR)
# Show the images
if len(images) > 1:
for i in range(len(images)):
# Dynamic name
name = 'Image'+str(i+1)
# Shape of Image
print(name,'Shape :',images[i].shape)
# Displaying the image
cv2.imshow(name, images[i])
# Save the images
cv2.imwrite(name+'.jpg', images[i])
# Waiting for user to press any key
cv2.waitKey()
# Destroying all windows
cv2.destroyAllWindows()
Output:
Image1 Shape : (515, 570, 3)
Image2 Shape : (176, 176, 3)
Output Images:
Image1:
Image1
Image2:
Image2Example 3:
If the image has only one page. Then the output will be also one image. To avoid issues while using this example code, the parameters' order must align with what is given.
Note: When loading single-page images, the imread() and imreadmulti() functions differ as the image is directly stored as a Mat object in the first while the latter stores it as a list of Mat objects that must be accessed using an index in square brackets (here- images[0], images2[0]).
Alternatively, the flags can also be specified as numerical value (here- cv::IMREAD_GRAYSCALE = 0, cv::IMREAD_COLOR = 1).
Python3
# Importing Opencv
import cv2
# Path to the file
path = "Ganesh.jpg"
images = []
ret, images = cv2.imreadmulti(mats=images,
filename = path,
flags=0)
print('Number of images :',len(images))
# With different flags
ret2,images2 = cv2.imreadmulti(filename = path,
flags=cv2.IMREAD_COLOR)
# Displaying image in grayscale
cv2.imshow("Gray Image", images[0])
# Save the images
cv2.imwrite('Gray Image.jpg', images[0])
# Displaying image in color
cv2.imshow("Color Image", images2[0])
# Save the images
cv2.imwrite('Color Image.jpg', images2[0])
# Waiting for user to press any key
cv2.waitKey()
# Destroying all windows
cv2.destroyAllWindows()
Output:
Number of images : 1
Output Images:
Gray Image:
Output Gray Image
Color Image:
Output Color Image
Similar Reads
cv2.imread() method - Python OpenCV
OpenCV-Python is a Python library used to solve computer vision tasks. cv2.imread() method loads an image from the specified file. If the image cannot be read because of missing file, improper permissions or an unsupported/invalid format then it returns an empty matrix.Example:Pythonimport cv2 image
2 min read
CalibrateCamera() OpenCV in Python
Image and video processing is the primary application of OpenCV. Images are captured by cameras, which use lenses to convert 3D objects from the real world into 2D images. However, because lenses are used during the transformation, some distortions are also introduced to the pictures. In order to pr
7 min read
Python OpenCV - Filter2D() Function
In this article, we are going to see about the filter2d() function from OpenCV. In a nutshell, with this function, we can convolve an image with the kernel (typically a 2d matrix) to apply a filter on the images. Syntax: filter2D (src, dst, ddepth, kernel) Parameters:Â Â Src - The source image to app
4 min read
Python OpenCV - Pose Estimation
What is Pose Estimation? Pose estimation is a computer vision technique that is used to predict the configuration of the body(POSE) from an image. The reason for its importance is the abundance of applications that can benefit from technology. Human pose estimation localizes body key points to accu
7 min read
Animate image using OpenCV in Python
In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example
3 min read
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 - 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 - 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
Introduction to OpenCV
OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance. In this article, to understand the basic functionalities of Python OpenCV module, we wi
4 min read
Python OpenCV | cv2.imwrite() method
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory. Syntax: cv2.imwrite(filename, image) Parameters:file
2 min read