Activity 7: Change the background of image
In this section, we will start to learn how to change the simple background of your image using pixels.
Example - Change background color
Let us change the background color of Nuvi to pink.
from PIL import Image # Open the Nuvi image img = Image.open("nuevo.png") width = img.size height = img.size # Set up a new image with width and height newimg = Image.new('RGB',(width,height)) # Set the pixels for the new image colorpink = (244,114,208) coloryellow = img.getpixel((0,0)) for i in range(width): for j in range(height): color = img.getpixel((i,j)) # Get the color from original image if color == coloryellow: # If it is yellow, then: newimg.putpixel((i,j),colorpink) # Change the color to pink else: newimg.putpixel((i,j),color) # Else the color stays the same newimg.save("nuevopink.png")
Wow! This is our new Nuvi after changing the background.
Challenge - Change background based on your choice
It’s now your turn to change whichever color you want on Nuvi!
In order to see your image, please click on top left corner (which says ‘Files’), and then click on the image file to see the result.