Python – Color Inversion using Pillow
Color Inversion (Image Negative) is the method of inverting pixel values of an image. Image inversion does not depend on the color mode of the image, i.e. inversion works on channel level. When inversion is used on a multi color image (RGB, CMYK etc) then each channel is treated separately, and the final result if formed by callibrating the results of all the channels.
We would be using pillow (
PIL) library for obtaining negative of an image. To install the library, execute the following command in the command-line:-
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
pip install pillow
Note: Several Linux distributions tend to have Python and PIL preinstalled into them.
In this article, 2 methods have been described for inverting color space of an image. The first one is an inbuilt method using
ImageChops.invert() function. In the second one we would be inverting the image by elementwise substraction of pixel values.
Sample Image –
Using inbuilt method
ImageChops.invert() for Negating color.
Firstly we import the
ImageChops module for using
invert() method. Then we open the test image (
test.jpg), and save it’s image object. Now we passed that image object to
ImageDraw.invert() which returns the inverted image. At last, we displayed the color inverted image.
Things to keep in mind while using
The method used for getting the inverse of an image is subtraction of the maximum value/intensity of a pixel by the value of current pixel. The resultant value is guided by the formula –
INV is the resultant inverted pixel,
I^MAX is the maximum intensity level in a given color mode and
I(x, y) is the intensity (pixel value) of image/color channel at a particular pair of coordinates.
Firstly we import numpy into our code, as numpy allows fast elementwise operations on matrices and offers several arithmetic operations on arrays. Then we open the test image using
Image.open(), and store the returned image object in the variable
img. Then we create an array (
img_arry) from the pixel values obtained from the opened image object (
img). This is done so as to allow elementwise substraction operation offered by the numpy library. Now we substract 255 from each channel/pixel value, this results in all the pixel values being inverted. Now, we use this resultant matrix to create an new image (
inverted_img). Finally we saved the image, under the name of
Some things to keep in mind –
- It should be ensured that the input image does not contain an alpha channel. This is because when the line
img_arry = 255 - img_arrywill execute for an image containing alpha channel, it would invert the alpha channel values as well. This would lead to inconsistency in the output image, as we may end up with completly transparent images (which is not a part of color inversion). One way to allow processing
RGBAimages is firstly converting them to
RGBcolor mode using
Image.convert('RGB'). Alternatively, we could extract the alpha channel using
Image.getdata(band=3)and then later combine it to the final image to get back the original
This input image
sample.jpgis chosen intentionally of the format
.jpgas a JPG/JPEG image format doesn’t support transparency or alpha channels.
- Input image should not be of
P(paletted) mode. As a paletted image does not contain pixel values at coordinates, but rather index to pixel values belonging to a color map (of varying sizes). Therefore, image inversion would lead to inconsistent results.
- The value
I_max = 255is assigned assuming that the maximum intensity achievable in the specific image mode is 255. The value isn’t hardcoded. The value depends on the color mode, and therefore could be less then 255 (ex.
1in bilevel image) or greater than 255 (ex.
32536in 16 Bit Unsigned Greyscale mode).