Home » Python

Find complementary image of the RGB image in Python

In this article, we will see how to find complementaryimage of the RGB image and show it in python?
Submitted by Ankit Rai, on April 27, 2019

Complementary image is a transformed image such that it consists of complementary colours of the ones, which is present in the original image.

For finding the complement of an image, we have to simply subtract each pixel value from the maximum pixel value supported by the class. (in this case, class - uint8, the maximum value of pixel can be 255) and store in the output image array. In the output image, dark areas become lighter and light areas become darker.

Complementary image = 255 – original image.

In this program, we will be using two functions of OpenCV-python (cv2) module. let's see their syntax and descriptions first :

1) imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.

2) imshow():
It takes window name and image matrix as an argument in order to display an image in a display window with a specified window name.

Python program to find complementary image of the RGB image

# open-cv library is installed as cv2 in python
# import cv2 library into this program
import cv2

# read an image using imread() function of cv2
# we have to  pass only the path of the image
img = cv2.imread(r'C:/Users/user/Desktop/pic1.jpg')

# displaying the image using imshow() function of cv2
# In this : 1st argument is name of the frame
# 2nd argument is the image matrix
cv2.imshow('original image',img)

# Find complements of img array and
# store it in the variable
comp_image = 255 - img

# Show the image formed
cv2.imshow("Complementary image",comp_image);


complementary image of the RGB image in Python - output

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.