A lightweight Python library for image processing operations using NumPy and Matplotlib. This project implements common image manipulation techniques from scratch without relying on high-level image processing libraries.
- Grayscale Conversion: Convert color images to grayscale using weighted RGB channels
- Noise Addition: Add Gaussian noise or salt-and-pepper noise to images
- Thresholding: Apply binary thresholding for image segmentation
- Cropping: Rectangular and circular crop operations
- Transformations: Rotate and flip images
- Resizing: Nearest neighbor interpolation for image resizing
numpy
matplotlib
Pillow- Clone this repository:
git clone https://github.com/issamsensi/numpy-image-processing.git
cd "Numpy Image Processing"- Install dependencies:
pip install numpy matplotlib Pillowfrom PIL import Image
import numpy as np
import functions
# Load an image
image = Image.open("your_image.jpeg")
image_array = np.array(image)
# Apply grayscale conversion
functions.grayscale(image_array)# Display grayscale image
functions.grayscale(image_array)
# Return grayscale array without displaying
gray_array = functions.grayscale1(image_array)# Add Gaussian noise
functions.bruit(gray_image, mean=0, sigma=50)
# Add salt-and-pepper noise (0.02 = 2% of pixels)
functions.bruit2(gray_image, nbpbruit=0.02)# Apply threshold and display
functions.threshold(image_array, thresh=128)
# Apply threshold and return array
binary_array = functions.threshold1(image_array, thresh=128)# Rectangular crop
functions.crop(image_array, x_start=200, y_start=100, x_end=400, y_end=400)
# Circular crop
functions.circle_crop(image_array, center_x=512, center_y=512, radius=400)# Rotate 90 degrees counter-clockwise
functions.rotate(image_array)
# Flip image (axis=0: vertical, axis=1: horizontal)
functions.flip(image_array, axis=1)# Resize using nearest neighbor interpolation
functions.nearest_neighbor_resize(image_array, new_width=40, new_height=40)Functions with numeric suffixes (e.g., grayscale1, crop1) return the processed array without displaying it, while their base versions display the result using Matplotlib.
.
├── functions.py # Core image processing functions
├── display.py # Example usage and testing
└── README.md # Project documentation
- Grayscale Conversion: Uses standard luminance weights (R: 0.299, G: 0.587, B: 0.114)
- Gaussian Noise: Adds random noise from a normal distribution
- Salt & Pepper Noise: Randomly sets pixels to black (0) or white (255)
- Nearest Neighbor Resize: Simple interpolation method for scaling images
gray = functions.grayscale1(image_array)
functions.bruit(gray, mean=0, sigma=60)functions.threshold(image_array, thresh=180)functions.circle_crop(image_array, center_x=512, center_y=512, radius=400)This project is open source and available for educational purposes.
Feel free to fork this project and submit pull requests for any improvements.
Created as an educational project to demonstrate image processing concepts using NumPy.
