imreadmulti() in Python Opencv
Last Updated :
28 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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Machin
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Logistic Regression in Machine Learning Logistic Regression is a supervised machine learning algorithm used for classification problems. Unlike linear regression which predicts continuous values it predicts the probability that an input belongs to a specific class. It is used for binary classification where the output can be one of two po
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
100+ Machine Learning Projects with Source Code [2025] This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an
5 min read
K means Clustering â Introduction K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ
4 min read