Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can scikit-learn library be used to get the resolution of an image in Python?
Data pre-processing refers to the task of gathering data from various resources into a common format. Since real-world data is never ideal, images may have alignment issues, clarity problems, or incorrect sizing. The goal of pre-processing is to remove these discrepancies.
To get the resolution of an image, we use the shape attribute. After reading an image, pixel values are stored as a NumPy array. The shape attribute returns the dimensions of this array, representing the image resolution.
Reading and Getting Image Resolution
Let's see how to upload an image and get its resolution using scikit-image library ?
from skimage import io
# Read the image (replace with your image path)
path = "path_to_your_image.png"
img = io.imread(path)
print("Image being read")
io.imshow(img)
print("Image displayed")
print("The image resolution is:")
print(img.shape)
Output
Image being read Image displayed The image resolution is: (397, 558, 4)
Understanding Image Shape
The shape attribute returns a tuple with three values ?
- Height: Number of pixels vertically (397)
- Width: Number of pixels horizontally (558)
- Channels: Number of color channels (4)
Working with Different Image Types
import numpy as np
from skimage import io, data
# Load a sample grayscale image
gray_img = data.camera()
print("Grayscale image shape:", gray_img.shape)
# Load a sample color image
color_img = data.astronaut()
print("Color image shape:", color_img.shape)
# Create a custom image array
custom_img = np.random.randint(0, 255, (100, 150, 3), dtype=np.uint8)
print("Custom image shape:", custom_img.shape)
Grayscale image shape: (512, 512) Color image shape: (512, 512, 3) Custom image shape: (100, 150, 3)
Image Channel Information
| Channels | Type | Description |
|---|---|---|
| 2D (H, W) | Grayscale | Single intensity values |
| 3D (H, W, 3) | RGB Color | Red, Green, Blue channels |
| 3D (H, W, 4) | RGBA Color | RGB + Alpha (transparency) |
Conclusion
Use img.shape to get image resolution after reading with io.imread(). The shape tuple shows height, width, and number of channels, helping you understand the image dimensions and format.
