Open In App

Python OpenCV – Getting and Setting Pixels

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss Getting and Setting Pixels through OpenCV in Python. 

Image is made up of pixels. A pixel will be denoted as an array. The 3 integers represent the intensity of red, green, blue in the same order. Eg. [0,0,0] in RGB mode represent black color. There are other modes as well-

  • HSV
  • Grayscale
  • CMY

Image can be read using imread() function which returns the matrix of pixels (default is RGB mode).

Image Used:

Syntax:

For Image shape: image.shape
For getting a pixel: image[row][col]
For setting a pixel: image[row][col] = [r,g,b]

Example 1: Python code to display image details

Python3




# import cv2 module
import cv2
  
# resd the image
img = cv2.imread('image.png')
  
# shape prints the tuple (height,weight,channels)
print(img.shape)
  
# img will be a numpy array of the above shape
print(img)


Output:

(225, 225, 3)
[[[ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]

[[ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]
 
...

[[ 72 133   9]
 [ 72 133   9]
 [ 72 133   9]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]

[[ 72 133   9]
 [ 72 133   9]
 [ 72 133   9]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]]

Here 225*225 pixels are there and every pixel is an array of 3 integers (Red, Green, Blue). 

Example 2: In this example, The single pixel can be extracted using indexing.

Python3




import cv2
  
# read the image
img = cv2.imread('image.png')
  
# this is pixel of 0th row and 0th column
print(img[0][0])


Output:

[ 87 157  14]

Example 3: Python code to make the black cross on the image.

For that we will extract all (i,j) such that i==j or i+j == image width and for all pixels with index (i,j), the value of the pixel will set to [0,0,0].

Python3




# import the cv2 package
import cv2
  
# read the image
img = cv2.imread('image.png')
for i, row in enumerate(img):
  
  # get the pixel values by iterating
    for j, pixel in enumerate(img):
        if(i == j or i+j == img.shape[0]):
  
                # update the pixel value to black
            img[i][j] = [0, 0, 0]
  
# display image
cv2.imshow("output", img)
cv2.imwrite("output.png", img)


Output:

Example 4: Get grayscale then the pixel will just be a number representing the intensity of white.

Python3




import cv2
img = cv2.imread('image.png', 0)
  
  
# shape prints the tuple (height,weight,channels)
print("image shape = ", img.shape)
  
# img will be a numpy array of the above shape
print("image array = ", img)
  
print("pixel at index (5,5): ", img[5][5])


Grayscale Image:

Output:

image shape =  (225, 225)
image array =  
[[106 106 106 ... 106 106 106]
 [106 106 106 ... 106 106 106]
 [106 106 106 ... 106 106 106]
 ...
 [ 88  88  88 ... 106 106 106]
 [ 88  88  88 ... 106 106 106]
 [ 88  88  88 ... 106 106 106]]
pixel at index (5,5):  106


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads