Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Processing Image Data for Deep Learning

This repository contains a Jupyter notebook that demonstrates fundamental image preprocessing techniques essential for deep learning applications. As the developer, I've created this notebook to walk through the complete workflow of handling image data, from downloading and loading to resizing and converting formats.

Overview

The notebook processing_image_data_for_deep_learning.ipynb serves as a hands-on tutorial for image data preparation. It covers:

  • Downloading images from the web
  • Loading and inspecting image data
  • Visualizing images
  • Resizing images to standard dimensions
  • Converting color images to grayscale
  • Saving processed images

This preprocessing pipeline is crucial because raw images often need to be standardized before feeding into deep learning models, which typically expect consistent input dimensions and formats.

Prerequisites

To run this notebook, you'll need the following Python libraries:

  • matplotlib (for image loading and visualization)
  • Pillow (PIL) (for image manipulation)
  • opencv-python (cv2) (for advanced image processing)
  • numpy (automatically included with matplotlib)

The notebook is designed to run in Google Colab, which provides these libraries pre-installed. If running locally, install them via pip:

pip install matplotlib pillow opencv-python

Workflow and Code Explanation

Step 1: Download Sample Image

!wget 'https://tractive.com/blog/wp-content/uploads/2016/04/puppy-care-guide-for-new-parents.jpg'

What it does: Downloads a sample dog image from the internet using wget (a command-line tool). This gives us a real image file to work with.

Why: Instead of requiring users to upload their own images, we start with a known sample. The image is saved as 'puppy-care-guide-for-new-parents.jpg' in the Colab environment.

Step 2: Import Visualization Libraries

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

What it does: Imports matplotlib's image reading module and pyplot for plotting.

Why: matplotlib.image.imread() can load various image formats into NumPy arrays, and pyplot provides display functions.

Step 3: Load Image with Matplotlib

img = mpimg.imread('/content/dogg.jpg')

What it does: Reads the downloaded image into a NumPy array.

Why: This converts the image file into a format Python can manipulate. The path '/content/dogg.jpg' suggests the file was renamed or the notebook assumes a different filename.

Step 4: Inspect Image Type

type(img)

Output: <class 'numpy.ndarray'>

What it does: Checks the Python data type of the loaded image.

Why: Confirms that images are loaded as NumPy arrays, which are the standard format for numerical computations in deep learning.

Step 5: Check Image Shape

print(img.shape)

Output: (1365, 2048, 3)

What it does: Prints the dimensions of the image array.

Why: Understanding shape is crucial:

  • 1365: height in pixels
  • 2048: width in pixels
  • 3: number of color channels (RGB)

Step 6: Examine Raw Pixel Values

print(img)

What it does: Prints the actual pixel values.

Why: Shows that images are just 3D arrays of numbers (0-255 for each color channel). This helps understand that image processing is essentially array manipulation.

Step 7: Display the Image

img_plot = plt.imshow(img)
plt.show()

What it does: Uses matplotlib to display the image in the notebook.

Why: Visual verification that the image loaded correctly.

Step 8: Import PIL for Image Manipulation

from PIL import Image

What it does: Imports the Pillow library (PIL fork).

Why: PIL provides powerful image manipulation functions, especially for resizing.

Step 9: Resize the Image

image = Image.open('/content/dogg.jpg')
image_resized = image.resize((200, 200))

What it does: Opens the image with PIL and resizes it to 200x200 pixels.

Why: Deep learning models often require fixed input sizes. Resizing standardizes dimensions across different images.

Step 10: Save Resized Image

image_resized.save('dog_image_resized.jpg')

What it does: Saves the resized image as a new JPEG file.

Why: Creates a processed version for further use or verification.

Step 11: Load and Display Resized Image

img_res = mpimg.imread("/content/dog_image_resized.jpg")
img_res_plot = plt.imshow(img_res)
plt.show()

What it does: Loads the resized image and displays it.

Why: Verifies the resizing worked correctly.

Step 12: Check Resized Image Shape

print(img_res.shape)

Output: (200, 200, 3)

What it does: Confirms the new dimensions.

Why: Ensures the resize operation worked as expected.

Step 13: Import OpenCV

import cv2

What it does: Imports OpenCV library.

Why: OpenCV is a powerful computer vision library with extensive image processing capabilities.

