
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Load Image Using Python Mahotas
Python is known for its strong libraries that can handle nearly any task, and image processing is no different. For this purpose, a popular choice is Mahotas, a computer vision and image processing library. This article explores how to load images using Python's Mahotas, providing you with practical examples.
Introduction to Mahotas
Mahotas is a sophisticated library that contains numerous methods for image processing and computer vision. With a strong focus on speed and productivity, Mahotas enables you use over 100 features, including color space conversions, filtering, morphology, feature extraction, and more. This guide focuses on one of the most essential phases in image processing - loading an image.
Installing Mahotas
Before we begin loading photos, we must first confirm that Mahotas is installed. Using pip, you may add this package to your Python environment
pip install mahotas
Ensure you have the newest version for the greatest performance and access to all features.
Loading Images with Mahotas
The mahotas.imread() function reads an image and loads it into a NumPy array. It supports a number of file formats, including JPEG, PNG, and TIFF.
Example 1: Basic Image Loading
Loading an image is as simple as supplying the image path to the imread() function
import mahotas as mh # Load the image image = mh.imread('path_to_image.jpg') # Print the type and dimensions of the image print(type(image)) print(image.shape)
This code loads the image and outputs the dimensions (height, width, and number of colour channels), type (which should be a numpy ndarray), and type of the image.
Example 2: Grayscale Image Loading
In some circumstances, you may want to load your image as grayscale right off the bat. For that, you can use the as_grey parameter
import mahotas as mh # Load the image as grayscale image = mh.imread('path_to_image.jpg', as_grey=True) # Print the type and dimensions of the image print(type(image)) print(image.shape)
Because there is only one colour channel, the image is now a 2D array (height and width only).
Example 3: Loading Images from URLs
Mahotas enables the direct loading of photos from a URL. Imread() cannot do this function directly, hence we must utilise other libraries like urllib and io
import mahotas as mh import urllib.request from io import BytesIO # URL of the image url = 'https://example.com/path_to_image.jpg' # Open URL and load image with urllib.request.urlopen(url) as url: s = url.read() # Convert to BytesIO object and read image image = mh.imread(BytesIO(s)) # Print the type and dimensions of the image print(type(image)) print(image.shape)
With the help of this code, you can quickly load an image from the web into a numpy ndarray so that it may be processed further.
Conclusion
The first step in image processing is to load the images, and Python's Mahotas package makes this process simple. Mahotas offers you the tools you need whether you're dealing with local files or web photographs, colour or grayscale.
You've made progress towards mastering Python's image processing capabilities by gaining proficiency with image loading. The journey doesn't end there, though; Mahotas has a wealth of tools at your disposal to further modify and analyse your photographs.