MEDIA GUIDES / Image Effects

Image Processing with Python

image_processing_python

Image processing is the process of transforming or modifying images into digital formats to obtain a specific output. In today’s digital world, image processing is used for a variety of reasons. In photography, image processing is used to add visual effects to the final output of images for enhancement and manipulation.

In a more technical scenario, such as machine learning, image processing is used to develop computer vision models. In social media, entertainment, and communication, image processing is used to apply various filters and effects to enhance user-generated content.

In this article, we’ll explore the various tools and methods you can use for image processing in the Python programming language ecosystem.

In this article:

image_processing_python

Image processing libraries in Python

Image processing in Python cuts across a lot of domains. In this section, we’ll examine some of the commonly used libraries in Python and see examples of some in action.

OpenCV

OpenCV (Open Source Computer Vision Library) is a popular free and open-source library for developing computer vision applications. Although OpenCV was initially written in C++, it has a series of language bindings and interfaces that allow its usage in many programming languages like Python, Java, JavaScript, etc.

Examples of uses of OpenCV in image processing include basic image editing (e.g., filters and effects, geometric transformations, format conversion), machine learning algorithms for developing image recognition and detection systems, computational photography in science and research, robotics, and security in industry and businesses.

To use OpenCV in Python, simply install the package with the command below:

pip install opencv-python

Here’s an example code showing how to crop an image with OpenCV:

import cv2

# Load an image from file
image = cv2.imread('image.jpg')

# Define the coordinates for the top-left and bottom-right corners of the cropping rectangle
start_row, start_col = 50, 50
end_row, end_col = 200, 200

# Crop the image
cropped_image = image[start_row:end_row, start_col:end_col]

# Save the cropped image to a new file
cv2.imwrite('path_to_save_cropped_image.jpg', cropped_image)

Here’s another example below showing OpenCV being used for object recognition. Source

image_processing_python

You can learn more about using OpenCV for image processing in Python in this comprehensive list of tutorials.

Cloudinary

Cloudinary is a cloud-based media management solution that offers an API-driven approach to image and video manipulation. It provides extensive capabilities for various image processing tasks, such as resizing, transforming, enhancing, and converting from one format to another.

Cloudinary can be used within many programming languages, one of which is Python. You can integrate Cloudinary into your Python applications using the Cloudinary Python SDK.

You can install Cloudinary using pip as follows:

pip install cloudinary

Here’s an example in Python showing how you can use Cloudinary for applying various transformations to an image:

import cloudinary
import cloudinary.uploader
import cloudinary.api
from cloudinary import CloudinaryImage

cloudinary.config( 
  cloud_name = "sample", 
  api_key = "874837483274837", 
  api_secret = "a676b67565c6767a6767d6767f676fe1",
  secure = True
)

CloudinaryImage("your_image.png").image(transformation=[
  {'gravity': "face", 'height': 150, 'width': 150, 'crop': "thumb"},
  {'radius': 20},
  {'effect': "sepia"},
  {'overlay': "cloudinary_icon"},
  {'effect': "brightness:90"},
  {'opacity': 60},
  {'width': 50, 'crop': "scale"},
  {'flags': "layer_apply", 'gravity': "south_east", 'x': 5, 'y': 5},
  {'angle': 10}
  ])

Pillow

Pillow is a fork of the defunct Python Imaging Library (PIL). PIL was discontinued in 2011 and only supported Python 2, but Pillow was developed as its successor. Pillow provides powerful modules for performing various image processing operations in Python.

You can install Pillow using pip as shown below:

python3 -m pip install --upgrade Pillow

Here’s an example usage of Pillow to convert an RGB image to grayscale:

from PIL import Image

# Load an image from file
image = Image.open('original.jpg')

# Convert the image to grayscale
gray_image = image.convert('L')

# Save the grayscale image and change the file format
gray_image.save('grayscale.png')

Wand

Wand is a Python binding of ImageMagick. ImageMagick allows for reading, converting, and editing images in over 200 formats. Wand provides an easy-to-use Python interface for these capabilities, enabling complex image processing tasks.

To use Wand, you need to install both ImageMagick and the Wand library. The installation steps vary for different operating systems. You can check how to install it for your OS here.

Here’s how you can use Wand to resize an image:

from wand.image import Image

# Load an image from file
with Image(filename='image.jpg') as img:
    # Resize the image to 50% of its original size
    img.resize(int(img.width / 2), int(img.height / 2))
    img.save(filename='resized_image.jpg')

scikit-image

scikit-image is an open-source image processing library with a collection of algorithms for image segmentation, geometric transformations, color space manipulation, analysis, filtering, morphology, feature detection, and more.

scikit-image is tailored for scientific image processing and analysis, and it’s built on top of NumPy, enabling numerical operations and integration with other scientific libraries such as sciPy and Matplotlib.

