Change the Pixel Value of an Image in OpenCV Python | OpenCV Tutorial

In Python OpenCV Tutorial, Explained How to Change Pixels Color in an image using NumPy Slicing and indexing.

Get the answers of below questions:

  1. What is pixel?
  2. What is resolution?
  3. How to read image pixel?
  4. How to get image pixel?
  5. How to print image pixel?
  6. How to change the pixel value of an image in python opencv?
  7. How do I pixelate an image in OpenCV?
  8. How do I change the pixel value of an image in OpenCV Python?

Add colour pixels in Image

Impor Libraries

"""
Video Tutorial Link:
Change the Pixel Value of an Image in OpenCV Python | | OpenCV Tutorial in Hindi | Computer Vision: https://youtu.be/8gI3NSfCpKw
"""
import cv2
import numpy as np
import time
import random

Read and Show Image

img_path = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\pexels-bess-hamiti-35188.jpg"

img = cv2.imread(img_path)
img = cv2.resize(img, (1280, 720))
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New single color pixel in a Image

img[0][0] ## Read pixel
# Add white pixels
img_copy = img.copy()
img_copy[0][0] = np.array([255,255,255])

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Add white, black, Red, Green, Blue pixels
img_copy = img.copy()
img_copy[0][0] = np.array([255,255,255]) # White pixel
img_copy[0][1] = np.array([0,0,0]) #  Black pixel
img_copy[0][2] = np.array([0,0,255]) # Red pixel
img_copy[0][3] = np.array([0,255,0]) # Green Pixel
img_copy[0][4] = np.array([255,0,0]) # Blue pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a row of Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for cols_index in range(img_width):
    img_copy[0][cols_index] = np.array([255,255,255]) # White pixel
    img_copy[1][cols_index] = np.array([0,0,0]) #  Black pixel
    img_copy[2][cols_index] = np.array([0,0,255]) # Red pixel
    img_copy[3][cols_index] = np.array([0,255,0]) # Green Pixel
    img_copy[4][cols_index] = np.array([255,0,0]) # Blue pixel
    
cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a row of Image with logic/alternatively

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for cols_index in range(img_width):
    if cols_index%2 == 0:
        img_copy[0][cols_index] = np.array([255,255,255]) # White pixel
        img_copy[1][cols_index] = np.array([0,0,0]) #  Black pixel
        img_copy[2][cols_index] = np.array([0,0,255]) # Red pixel
        img_copy[3][cols_index] = np.array([0,255,0]) # Green Pixel
        img_copy[4][cols_index] = np.array([255,0,0]) # Blue pixel
    
cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New color pixel in a Image with logic/alternatively

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    for cols_index in range(img_width):
        #if cols_index%2 == 0:
        if cols_index%50 == 0:
            #img_copy[rows_index][cols_index] = np.array([255,255,255]) # White pixel
            img_copy[rows_index][cols_index] = np.array([0,0,255]) # Red pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(img_width):
            if cols_index%2 == 0:
            #if cols_index%50 == 0:
                img_copy[rows_index][cols_index] = np.array([255,255,255]) # White pixel
                #img_copy[rows_index][cols_index] = np.array([0,0,255]) # Red pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Add New random color pixel randomly in a Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(img_height):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(random.randint(0,1000)):
            #if cols_index%2 == 0:
            #if cols_index%50 == 0:
                b_pixel = random.randint(0,255)
                g_pixel = random.randint(0,255)
                r_pixel = random.randint(0,255)
                rand_cols_index = random.randint(0,img_width-1)
                img_copy[rows_index][rand_cols_index] = np.array([b_pixel,g_pixel,r_pixel]) # random pixel
                #img_copy[rows_index][cols_index] = np.array([255,255,255]) # white pixel

cv2.imshow("Image", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()
random.randint(0,255)

Add New color pixels in portion or any location of Image

img_copy = img.copy()

img_width= img_copy.shape[1]
img_height= img_copy.shape[0]

print("img_width: ", img_width)
print("img_height: ", img_height)

for rows_index in range(55, 700):
    if rows_index%2 == 0:
    #if rows_index%20 == 0:
        for cols_index in range(380, 950):
            if cols_index%2 == 0:
            #if cols_index%50 == 0:
                b_pixel = random.randint(0,255)
                g_pixel = random.randint(0,255)
                r_pixel = random.randint(0,255)
                #rand_cols_index = random.randint(0,img_width-1)
                #img_copy[rows_index][cols_index] = np.array([b_pixel,g_pixel,r_pixel]) # random pixel
                #img_copy[rows_index][cols_index] = np.array([255,255,255]) # white pixel
                img_copy[rows_index][cols_index] = np.array([255,0,0]) # blue pixel
cv2.imshow("Image", img_copy)
cv2.imwrite("effect_img.png", img_copy)
cv2.waitKey(0)
cv2.destroyAllWindows()

Leave a Reply