Skip to content
Learnearn.uk » Python Unit Home » Python Image Editing Challenges

Python Image Editing Challenges

Example Code

Example Code

Here is some example code to help you get started!

from PIL import Image

im = Image.open('cat.jpg')
im2 = im.resize((400,300),Image.LANCZOS) #create a smaller copy
im2.save('resize.jpg')
for x in range(im2.width):
    for y in range(im2.height):
        r,g,b = im2.getpixel((x,y))
        r = r + 30
        g = g + 30
        b = b + 30
        
        im2.putpixel((x, y), (r, g, b))

im2.save('lighter.jpg')

Original

Original Image

Here is the original image I will be working with so that you can compare the results.

Challenge 1

Challenge 1 – Darker

Create a program that makes your image darker rather than lighter and saves the image as darker.jpg.

Result:

 

2

Challenge 2 – Grayscale

Create a program that converts the image to grayscale and saves it as ‘grayscale.jpg’

Result:

 

3

Challenge 3 – B&W

Create a program that converts the image to pure black and white and saves it as ‘bw.jpg’.

Result:

 

 

 

4

Challenge 4 – Invertor

Create a program that inverts all the colour channels in the image and saves it as ‘inverted.jpg’.

Result:

 

5

Challenge 5 – RGB Split

Create a program that splits the image into 3 different colour channels and saves a single colour copy of each image in ‘r.jpg’, ‘g.jpg’ and ‘b.jpg’

Result:

 

6

Challenge 6 – Increased Contrast

Create a program that increases the contrast in the image and saves it as ‘contrast.jpg’.

Result:

 

7

Challenge 7 – Rotated

Create a program that rotates the image by 180 degrees and saves it as ‘rotated.jpg’. You should not use the in-built functions for this.

Result:

 

8

Challenge 8 – Secret Message

Create a program that stores a secret message that the user inputs in the image using steganography and saves the image as ‘secret.jpg’.

Write another program that decrypts the secret message.