Step 14: Load Image with OpenCV

img = cv2.imread('/content/dogg.jpg')

What it does: Reads the image using OpenCV.

Why: OpenCV and matplotlib may handle color channels differently (OpenCV uses BGR by default).

Step 15: Inspect OpenCV Image Type and Shape

type(img)
img.shape

Output: <class 'numpy.ndarray'>, (1365, 2048, 3)

What it does: Same checks as before.

Why: Confirms OpenCV also loads images as NumPy arrays with similar shapes.

Step 16: Convert to Grayscale

grayscale_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

What it does: Converts the color image to grayscale.

Why: Some deep learning tasks (like certain classification problems) work with grayscale images, reducing computational complexity and focusing on shape rather than color.

Step 17: Inspect Grayscale Image

type(grayscale_image)
grayscale_image.shape

Output: <class 'numpy.ndarray'>, (1365, 2048)

What it does: Checks the grayscale image properties.

Why: Note that grayscale has only 2 dimensions (height, width) since there's no color channel.

Step 18: Display Grayscale Image

from google.colab.patches import cv2_imshow
cv2_imshow(grayscale_image)

What it does: Displays the grayscale image using Colab's OpenCV patch.

Why: cv2.imshow() doesn't work in Colab, so we use the special Colab function.

Step 19: Save Grayscale Image

cv2.imwrite('dog_grayscale_image.jpg', grayscale_image)

What it does: Saves the grayscale image.

Why: Creates a final processed version.

How to Run the Notebook

  1. Open the notebook in Google Colab (recommended) or Jupyter Lab
  2. Run cells sequentially from top to bottom
  3. The notebook will download the sample image automatically
  4. Each step builds on the previous, demonstrating the complete preprocessing pipeline

Expected Outputs

  • Original image: 1365x2048 RGB
  • Resized image: 200x200 RGB
  • Grayscale image: 1365x2048 single channel

Key Learning Points

  1. Image Representation: Images are NumPy arrays with shape (height, width, channels)
  2. Library Differences: matplotlib and OpenCV handle colors differently
  3. Preprocessing Importance: Raw images need standardization for ML models
  4. Format Conversions: RGB ↔ Grayscale, resizing, etc.
  5. File I/O: Loading from disk, saving processed versions

Applications

This preprocessing workflow is applicable to:

  • Computer vision model training
  • Image classification datasets
  • Object detection pipelines
  • Any ML project involving image inputs

Next Steps

After preprocessing, you might:

  • Normalize pixel values (0-1 range)
  • Apply data augmentation
  • Create training/validation splits
  • Feed into neural networks (CNNs, etc.)

This notebook provides the foundation for more advanced image processing and deep learning tasks.

The notebook also prints the full pixel matrix. That is mainly for learning purposes, so you can see that images are stored as arrays of numbers.

4. Display the original image

The image is visualized using:

img_plot = plt.imshow(img)
plt.show()

This confirms that the file loaded correctly and lets you visually compare the original image with later processed versions.

5. Resize the image using Pillow

The notebook imports Pillow:

from PIL import Image

Then it opens the same image and resizes it:

image = Image.open('/content/dogg.jpg')
image_resized = image.resize((200, 200))

Why resizing matters

Deep learning models usually expect fixed-size inputs. Real-world images can have very different dimensions, so resizing is a standard preprocessing step.

Here, the original image is resized from:

(1365, 2048, 3)

to:

(200, 200, 3)

This makes the image much smaller and more manageable.

6. Save and reload the resized image

The resized image is saved using:

image_resized.save('dog_image_resized.jpg')

Then it is read again and displayed:

img_res = mpimg.imread('/content/dog_image_resized.jpg')
img_res_plot = plt.imshow(img_res)
plt.show()

Finally, the notebook prints:

print(img_res.shape)

The saved output confirms:

(200, 200, 3)

Why this reload step is useful

This verifies that:

  • the resized file was saved successfully
  • the resized image can be loaded again
  • the new dimensions are correct

7. Load the image with OpenCV

Next, the notebook switches from Matplotlib/Pillow to OpenCV:

import cv2
img = cv2.imread('/content/dogg.jpg')

Again, it checks:

  • the object type
  • the image shape

The output shows:

numpy.ndarray
(1365, 2048, 3)

