This is the code repository for Hands-On Image Processing and Computer Vision with Python, Second Edition, published by Packt.
Sandipan Dey
Analyzing and understanding visual data has become essential in modern applications such as healthcare, security, remote sensing, manufacturing, and digital media. This book provides a hands-on guide to image processing and computer vision using Python, following a practical approach that bridges theory with implementation. As you progress through the chapters, you will develop proficiency in Python 3 and implement algorithms spanning classical image processing, modern computer vision, and state-of-the-art (SOTA) deep learning and generative AI. The book covers image enhancement, restoration, filtering, segmentation, feature extraction, classification, and object detection using libraries including NumPy, OpenCV, PIL, SciPy, scikit-image, scikit-learn, TensorFlow, Keras, and PyTorch. Advanced chapters introduce CNNs, Vision Transformers, transformer-based segmentation, modern detection frameworks, GANs, diffusion models, foundation models, image-to-image translation, super-resolution, and multimodal vision-language understanding. Real-world applications span medical imaging, remote sensing, banking, augmented reality, autonomous driving, industrial inspection, and intelligent visual analytics. By the end of the book, you will be equipped to design and implement real-world visual computing solutions. *Email sign-up and proof of purchase required
- Build image processing and computer vision pipelines
- Apply image enhancement, restoration, and segmentation
- Implement image classification and object detection models
- Explore CNNs, Vision Transformers, and attention models
- Generate and edit images using GANs and diffusion models
- Develop multimodal vision-language AI applications
- Apply visual AI across diverse real-world domains
- Implement super-resolution, style transfer, and image-to-image translation
To get the most out of this book, ensure you have the following background and setup:
- Basic understanding of Python programming
- Foundational knowledge of machine learning, deep learning, and large language models (LLMs)
- Familiarity with computer vision workflows and AI model APIs is helpful
- Basic understanding of linear algebra, calculus, and optimization for the theoretical and mathematical concepts
- Experience working with AI development environments and APIs
- Python 3.12+
- A Python development environment such as Jupyter Notebook, Visual Studio Code (VS Code), or Google Colab
- A Kaggle account for downloading datasets
- A Hugging Face account and API token for accessing pretrained models and transformer pipelines
- An OpenAI API key (with available credits) for running the generative AI and multimodal examples
- Windows 11 x64 (primary development platform)
- macOS
- Linux
- Google Colab (web-based)
- A computer capable of running Python 3.12+
- At least 8 GB of GPU VRAM is recommended for deep learning and generative AI workloads
- The examples were developed and tested using Google Colab (T4 GPU) and an NVIDIA GeForce RTX 3050 (6 GB VRAM)
- Most examples can run on a CPU, but GPU acceleration is strongly recommended for training models and running generative AI workloads
- If using the digital version of this book, it is recommended to type the code manually or use the official GitHub repository (link provided in the next section)
- This helps avoid errors caused by copying and pasting code
Sandipan Dey is a Data Scientist with a wide range of interests in related areas, including machine learning, computer vision, image processing, and deep learning. He has been working in several areas of applied machine learning for the past few years. Sandipan earned his MS in Computer Science from UMBC and his BE in Computer Science from JU. He has published papers in several international data mining conferences and journals. He has authored several books on image processing with Python. He has also completed online specializations in machine learning, image processing, AI, and related areas through MOOCs.
If you find this repository useful, you may also be interested in the author's previous books on image processing and computer vision:
-
Hands-On Image Processing with Python
GitHub: https://github.com/PacktPublishing/Hands-On-Image-Processing-with-Python -
Python Image Processing Cookbook
GitHub: https://github.com/PacktPublishing/Python-Image-Processing-Cookbook -
Image Processing Masterclass with Python
GitHub: https://github.com/sandipan/Book-BPB-11-Image-Processing-Masterclass-with-Python -
Image Processing and Computer Vision Masterclass with Python (2nd Edition)
GitHub: https://github.com/sandipan/Book-BPB-600-Image-Processing-and-Computer-Vision-Masterclass-with-Python
These repositories contain complete source code, examples, datasets, and implementations accompanying the respective books.
- Getting Started with Digital Image Processing
- Image Manipulation
- More Image Manipulation
- Sampling & Fourier Transform
- Convolution & Spatial / Frequency Domain Filtering
- Frequency Domain Filtering
- Image Enhancement
- More Image Enhancement using Derivatives
- Image Restoration
- Image Segmentation - From Classical to Deep Learning Methods
- More Image Segmentation with Deep Learning
- Image Classification and Object Detection
- Generative AI in Image Processing and Computer Vision
The next few paragraphs describe how to install different image processing libraries and setup the environment for writing codes to process images using the classical image processing techniques in python. In the last few chapters of the book, we shall need to use a different setup when we shall use deep-learning-based methods.
We are going to use the pip (or pip3) tool to install the libraries, so we need to install pip first, in case it's not installed. As mentioned here, pip is already installed if we use python 3 >=3.4 downloaded from python.org or if we are working in a Virtual Environment created by virtualenv or pyvenv. Just we need to make sure to upgrade pip. How to install pip for different OS / platforms can be found here.
In python, there are many libraries which we can use for image processing. Some of the following libraries we are going to use are as below:
- numpy
- scipy
- scikit-image
- PIL (pillow)
- wand
- opencv-python
- scikit-learn
- mahotas
- matplotlib
- seaborn
The library matplotlib and seaborn will primarily be used for display purpose, where as
numpy will be used for storing an image. The scikit-learn library will be used for building
machine-learning models for image processing, scipy will be used mainly for image
enhancements. The scikit-image, mahotas and opencv will be used for different image
processing algorithms.
The following code block shows how the libraries that we are going to use can be downloaded and installed with pip from a python prompt (interactive mode) or in jupyter notebook:
!pip install numpy
!pip install scipy
!pip install scikit-image
!pip install scikit-learn
!pip install matplotlib
!pip install pillow
!pip install mahotas
!pip install opencv-python
!pip install jupyter
!pip install -U wandThere may be some additional installation instructions, depending on the OS platform one is going to use. We suggest the reader to go through the documentation sites for each of the libraries to get a detailed platform-specific installation instructions for each library. For example, for the scikit-image library, detailed installation instructions for different OS platforms can be found here. Also, one should be familiar to websites such as stackoverflow to resolve platform-dependent installation issues for different libraries.
Finally, we can verify whether a library is properly installed or not by importing it from the python prompt. If the library is imported successfully (no error message is thrown), then we don't have any installation issue. We can print the version of the library installed by printing it to the console.
The following code block shows the same for the python library scikit-image.
import skimage
print(skimage.__version__)0.17.2
As can be seen from above, an example output of the above code block 0.17.2.
The following code-block shows the version of PIL and numpy libraries, respectively, as a tuple:
import PIL
import numpy
PIL.__version__, numpy.__version__('8.3.2', '1.21.6')
Let's ensure that we have the latest versions for all the libraries.
We also recommend to download and install the latest version of the anaconda distribution from here, this will eliminate the need of explicit installation of many python packages.
More about installing anaconda for different OS can be found ''.
We are going to use jupyter notebooks to write our python code. So we need to install the jupyter package first from a python prompt with>>> pip install jupyter and then launch the jupyter notebook app in the browser using>>> jupyter notebook where we can create new python notebooks and choose a kernel. If we use anaconda, we do not need to install jupyter explicitly, the latest anaconda distribution comes with jupyter.
More about running jupyter notebooks can be found here.
We can even install a python package from inside a notebook cell, e.g., we can install scipy with the command !pip install scipy
For more information on installing Jupyter please refer to: http://jupyter.readthedocs.io/en/latest/install.html
To ensure reproduciblity, a requirements.txt file has been added corresponding to each chapter, to the book’s GitHub repository.
To install all required packages with compatible versions, run:
pip install -r requirements.txt
This ensures all dependencies match the versions used in the book's code examples.