For a comprehensive overview on installing scikit-image, see this article.

The example code below shows how you can use scikit-image for feature extraction:

from skimage import feature, io

# Load an image
image = io.imread('path_to_image.jpg', as_gray=True)

# Extract features using Canny edge detector
edges = feature.canny(image)

# Display the result
io.imshow(edges)
io.show()

SimpleCV

SimpleCV is an open-source framework for building computer vision applications in Python. It exposes a set of simple functions that internally call functions and methods in OpenCV and several other high-powered computer vision libraries.

SimpleCV is best used for performing simple image processing tasks in Python or quick prototyping if you don’t want to go through the hassle of writing complex code.

To install SimpleCV, visit the official download page to ensure you’re installing the latest version.

GraphicsMagick

GraphicsMagick is a free and open-source image processing library based on ImageMagick. According to its website, GraphicsMagick provides a robust and efficient collection of tools and libraries that support reading, writing, and manipulating images in over 92 major formats, including important formats like DPX, GIF, JPEG, JPEG-2000, JXL, PNG, PDF, PNM, TIFF, and WebP.

GraphicsMagick can be used through command-line tools or via its bindings in various programming languages, including Python. The Python binding for GraphicsMagick is called pgmagick.

You can install pgmagick as follows:

pip install pgmagick

The following example shows how you can use pgmagick for drawing on an image:

from pgmagick import Image, DrawableCircle, DrawableText, Geometry, Color
im = Image(Geometry(300, 300), Color("yellow"))
circle = DrawableCircle(100, 100, 20, 20)
im.draw(circle)
im.fontPointsize(65)
text = DrawableText(30, 250, "Hello pgmagick")
im.draw(text)
im.write('hoge.png')

Wrapping Up

There are various tools and techniques available for image processing in Python. In this article, we looked at different Python image processing packages and modules. As we’ve seen, there are various possibilities to consider, and there is no clear winner. However, your decision will often be influenced by a number of factors, such as use cases, simplicity of usage, and so on.

Empower your development team with Cloudinary’s easy-to-use APIs and SDKs. Sign up for free today!

QUICK TIPS
Colby Fayock
Cloudinary Logo Colby Fayock

In my experience, here are tips that can help you optimize image processing workflows in Python:

  1. Use OpenCV’s GPU acceleration for real-time tasks Leverage OpenCV’s CUDA module if you’re working with high-resolution images or video streams. This enables GPU acceleration, significantly speeding up tasks like face detection, object tracking, and image transformations.
  2. Combine OpenCV and scikit-image for advanced analysis OpenCV excels at image manipulation, while scikit-image is built for scientific image analysis. By combining both, you can perform robust preprocessing (OpenCV) and apply complex analysis algorithms (scikit-image) for better results.
  3. Optimize memory usage with Pillow’s lazy loading Use Pillow’s Image.load() only when you need to manipulate images. This lazy loading technique keeps large image files from consuming memory unnecessarily and helps avoid performance bottlenecks when dealing with many images.
  4. Batch process images with Cloudinary When processing a large number of images, use Cloudinary’s bulk transformation features, such as batch cropping or resizing, in a single API call. This minimizes bandwidth usage and speeds up the workflow by reducing multiple network requests.
  5. Handle large datasets with Dask for parallel computing Use Dask in combination with libraries like scikit-image or OpenCV to process large datasets of images in parallel, making your image pipelines scalable for tasks like training deep learning models or bulk image analysis.
  6. Automate transformation workflows with Cloudinary presets Create transformation presets in Cloudinary to streamline repetitive tasks like resizing, cropping, and applying filters. This not only saves time but ensures consistency across all processed images.
  7. Use ImageMagick via Wand for complex image formats If you’re working with unusual or complex image formats (e.g., TIFF, PDF, or DPX), use Wand with ImageMagick’s robust format support. This gives you flexibility in reading and manipulating images across different formats.
  8. Chain transformations for efficient processing Combine multiple image transformations (e.g., resize, crop, rotate) in a single step with libraries like OpenCV or Cloudinary’s API. Chaining these operations reduces memory overhead and speeds up processing times.
  9. Pre-process images for machine learning models Use scikit-image for image normalization, contrast adjustment, or geometric transformations before feeding images into machine learning models. Proper preprocessing ensures that your models perform optimally and generalize better across datasets.
  10. Monitor performance with profiling tools Use Python’s cProfile or timeit modules to identify bottlenecks in your image processing pipelines. This helps you optimize areas of your code that are consuming the most time and resources, particularly when dealing with real-time applications.

These strategies will help you efficiently process images in Python, whether for scientific analysis, machine learning, or digital asset management.

Last updated: Oct 23, 2024