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 upload and view an image in Python?
Data preprocessing is a crucial step in machine learning that involves cleaning data, removing noise, and transforming raw data into a suitable format. When working with images, preprocessing often includes loading, viewing, and manipulating image data using libraries like scikit-image.
The scikit-image library (skimage) provides powerful tools for image processing in Python. It integrates well with NumPy arrays and offers functions to read, display, and process images efficiently.
Loading and Displaying an Image
Here's how to upload and view an image using scikit-image ?
from skimage import io
import matplotlib.pyplot as plt
# Load the image
path = "path/to/your/image.png"
img = io.imread(path)
print("Image loaded successfully")
print(f"Image shape: {img.shape}")
print(f"Image data type: {img.dtype}")
# Display the image
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.axis('off') # Hide axes
plt.title('Loaded Image')
plt.show()
Working with Sample Images
Scikit-image provides built-in sample images for testing purposes ?
from skimage import data
import matplotlib.pyplot as plt
# Load a sample image
img = data.coffee()
print("Sample image loaded")
print(f"Image shape: {img.shape}")
print(f"Pixel value range: {img.min()} to {img.max()}")
# Display the image
plt.figure(figsize=(8, 6))
plt.imshow(img)
plt.axis('off')
plt.title('Sample Coffee Image')
plt.show()
Sample image loaded Image shape: (400, 600, 3) Pixel value range: 0 to 255
Image Properties and Information
You can extract various properties from the loaded image ?
from skimage import data
import numpy as np
# Load sample image
img = data.coins()
print("Image Information:")
print(f"Shape: {img.shape}")
print(f"Data type: {img.dtype}")
print(f"Size: {img.size} pixels")
print(f"Memory usage: {img.nbytes} bytes")
print(f"Min pixel value: {np.min(img)}")
print(f"Max pixel value: {np.max(img)}")
print(f"Mean pixel value: {np.mean(img):.2f}")
Image Information: Shape: (303, 384) Data type: uint8 Size: 116352 pixels Memory usage: 116352 bytes Min pixel value: 0 Max pixel value: 255 Mean pixel value: 120.85
Key Functions
| Function | Purpose | Usage |
|---|---|---|
io.imread() |
Load image from file | Reading external images |
io.imshow() |
Display image | Quick visualization |
data.coffee() |
Sample color image | Testing and examples |
data.coins() |
Sample grayscale image | Testing and examples |
Common Image Formats
Scikit-image supports various image formats including PNG, JPEG, TIFF, and BMP. The library automatically detects the format and loads the image as a NumPy array where each pixel is represented by numerical values.
Conclusion
Scikit-image provides efficient tools for loading and displaying images in Python. Use io.imread() to load images from files and data module for sample images. Images are stored as NumPy arrays, making them easy to manipulate and process.
