In this step, you’ll put some of the theory about transforming images into practice by creating a negative version of a photo of your choice.
Negatives are inversions of images where light areas appear dark and vice versa. A negative colour image has opposite colour values and is colour-reversed: the areas that are red in the original image are cyan in the negative, green areas are magenta, blues are yellow, and vice versa. So to create a negative, all the original colours need to be swapped for their opposing colours on the colour wheel.
Here’s an example of an image with the right hand side replaced with its negative:
You will use Python to code a negative filter that turns bitmap images into negatives.
To converting a bitmap color image into its negative, all you need to do is repeat three steps for each pixel in the image:
Get the RGB value of the pixel
Calculate the new RGB value
Save the new RGB value for the pixel
Let’s look at the second step in more detail.
You already know from Week 1 that for RGB images, colour values go up to 255, where rgb(255,255,255) is white and rgb(0,0,0) is black. To get the opposite, or negative, value of a colour, you need to find the values of R, G, and B that oppose the colour’s RGB values on each of the three value ranges. And to do this, you can simply subtract the colours’ R, G, and B values from the maximum in the range, 255.
negative R = 255 – R, negative G = 255 – G, negative B = 255 – B
Example:
You have a pixel with the colour rgb(150,100,200).
To convert the colour of this pixel into its negative, subtract the values of R, G, and B from 255.
negative R = 255 - 150 = 105, negative G = 255 - 100 = 155, negative B = 255 - 200 = 55
So the negative of the colour is rgb(105,155,55).
As in previous steps, you’ll use the PIL Python library again to write a script that manipulates image files. I’ll show you how to apply the three-step algorithm in order to create negative bitmap images.
Open a new Python file in whatever IDE you are using, and import the PIL library:
from PIL import Image
Choose an image that you want to convert into a negative. I’ve chosen this image, but you can choose any image you like.
Now write code to open the image and convert it into RGB.
im = Image.open('image.png')
rgb_im = im.convert('RGB')
Get the size of the image, and save it as the width in number of pixels, and the height in number of pixels. You need these numbers so that you can address each pixel individually.
width, height = im.size
Now address each pixel. As in the Python scripts you’ve written for previous steps, you can do this with a nested loop.
for row in range(height):
for col in range(width):
Inside the loop, convert each pixel into its negative colour and display the new colour. Make sure your code follows step 2 of the algorithm above.
r, g, b = rgb_im.getpixel((col, row))
out.putpixel((col, row), (255 - r, 255 - g, 255 - b))
Check that your code is in the right place: these lines of code need to be within the nested loop!
Save the output pixels as an image. You need two new lines of code for this. The first one is:
out = Image.new('RGB', (width, height))
This line defines the output image of pixels with changed RGB values. This new line of code needs to be above the nested loop, because you refer to the output image in the nested loop using the out.putpixel command; your script needs to define out before it can successfully run the out.putpixel command.
The second line of code you need to save your image negative is:
out.save("negative.png")
This line simply saves the out image as a file called negative.png, so put this below your nested loop.
You should get an image similar to this:
Share your results and any problems you encounter in the comments.
And to build context for this activity in the classroom, discuss the following questions with your fellow participants:
Can you think of any scenarios where you have seen negative images or videos?
Why would you want to create a digital image negative? What could you use it for?