How to get the RGB values from an Image using Python?

 This post is a continuation from an earlier post at Reading an Image and displaying where we learned how to read an image from disk  and display it.
In this post we will read the dimensions of a the ndarray that results from reading an image.

Then we will separate the RGB values and then create a Black & White Image and then display it. We will also save it to disk.

import cv2 as pp

pic = pp.imread("K:\\Blogspot\\pics\\program output\\latest.png")

print()

if pic is None:

    print("Picture not found")

else:

    print(type(pic))

    mx,my,mz=pic.shape

    

    for x in range(mx):

        for y in range(my):

            b=int(pic[x][y][0])#Blue Value

            g = int(pic[x][y][1])#Green Value

            r = int(pic[x][y][2])#Red Value

            bwvalue=int((r+g+b)//3)#Average RGB


            pic[x][y][0]=bwvalue

            pic[x][y][1] =bwvalue

            pic[x][y][2] =bwvalue

            print(r,g,b)


    output=pp.imshow("Show Picture", pic)

    print(output)

    pp.imwrite("K:\\Blogspot\\pics\\program output\\bw.png",pic)#Save to disk

    output=pp.waitKey(0)



Here is the output
Varanasi Software Junction: Python Image Processing

And here is the image saved on disk.

Varanasi Software Junction: Python Image Processing





Post a Comment

0 Comments

Me