Plaid Dog Silhouette Example

In this example, we’ll paste a colored silhouette of an animal shape onto a background to produce something neat like this:

This has potential as a music album cover.

Usually I do cats, but let’s switch it up and feature a dog.

The plaid pattern camouflages the dog, making him a deadly hunter.

We begin with a picture of a dog.

You’ll need to make this image into an “RGBA” image with transparent background. That means you should choose a dog picture where it’s easy to pick out the dog from the background.

from PIL import Image
from rembg import remove
dog = remove(Image.open('dog.png'))

Read a bit more about this process here:
Removing an Image Background

You could go a step further and make this into an “L” mode image.

# only works for "RGBA" dog image
# with a transparent background
silhouette = dog.getchannel("A")
silhouette.save("dog_silhouette.jpg")

The getchannel("A") function is retrieving the “A” channel (“alpha” or opacity) from the original “RGBA” four channel image. The output is a one channel grayscale image whose mode is “L.”

When used as a mask, this image is like a piece of cardboard with a dog shape cut out of the middle. If you lay that cardboard mask over a rain forest painting and spray it with blue paint, you’ll get a blue dog shape on your rain forest painting.

In fact, you can use either the “RGBA” dog or the “L” dog as a mask, and the effect will be the same. Using the “RGBA” saves a line of code.

But for many people, I suspect that the “L” silhouette makes it easier to see what’s happening.

Now we need a plaid pattern. We will paste it onto the rain forest.

The size of your plaid pattern must match the size of the mask.
If you don’t resize the image(s), you get this:

ValueError: images do not match

The background should be at least as big as the dog.
I suggest resizing the plaid pattern to match the dog.

rainforest = rainforest.resize((400, 300))
silhouette = silhouette.resize((350, 240))
plaid = plaid.resize(silhouette.size)
rainforest.paste(plaid, (25, 30), mask=silhouette)
rainforest.save('plaid_nature_dog.png')

Above you can see that rainforest and silhouette (the dog) are sized such that the background will be a little bigger than the dog.

We match the plaid picture’s size to the dog silhouette using silhouette.size.

(You might actually prefer to crop the plaid image instead. That would prevent the pattern from getting squished vertically while it’s forced to match the width to height ratio of the dog image.)

  • In the paste command, plaid contains the “RGB” color values that will be pasted onto rainforest.
  • We paste plaid at x,y coordinates (25, 30) to center it on the background (do the math).
  • Don’t forget to set the mask as silhouette, or you will get this:

Oops! You forgot to lay down the cardboard cutout with the dog shape before you started painting. That shape is your mask. That’s an easy fix if you understand how this works.

Bonus:

You can use a black and white image to make your mask. You can search the web for “dog silhouette” and find something like this:

This can work, but we have to fix two problems:

1) It’s an “RGB” image (not usable as a mask). It needs to be converted to “L” mode.

2) We need the dog shape to be the white part, not the black part.

Both are easy to fix:

from PIL import Image, ImageOps
dog = Image.open('dog_silhouette.jpg')
dog = dog.convert("L")
dog = ImageOps.invert(dog)
dog.save('dog_mask.png')

Now the image looks like this:

We imported the ImageOps module and used the invert(img) function to return the negative of the original dog image.

We can also invert the image manually without breaking a sweat:

from PIL import Image
dog = Image.open('bwdog.jpg')
dog = dog.convert("L")
pix = dog.load()
for y in range(dog.height):
    for x in range(dog.width):
        pix[x,y] = 255 - pix[x,y]
dog.save('dog_mask.png')

Understanding how to do that by hand unlocks the ability to change a lot of other things about this image. For example, one could modify this code to produce a ghostly mask to paste a translucent dog shape.

I hope you have some fun creating neat things by pasting images through masks!