Image Effects Creating Custom Image Cropping Interfaces in Android How to Create Simple Yet Effective PHP Overlay Understanding Real-Time Image Recognition How to add a shadow effect to an image with CSS How to crop an image in Flutter with Cloudinary How To Rotate an Image with Java Image Processing with Python Rotating an image with CSS Enhancing User Experience with a Responsive Image Slider Building a Python Image Recognition System Building an Interactive JavaScript Image Manipulation Tool Image Align Centering with HTML and CSS Efficient Image Cropping Techniques with Angular and Cloudinary Ultimate Guide to Photo Gallery on Android A Comprehensive Guide to Adding Text to Images on Android Mastering Background Changes in React Applications Comprehensive Guide on Changing Background on Android Devices Mastering Image Rotation in Java A Guide to Adding Text to Images with Python A Guide to Converting Images to Grayscale with Python Introduction Creating an Image Overlay with JavaScript Rotating an Image in Python Creating a Dynamic Photo Gallery with jQuery Creating An Interactive Photo Gallery Using JavaScript Mastering Overlay in Android Mastering Angular Overlay: A Comprehensive Guide Comprehensive Guide to Overlay in Flutter Mastering Overlay React for Responsive Design Solutions Create a Blurred Image with PHP: A Comprehensive Guide Guide to Using Blur Image in Flutter Mastering Blur Image in React Native Mastering Image Blurring in Python Mastering the Art of Image Blurring Mastering the Art of Image Blurring in Java The Ultimate Guide to Blurring Images on Android Understanding and Implementing Blur Image in JQuery An Extensive Walkthrough of Blurring Images with JavaScript How to Use HTML, CSS, and JavaScript to Make an Image Slider HTML Image Tag How to Crop GIFs? How to Align Images with CSS Ken Burns Effect – Complete Guide and How to Apply It Cartoonify – Complete Guide on Cartoonify Image Effect Mastering Web Aesthetics: A Comprehensive Guide to Gradient Fades Sepia Effect: The Ultimate Guide to the Sepia Photo Effect What is Vignette? Guide to Vignette Image Editing Pixelate – The Ultimate Guide to the Pixelation Effect How to Outline an Image: Enhancing Visual Appeal and Depth Make Your Photos Pop with Image Effects Upscale Image – Developers guide to AI-driven image upscaling Image Manipulation: History, Concepts and a Complete Guide A Full Guide to Object-aware Cropping Simplify Your Life with Automatic Image Tagging How To Resize Images In WordPress How To Create a Progress Bar For Asset Uploads Animated GIFs – What They Are And How To Create Them How To Automatically Improve Image Resolution AI Drop Shadow Get Image Dimensions From URLs Automatically Add Sepia Effect To Images Automatically Make an Image a Cartoon Automatically Add Blur Faces Effect To Images Automatically Add Background Removal Effect to an Image How to Resize an Image with React How to Easily Resize an Image with React Native

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!

Last updated: Sep 8, 2024