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 to view the pixel values of an image using scikit-learn in Python?
Viewing pixel values of an image is a fundamental step in image processing and computer vision tasks. Scikit-image provides convenient functions to read images and extract pixel data, which can then be converted to a pandas DataFrame for analysis.
Images are stored as multi-dimensional arrays where each pixel has intensity values. For RGB images, each pixel contains three values (Red, Green, Blue), while grayscale images have single intensity values per pixel.
Reading and Displaying an Image
First, let's read an image and display its basic properties ?
from skimage import io, data
import pandas as pd
import numpy as np
# Using a sample image from scikit-image
img = data.camera() # Grayscale image
print("Image shape:", img.shape)
print("Image data type:", img.dtype)
print("Min pixel value:", img.min())
print("Max pixel value:", img.max())
Image shape: (512, 512) Image data type: uint8 Min pixel value: 0 Max pixel value: 255
Converting Image Pixels to DataFrame
The flatten() method converts the 2D image array into a 1D array, making it easy to create a DataFrame ?
from skimage import data
import pandas as pd
# Load sample image
img = data.camera()
print("Original image dimensions:", img.shape)
# Flatten the image to get pixel values in 1D array
pixel_values = img.flatten()
print("Flattened array length:", len(pixel_values))
# Create DataFrame from pixel values
df = pd.DataFrame(pixel_values, columns=['Pixel_Value'])
print("\nDataFrame shape:", df.shape)
print("\nFirst 10 pixel values:")
print(df.head(10))
Original image dimensions: (512, 512) Flattened array length: 262144 DataFrame shape: (262144, 1) First 10 pixel values: Pixel_Value 0 162 1 162 2 162 3 162 4 162 5 163 6 163 7 163 8 163 9 164
Working with Color Images
For RGB images, each pixel has three color channels. We can create separate columns for each channel ?
from skimage import data
import pandas as pd
# Load a color image
img_color = data.astronaut()
print("Color image shape:", img_color.shape)
# Reshape to get each pixel as a row with RGB values
pixels = img_color.reshape(-1, 3)
print("Reshaped pixels array shape:", pixels.shape)
# Create DataFrame with separate columns for R, G, B
df_color = pd.DataFrame(pixels, columns=['Red', 'Green', 'Blue'])
print("\nColor DataFrame shape:", df_color.shape)
print("\nFirst 5 rows:")
print(df_color.head())
# Basic statistics
print("\nPixel value statistics:")
print(df_color.describe())
Color image shape: (512, 512, 3)
Reshaped pixels array shape: (262144, 3)
Color DataFrame shape: (262144, 3)
First 5 rows:
Red Green Blue
0 61 55 123
1 62 56 124
2 63 57 125
3 65 59 127
4 67 61 129
Pixel value statistics:
Red Green Blue
count 262144.0000 262144.0000 262144.0000
mean 119.2767 89.8135 139.0453
std 58.9895 49.1469 67.8479
min 0.0000 0.0000 0.0000
25% 73.0000 51.0000 89.0000
50% 118.0000 83.0000 137.0000
75% 163.0000 124.0000 186.0000
max 255.0000 255.0000 255.0000
Adding Pixel Position Information
Sometimes it's useful to include the pixel coordinates along with their values ?
from skimage import data
import pandas as pd
import numpy as np
# Load grayscale image
img = data.coins()[0:100, 0:100] # Take smaller portion for demonstration
height, width = img.shape
# Create coordinate arrays
y_coords, x_coords = np.meshgrid(range(height), range(width), indexing='ij')
# Create DataFrame with coordinates and pixel values
df_with_coords = pd.DataFrame({
'x': x_coords.flatten(),
'y': y_coords.flatten(),
'pixel_value': img.flatten()
})
print("DataFrame with coordinates:")
print(df_with_coords.head(10))
print(f"\nTotal pixels: {len(df_with_coords)}")
DataFrame with coordinates: x y pixel_value 0 0 0 170 1 1 0 170 2 2 0 170 3 3 0 170 4 4 0 170 5 5 0 170 6 6 0 170 7 7 0 170 8 8 0 170 9 9 0 170 Total pixels: 10000
Practical Applications
Converting image pixels to DataFrames enables various data science operations ?
from skimage import data
import pandas as pd
# Load image and create DataFrame
img = data.camera()
df = pd.DataFrame(img.flatten(), columns=['intensity'])
# Find bright pixels (intensity > 200)
bright_pixels = df[df['intensity'] > 200]
print(f"Number of bright pixels: {len(bright_pixels)}")
# Find dark pixels (intensity < 50)
dark_pixels = df[df['intensity'] < 50]
print(f"Number of dark pixels: {len(dark_pixels)}")
# Calculate histogram-like distribution
intensity_counts = df['intensity'].value_counts().sort_index()
print(f"\nMost common intensity value: {intensity_counts.idxmax()}")
print(f"Occurs {intensity_counts.max()} times")
Number of bright pixels: 5982 Number of dark pixels: 21073 Most common intensity value: 0 Occurs 5476 times
Conclusion
Converting image pixels to pandas DataFrames using flatten() or reshape() enables powerful data analysis on image data. This approach is particularly useful for image preprocessing, statistical analysis, and preparing data for machine learning models.
