Skip to content

issamsensi/numpy-image-processing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numpy Image Processing

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.

Image Processing Demo

Features

  • 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

Requirements

numpy
matplotlib
Pillow

Installation

  1. Clone this repository:
git clone https://github.com/issamsensi/numpy-image-processing.git
cd "Numpy Image Processing"
  1. Install dependencies:
pip install numpy matplotlib Pillow

Usage

Basic Example

from 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)

Available Functions

Grayscale Conversion

# Display grayscale image
functions.grayscale(image_array)

# Return grayscale array without displaying
gray_array = functions.grayscale1(image_array)

Noise Addition

# 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)

Binary Thresholding

# Apply threshold and display
functions.threshold(image_array, thresh=128)

# Apply threshold and return array
binary_array = functions.threshold1(image_array, thresh=128)

Cropping

# 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)

Transformations

# Rotate 90 degrees counter-clockwise
functions.rotate(image_array)

# Flip image (axis=0: vertical, axis=1: horizontal)
functions.flip(image_array, axis=1)

Resizing

# Resize using nearest neighbor interpolation
functions.nearest_neighbor_resize(image_array, new_width=40, new_height=40)

Function Naming Convention

Functions with numeric suffixes (e.g., grayscale1, crop1) return the processed array without displaying it, while their base versions display the result using Matplotlib.

Project Structure

.
├── functions.py    # Core image processing functions
├── display.py      # Example usage and testing
└── README.md       # Project documentation

Implementation Details

  • 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

Examples

Convert to Grayscale and Add Noise

gray = functions.grayscale1(image_array)
functions.bruit(gray, mean=0, sigma=60)

Apply Threshold on Grayscale Image

functions.threshold(image_array, thresh=180)

Create a Circular Crop

functions.circle_crop(image_array, center_x=512, center_y=512, radius=400)

License

This project is open source and available for educational purposes.

Contributing

Feel free to fork this project and submit pull requests for any improvements.

Author

Created as an educational project to demonstrate image processing concepts using NumPy.

About

A lightweight Python library for image processing operations using NumPy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages