An image looks pixelated when individual rectangles of colour become visible. These rectangles of perceived ‘blurriness’ can also appear when an image is resized or compressed (more on compression in a future step).
Pixelation is used purposefully to make an image (or parts of an image) unrecognisable. Think, for example, about news reports where people’s faces in CCTV footage or in pictures are pixelated to protect their identity.
You will use Python code to do just this: protect the identity of your favourite emoji.
To pixelate an image, individual pixels are selected at regular intervals, and their colour is retrieved and duplicated over a nearby range of pixels. The interval at which pixels are selected defines the level of pixelation: in the example here, the algorithm creates pixelation in 8 × 8 rectangles.
Again use the same starter code. Adapt the input and output file names as necessary, convert the image to RGB, and get the image width and height like you did for all the previous filters.:
from PIL import Image #Import the Image module from the PIL library
image = Image.open('image_in.bmp') #Create a path to the image file
rgb_image = image.convert('RGB') #Convert the image to RGB
width, height = image.size #Assign the image's width and height to variables
output_image = Image.new('RGB', (width,height)) #Create a grid for the output image
#Set up loops to modify each pixel within the image
for row in range(height):
for col in range(width):
#Change the image pixels here
output_image.save("image_out.png")
Set the parameter for pixelating your image in 8 × 8 pixel rectangles:
pixelAmount = 8
This is the algorithm for the pixelate filter:
Starting with the first pixel in the top left-hand corner, the colour of every eighth pixel is retrieved
For each of the selected pixels, an 8 × 8 area of pixels is assigned
A function receives the retrieved pixel colour values and fills the pixel’s assigned area with this colour, thereby essentially creating a larger pixel
This three-step process is repeated until the whole image is replaced with blocks of colour.
As in the scripts for the other filters, pixel manipulation occurs within a nested loop.
For previous filters, your loop looks like this, because those filters manipulate each pixel individually:
for row in range(height):
for col in range(width):
r, g, b = rgb_image.getpixel((col, row))
However, the pixelate filter manipulates pixels in 8 × 8 pixel areas.
That means the loop only needs the colour values of the pixel in every eighth column within every eighth row.
The Python command range() makes addressing these pixels easy, because range() takes three parameters, start, stop, and step. So you can iterate over pixels in 8 by 8 intervals using this command and the variable pixelAmount, which you set to `
for row in range(0, height - pixelAmount, pixelAmount):
for col in range(0, width - pixelAmount, pixelAmount):
r, g, b = rgb_im.getpixel((col, row))
The loops start from 0
The loops stop at the value of height and width (8 is subtracted to counteract the step increment, which is pixelAmount = 8)
For both loops, the step parameter is set to pixelAmount = 8, so that only every eighth pixel is addressed
In order to set the colour of each pixel of the 8 × 8 area, you need another nested loop within the nested loop you’ve just written:
for o in range(row, row + pixelAmount):
for p in range(col, col + pixelAmount):
All the way within this nested loop, assign the colour values for each pixel of the 8 × 8 area:
output_image.putpixel((p,o), (r,g,b))
This process of assigning colour values is repeated by the outer loops until all of the 8 × 8 areas are filled with their new colours.
Save and run your script. If everything is correct, the resulting image should be slightly less recognisable than the original. Your final image should look something like:
Extend your code by adding an input to allow the user to control the level of pixelation.
Build on what you’ve learned about filters to create one of the following filters:
Easy: black and white
Medium: greyscale
Challenging: an Instagram-style Clarendon filter — lights made lighter, darks made darker, and a blue tint
Share your code in the comments below — you may wish to use pastebin to preserve formatting.
Discuss with your fellow course participants how you created your new filter, and exchange ideas for how to implement these activities in the classroom.