Predictive Hacks

Iterate over Image Pixels

Today, we will provide an example of how we can get image pixels and how we can change them using the Pillow python library. You can find a short tutorial in Pillow here.

Usually, the images follow the RGB color model which means that every pixel is a vector of 3-D, where each position refers to the R (Red), G (Green) and B (Blue) respectively, each one taking values from 0-255. There is also the RGBA where we add another dimension called “alpha” which indicates how opaque each pixel is, taking values from 0-255 as well. Finally, there are the Grayscale images where each pixel is 1-D taking values from 0-255.

You should load the following libraries for the following examples:

import PIL
from PIL import Image
import requests
from io import BytesIO
from PIL import ImageFilter
from PIL import ImageEnhance
from IPython.display import display
import numpy as np

Q: How to get the “mode” of the image (i.e. RGB, RGBA, L)?

URL = "https://www.timeanddate.com/scripts/cityog.php?title=Current%20Local%20Time%20in&city=New%20York&state=New%20York&country=USA&image=new-york1"
response = requests.get(URL)
img = Image.open(BytesIO(response.content))

# get the mode of the image
img.mode 
# it returns 'rgb'

# create a grayscale image
grayimg = img.convert('L')
grayimg.mode
# it returns 'L' which means grayscale

Q: How to get the pixel of an image?

The command that we use is the “getpixel((x1,x2))”

# get the pixel of the top left corner of the RGB image
img.getpixel((0,0))
# we get (44, 110, 160) since it is an RGB we expected a tuple of 3-D taking values from 0-255

# get the pixel of the top left corner of the grayscale image
grayimg.getpixel((0,0))
# it returns 95 , as we expected we got only one element within the range of 0-255

Q: How to change a pixel of an image?

The command that we use it the “putpixel((x1,x2), value)”

# change the top left cornee pixel to white of the RBG Image
img.putpixel( (0,0), (0,0,0) )

# change the top left corner pixel to white of the grayscale image
grayimg.putpixel( (0,0), 0 )

Iterate over Image Pixels

Let’s give an example of Iterating over Image Pixels and based on a threshold to change the image color to white or black. For convenience our function will consider only grayscale images but you can easily extend it to RGA.

def binarize(image_to_transform, threshold):
    # now, lets convert that image to a single greyscale image using convert()
    output_image=image_to_transform.convert("L")
    # the threshold value is usually provided as a number between 0 and 255, which
    # is the number of bits in a byte.
    # the algorithm for the binarization is pretty simple, go through every pixel in the
    # image and, if it's greater than the threshold, turn it all the way up (255), and
    # if it's lower than the threshold, turn it all the way down (0).
    # so lets write this in code. First, we need to iterate over all of the pixels in the
    # image we want to work with
    for x in range(output_image.width):
        for y in range(output_image.height):
            # for the given pixel at w,h, lets check its value against the threshold
            if output_image.getpixel((x,y))< threshold: #note that the first parameter is actually a tuple object
                # lets set this to zero
                output_image.putpixel( (x,y), 0 )
            else:
                # otherwise lets set this to 255
                output_image.putpixel( (x,y), 255 )
    #now we just return the new image
    return output_image

binarize(img, 100)

For comparison we represent the Modified with a threshold of 100 and the Original!

Iterate over Image Pixels 1
Iterate over Image Pixels 2

We can change also random pixels with random colors. The function below takes as an input the percentage of the pixels that are going to be changed with another random color from the RGB spectrum.

def random_image(image_to_transform, threshold):
    # convert any image to RGB
    output_image=image_to_transform.convert("RGB")
    # The threshold is the percentage of random pixels to generate
    
    for x in range(output_image.width):
        for y in range(output_image.height):
            if float(np.random.rand(1))<threshold:
                output_image.putpixel( (x,y), tuple(np.random.choice(range(255), 3, replace=True)) )
    return output_image

random_image(img, 0.2)
Iterate over Image Pixels 3

Related Articles

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

7 thoughts on “Iterate over Image Pixels”

  1. Haѵe уou ever considered about adding a little bit more than just your articles?

    I mean, what you say iѕ impⲟrtant and еverything.
    Neᴠertheless juѕt imagine if you added some great graphіcs or videos to give yⲟur posts more, “pop”!
    Your content is excellent but with images and clips, this website cߋuld ceгtainly be one of
    the most beneficial in its field. Excellent blog!

    Reply

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

letter frequency
Python

Document Letter Frequency in Python

Letter Frequency We will provide you a walk-through example of how you can easily get the letter frequency in documents

[the_ad_group id="232"]
[the_ad id="2133"]