close

How do I access the pixels of an image using OpenCV-Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I access the pixels of an image using OpenCV-Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I access the pixels of an image using OpenCV-Python?

  1. How do I access the pixels of an image using OpenCV-Python?

    which will print indeed a vertical set of numbers. If you want to modify the values of the pixels use img.itemset().

  2. access the pixels of an image using OpenCV-Python

    which will print indeed a vertical set of numbers. If you want to modify the values of the pixels use img.itemset().

Method 1

I don’t see what’s the purpose of your x variable. You don’t need it.

Simply use:

img = cv2.imread('/path/to/Capture1.jpg',0)
rows,cols,_ = img.shape

for i in range(rows):
    for j in range(cols):
        k = img[i,j]
        print(k)

which will print indeed a vertical set of numbers. If you want to modify the values of the pixels use img.itemset().

Method 2

Access specific pixel in Python

import cv2
image = cv2.imread("sample.jpg")
pixel= image[200, 550]
print pixel

output: [ 73 89 102]

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read