Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Inverting an Image with Python

  • 25 Oct, 2024
  • Com 0
Inverting an Image with Python

Image inversion is a process in which each pixel’s intensity value is subtracted from the maximum possible value, creating a photographic negative of an original image.

If you are working with medical reports and imaging, you must have seen negative images like X-ray reports where image inversion is helpful.

Below is an example of a simple image and an inverted image:

Difference between normal and inverted imageYou can see from the above image that “Inverted Image” looks like a negative image.

Here are three ways to invert an image with Python:

  1. Using Pillow’s ImageOps (If you are looking for a simple approach)
  2. Using cv2.bitwise_not() (If you are looking for an optimized approach)
  3. Using 255-image subtraction (Univeral approach)

Method 1: Using Pillow’s ImageOps

Inverting an image means inverting its colors. The Pillow library provides an ImageOps module with the .invert() method for inverting the colors. Then, save the inverted image in your system.

Decision Tree Diagram

Decision Tree using ImageOps.invert()The image above highlights the process flow of inverting an image using the ImageOps.invert() method. First, we load an image using PIL. Then, inverting its color, and finally save that image.

Install the pillow library if not already installed:

pip install pillow

Code example

from PIL import Image, ImageOps

# Loading an image
image = Image.open('krunal.png').convert('RGB')

# Inverting the color of the image
im_invert = ImageOps.invert(image)

# Saving the image
im_invert.save('invert.jpg', quality=95)

Before conversion

Krunal

After conversion

kunal_inverted

The pillow approach is simple and intuitive and handles different image modes (RGB, RGBA) differently.

If you are working with large images, it won’t be as fast as OpenCV.

Method 2: Using cv2.bitwise_not()

The opencv-python is a computer vision library in Python that provides the cv2.bitwise_not() function that performs a bitwise NOT operation on each pixel, effectively inverting the image.

Decision Tree Diagram

Decision Tree using cv2.bitwise_not()The above image is self-explainable. First, we loaded an image using the cv2.imread() function and then inverted the color using the cv2.bitwise_not() function. Atlast, I saved the inverted image using the cv2.imwrite() method.

Install the opencv-python library if you have not installed it already:

pip install opencv-python

Code example

import cv2

# Loading the image
image = cv2.imread('krunal.png')

# Checking if the image was loaded successfully
if image is None:
    print("Error: Image not found.")
else:
    # Inverting the image
    inverted_image_bitwise = cv2.bitwise_not(image)

    # Saving the inverted image
    cv2.imwrite('krunal_inverted', inverted_image_bitwise)

Before conversion

Krunal

After conversion

kunal_inverted

If you are already working with computer vision projects, you should always use the cv2.bitwise_not() approach. It is fast and concise. This approach is suitable when your images have 8 bits and the value changes from 0 to 255. The only con is that it may support limited data types supported by OpenCV.

Method 3: Using 255-image subtraction

You can subtract each pixel value of the image from 255 which is also the maximum pixel value for 8-bit images. The basic syntax is this:

inverted_image_subtract = 255 - your_image

Decision Tree Diagram

Decision Tree diagram using subtractionYou can see from the above diagram that the process is very simple. We are simply subtracting 255 from each pixel of an image.

Code example

import cv2

# Loading the image
image = cv2.imread('Krunal.png')

# Checking if the image was loaded successfully
if image is None:
    print("Error: Image not found.")
else:
    # Inverting the image by subtraction
    inverted_image_subtract = 255 - image

    # Saving the inverted image
    cv2.imwrite('kunal.png', inverted_image_subtract)

Before conversion

Krunal

After conversion

kunal_inverted

The big advantage of the subtracting approach is that it works universally across different image data types.

You can use this approach with OpenCV as well as Numpy arrays. However, it requires knowledge of the maximum pixel value which varies with data type (e.g., 255 for 8-bit, 65535 for 16-bit).

Post Views: 50
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Adding Text to Images using Python
Rotating an Image with Python [ROI, Affine, Perspective Rotations]

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend