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.
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.
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!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.
import matplotlib.image as mpimg
import matplotlib.pyplot as pltWhat 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.
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.
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.
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)
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.
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.
from PIL import ImageWhat it does: Imports the Pillow library (PIL fork).
Why: PIL provides powerful image manipulation functions, especially for resizing.
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.
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.
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.
print(img_res.shape)Output: (200, 200, 3)
What it does: Confirms the new dimensions.
Why: Ensures the resize operation worked as expected.
import cv2What it does: Imports OpenCV library.
Why: OpenCV is a powerful computer vision library with extensive image processing capabilities.
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).
type(img)
img.shapeOutput: <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.
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.
type(grayscale_image)
grayscale_image.shapeOutput: <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.
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.
cv2.imwrite('dog_grayscale_image.jpg', grayscale_image)What it does: Saves the grayscale image.
Why: Creates a final processed version.
- Open the notebook in Google Colab (recommended) or Jupyter Lab
- Run cells sequentially from top to bottom
- The notebook will download the sample image automatically
- Each step builds on the previous, demonstrating the complete preprocessing pipeline
- Original image: 1365x2048 RGB
- Resized image: 200x200 RGB
- Grayscale image: 1365x2048 single channel
- Image Representation: Images are NumPy arrays with shape (height, width, channels)
- Library Differences: matplotlib and OpenCV handle colors differently
- Preprocessing Importance: Raw images need standardization for ML models
- Format Conversions: RGB ↔ Grayscale, resizing, etc.
- File I/O: Loading from disk, saving processed versions
This preprocessing workflow is applicable to:
- Computer vision model training
- Image classification datasets
- Object detection pipelines
- Any ML project involving image inputs
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.
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.
The notebook imports Pillow:
from PIL import ImageThen it opens the same image and resizes it:
image = Image.open('/content/dogg.jpg')
image_resized = image.resize((200, 200))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.
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)This verifies that:
- the resized file was saved successfully
- the resized image can be loaded again
- the new dimensions are correct
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)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.
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)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.
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
Because OpenCV image display does not work the same way in Colab notebooks, the notebook imports:
from google.colab.patches import cv2_imshowThen shows the image with:
cv2_imshow(grayscale_image)This is the Colab-friendly way to render OpenCV images directly in notebook output.
The final step writes the processed grayscale image to disk:
cv2.imwrite('dog_grayscale_image.jpg', grayscale_image)The saved output returns:
Truewhich means the file was written successfully.
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
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
This notebook currently assumes a Google Colab runtime because it uses:
- shell syntax like
!wget /content/...file pathscv2_imshowfromgoogle.colab
If you run this notebook locally, you may need to:
- Replace
/content/...paths with local file paths. - Replace
!wgetwith a manual download step or Python download code. - Replace
cv2_imshowwithmatplotlibor another local display method.
The notebook downloads:
puppy-care-guide-for-new-parents.jpgbut later reads:
/content/dogg.jpgThat 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.
A more consistent version would either:
- Read the downloaded file directly by its real name, or
- Rename the downloaded file immediately after download
so every later cell uses the same filename.
If you want to extend this project later, these would be strong next steps:
- Fix the filename mismatch between the downloaded file and the loaded file.
- Add markdown cells inside the notebook explaining each image-processing step.
- Add color-space conversion notes, especially the OpenCV BGR/RGB distinction.
- Normalize pixel values to the
0-1range for model-ready inputs. - Add cropping, thresholding, and edge-detection examples.
- Export preprocessing into a reusable Python script or function.
- Add a section showing how to prepare the final NumPy array for a CNN input tensor.
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.