The Image Processing Series Part 2: Setting pixels in Python

Filed Under: Python Modules
Opencv Part2 Min

We’re back with another article on working with images and in this one, you’ll learn the steps for setting pixels with the OpenCV module in Python. If you’re just starting out, make sure you learn how to load images in Python which is covered in part 1 of this series.

Mind you, we still haven’t even scratched the surface, but, like they say, Rome wasn’t built in a day.

This is an article that extends off the previous introductory article and hence, will be skipping the installation process of the OpenCV module.

Getting and setting pixels on an image

A large part of Image Processing has to do with processing.

As such, we’ll be dipping our hands into this aspect and we’ll be working with an image, and change the properties of the image.

This means that we’ll be modifying the data on the image by manipulating the pixels.

In simple words, altering them.

Implementing the course with OpenCV

Working on OpenCV is a lot more hands on than you might think, we’ll be manipulating images.

So, it’s a good practice to download a few low resolution, medium resolution, and high resolution pictures that you can work with.

This would help in resolving the issue of trying to find images to work with.

A few places that you can find image datasets,

  • Caltech 101 – Caltech provides us with a great dataset to work with Image Processing, we’ll also be using it in future implementations of OpenCV.
  • MNIST – Known for providing data used to train models on identifying digits of the numerical system.
  • Unsplash – A resort that provides high-resolution images without copyright. Remember to credit the author, and discuss with them if you plan to commercialize it, however.
  • Google Images, The last resort. Hey, sometimes, it’s inevitable. Use creative commons if you have to.

Getting Images and Setting Pixels in Python OpenCV

We’ll be using the argparse module for this script, allowing us to provide the image file path in the command line.

1. Import the required modules

Let’s import both the modules,

# importing argument parsers
import argparse

# importing the OpenCV module
import cv2

2. Reading the Image

Next, we’ll create an object that takes in the argument parameter and then, assigns the image path to args.

Proceeding forward, we let the image variable read the image from args using the cv2.imread() function.

# initializing an argument parser object
ap = argparse.ArgumentParser()

# adding the argument, providing the user an option
# to input the path of the image
ap.add_argument("-i", "--image", required=True, help="Path to the image")

# parsing the argument
args = vars(ap.parse_args())

# reading the image location through args
# and reading the image using cv2.imread
image = cv2.imread(args["image"])

3. Assigning Dimensions to Variables

Now, let’s assign the dimensions of the image to a few variables such that we can use them later for the representation of a section of the image.

But, let’s view the original image before proceeding to view specific sections.

# assigns the image dimensions to two different variables
(h, w) = image.shape[:2]

# Displays the image using cv2.imshow
cv2.imshow("Original", image)

# Displays the image until the user presses a key closing the display

4. Coloring Individual Pixels

Let us now assign a color to a specific pixel. We’ll be assigning the red color to the pixel at the location (0, 0).

# Values of the pixel before assigning a color to it
(b, g, r) = image[0, 0]
print("Pixel at (0,0) - Red: {r}, Green: {g}, Blue: {b}".format(r=r, g=g, b=b))

# Values of the pixel after assigning a color to it
image[0, 0] = (0, 0, 255)
(b, g, r) = image[0, 0]
print("Pixel at (0,0) - Red: {r}, Green: {g}, Blue: {b}".format(r=r, g=g, b=b))

# Displaying the altered image
cv2.imshow("Original-RedDot@0,0", image)

It’s difficult to notice with a larger image, and you’ll really have to look closely to notice it.

So, let’s instead make a lot more visible to the eye!

We’ll cover the entirety of the first quarter of the image with the color green! We’ll first view the first quarter through the dimensions before coloring it in.

# Providing values to display a quarter of the image
(cX, cY) = (w / 2, h / 2)

# Displaying a quarter of the image
tl = image[0 : int(cY), 0 : int(cX)]
cv2.imshow("Top Left Corner", tl)

The three other quarters have also been provided, in case you wish to test it on any one of them 馃槈

We then proceed to color the first quarter accessing the values of the image through lists which signify dimensions.

# Top Right, Bottom Right, Bottom Left
tr = image[0 : int(cY), int(cX) : w]
br = image[int(cY) : h, int(cX) : w]
bl = image[int(cY) : h, 0 : int(cX)]

print("Top Right Corner", tr)
print("Bottom Right Corner", br)
print("Bottom Left Corner", bl)

# Altering the first quarter of the image to turn into green color
image[0: int(cY), 0: int(cX)] = (0, 255, 0)

# Displaying the updated image
cv2.imshow("Updated", image)

Upon finally doing so, you’ll be greeted with a updated image with a colored first quarter!

You can experiment with this as much as you’d like to, as we’re not modifying the originally existing image.


Now, you know how to work with the data present in Images.

Manipulating Pixels in Images might seem like not that huge of a deal as of right now, but, these are baby steps in working with Image Processing.

Want a sneak peek into working with the concepts in the future? Here’s a few articles on working with image processing!

Work with OpenCV and Facial Recognition, and a Java implementation of Android and CameraX OpenCV!


Generic selectors
Exact matches only
Search in title
Search in content