Why use OpenCV as well?

OpenCV is one of the most common libraries for image preprocessing in deep learning and computer vision projects. It provides efficient tools for:

  • resizing
  • filtering
  • color conversion
  • thresholding
  • edge detection
  • many other transformations

This notebook uses OpenCV mainly for grayscale conversion.

8. Convert the image to grayscale

The grayscale transformation is done with:

grayscale_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

After conversion, the notebook checks:

  • the type of the result
  • the shape of the grayscale image

The saved output shows:

numpy.ndarray
(1365, 2048)

What changed?

The original image shape was:

(1365, 2048, 3)

The grayscale image shape is:

(1365, 2048)

The third channel dimension disappears because a grayscale image stores only one intensity value per pixel instead of three color values.

Why grayscale is useful

Grayscale preprocessing is common when:

  • color is not necessary for the task
  • you want to reduce input size
  • you want simpler features
  • the target model works with one-channel images

9. Display grayscale image in Colab

Because OpenCV image display does not work the same way in Colab notebooks, the notebook imports:

from google.colab.patches import cv2_imshow

Then shows the image with:

cv2_imshow(grayscale_image)

This is the Colab-friendly way to render OpenCV images directly in notebook output.

10. Save the grayscale image

The final step writes the processed grayscale image to disk:

cv2.imwrite('dog_grayscale_image.jpg', grayscale_image)

The saved output returns:

True

which means the file was written successfully.

End-to-End Data Flow

This is the practical flow of the notebook:

download image
-> read image as array
-> inspect type, shape, and pixel values
-> display original image
-> resize image to 200x200
-> save resized image
-> reload resized image and verify new shape
-> load original image with OpenCV
-> convert image to grayscale
-> display grayscale version
-> save grayscale image

What This Notebook Teaches

This notebook is useful because it demonstrates the core idea that an image is just structured numerical data.

By the end of the notebook, you can see how to:

  • download image data
  • load images into arrays
  • inspect dimensions
  • visualize image content
  • resize inputs to standard dimensions
  • reduce color images to grayscale
  • save transformed outputs for later model use

These are foundational preprocessing skills for:

  • image classification
  • object detection
  • facial recognition
  • OCR
  • medical imaging
  • almost any computer vision task

Important Developer Notes

Colab assumptions

This notebook currently assumes a Google Colab runtime because it uses:

  • shell syntax like !wget
  • /content/... file paths
  • cv2_imshow from google.colab

If you run this notebook locally, you may need to:

  1. Replace /content/... paths with local file paths.
  2. Replace !wget with a manual download step or Python download code.
  3. Replace cv2_imshow with matplotlib or another local display method.

File naming issue to be aware of

The notebook downloads:

puppy-care-guide-for-new-parents.jpg

but later reads:

/content/dogg.jpg

That means the notebook assumes the image has either been renamed to dogg.jpg or another file with that name already exists in the Colab session.

From a developer perspective, this is the main workflow issue in the notebook.

Suggested improvement

A more consistent version would either:

  1. Read the downloaded file directly by its real name, or
  2. Rename the downloaded file immediately after download

so every later cell uses the same filename.

Ideas For Next Improvements

If you want to extend this project later, these would be strong next steps:

  1. Fix the filename mismatch between the downloaded file and the loaded file.
  2. Add markdown cells inside the notebook explaining each image-processing step.
  3. Add color-space conversion notes, especially the OpenCV BGR/RGB distinction.
  4. Normalize pixel values to the 0-1 range for model-ready inputs.
  5. Add cropping, thresholding, and edge-detection examples.
  6. Export preprocessing into a reusable Python script or function.
  7. Add a section showing how to prepare the final NumPy array for a CNN input tensor.

Conclusion

This notebook is a beginner-friendly walkthrough of image preprocessing for deep learning. It shows how raw image files move through a simple preparation pipeline:

  • image download
  • loading
  • inspection
  • visualization
  • resizing
  • grayscale conversion
  • saving processed results

That makes it a solid foundation for larger computer vision projects where clean and consistent image input matters.

About

image preprocessing for deep learning. Covers downloading, loading, resizing, grayscale conversion, and visualization using OpenCV, Matplotlib, and Pillow. Essential data preparation techniques for computer vision and neural network training.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages