Prerequisite: Arithmetic Operations on Images | Set-1
Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are :
- AND
- OR
- XOR
- NOT
Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can be helpful in enhancing the properties of the input images.
NOTE: The Bitwise operations should be applied on input images of same dimensions
Input Image 1:

Input Image 2:

Bitwise AND operation on Image:
Bit-wise conjunction of input array elements.
Syntax: cv2.bitwise_and(source1, source2, destination, mask)
Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask
Python3
import cv2
import numpy as np
img1 = cv2.imread( 'input1.png' )
img2 = cv2.imread( 'input2.png' )
dest_and = cv2.bitwise_and(img2, img1, mask = None )
cv2.imshow( 'Bitwise And' , dest_and)
if cv2.waitKey( 0 ) & 0xff = = 27 :
cv2.destroyAllWindows()
|
Output:

Bitwise OR operation on Image:
Bit-wise disjunction of input array elements.
Syntax: cv2.bitwise_or(source1, source2, destination, mask)
Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask
Python3
import cv2
import numpy as np
img1 = cv2.imread( 'input1.png' )
img2 = cv2.imread( 'input2.png' )
dest_or = cv2.bitwise_or(img2, img1, mask = None )
cv2.imshow( 'Bitwise OR' , dest_or)
if cv2.waitKey( 0 ) & 0xff = = 27 :
cv2.destroyAllWindows()
|
Output:

Bitwise XOR operation on Image:
Bit-wise exclusive-OR operation on input array elements.
Syntax: cv2.bitwise_xor(source1, source2, destination, mask)
Parameters:
source1: First Input Image array(Single-channel, 8-bit or floating-point)
source2: Second Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask
Python3
import cv2
import numpy as np
img1 = cv2.imread( 'input1.png' )
img2 = cv2.imread( 'input2.png' )
dest_xor = cv2.bitwise_xor(img1, img2, mask = None )
cv2.imshow( 'Bitwise XOR' , dest_xor)
if cv2.waitKey( 0 ) & 0xff = = 27 :
cv2.destroyAllWindows()
|
Output:

Bitwise NOT operation on Image:
Inversion of input array elements.
Syntax: cv2.bitwise_not(source, destination, mask)
Parameters:
source: Input Image array(Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask
Python3
import cv2
import numpy as np
img1 = cv2.imread( 'input1.png' )
img2 = cv2.imread( 'input2.png' )
dest_not1 = cv2.bitwise_not(img1, mask = None )
dest_not2 = cv2.bitwise_not(img2, mask = None )
cv2.imshow( 'Bitwise NOT on image 1' , dest_not1)
cv2.imshow( 'Bitwise NOT on image 2' , dest_not2)
if cv2.waitKey( 0 ) & 0xff = = 27 :
cv2.destroyAllWindows()
|
Output:
Bitwise NOT on Image 1

Bitwise NOT on Image 2

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jan, 2023
Like Article
Save Article