Creating a Warhol Triptych Image with Pillow and Python

Andy Warhol is a well-known artist who created a famous image where there are multiple copies of the same face but with different background colors.

You can do a similar trick with software using Python and the Pillow package. You will also need to have NumPy installed to make this work.

Let’s find out how this all works!

Getting Started

The first thing you need to do is make sure you have Pillow and NumPy installed. If you use pip, you can try running the following command:

python3 -m pip install numpy Pillow

This will install NumPy and Pillow if you don’t already have them installed.

If you are running Anaconda instead, then both of these packages should already be installed.

Now you’re ready to create some art!

Creating a Triptych

Creating a Triptych with Python doesn’t take a lot of code. All you need is a little know-how and some experimentation. The first step is to learn why you need NumPy.

NumPy isn’t so much a replacement for Pillow as it is a way to enhance Pillow’s capabilities. You can use NumPy to do some of the things that Pillow does natively. For the examples in this section, you will use this photo of the author:

Michael Driscoll
Michael Driscoll

To get a feel for how you might use NumPy with Pillow, you will create a Python program that concatenates several images together. This will create your Triptych image! Open up your Python editor and create a new file named concatenating.py. Then enter this code in it:

# concatenating.py

import numpy as np
from PIL import Image


def concatenate(input_image_path, output_path):
    image = np.array(Image.open(input_image_path))

    red = image.copy()
    red[:, :, (1, 2)] = 0

    green = image.copy()
    green[:, :, (0, 2)] = 0

    blue = image.copy()
    blue[:, :, (0, 1)] = 0

    rgb = np.concatenate((red, green, blue), axis=1)
    output = Image.fromarray(rgb)
    output.save(output_path)

if __name__ == "__main__":
    concatenate("author.jpg", "stacked.jpg")

This code will open up the image using Pillow. However, rather than saving that image off as an Image object, you pass that object into a Numpy array(). Then you create three copies of the array and use some matrix math to zero out the other color channels. For example, for red, you zero out the green and blue channels, leaving the red channel alone.

When you do this, it will create three tinted versions of the original image. You will now have a red, green, and blue version of the photo. Then you use NumPy to concatenate the three images together into one.

To save this new image, you use Pillow’s Image.fromarray() method to transform the NumPy array back into a Pillow Image object.

After running this code, you will see the following result:

Python Triptych

That’s a neat effect!

NumPy can do other things that Pillow doesn’t do easily, like Binarization or denoising. You can do even more when you combine NumPy with other scientific Python packages, such as SciPy or Pandas.

Wrapping Up

Python is powerful. You can do many things with Python, Pillow and NumPy. You should try doing this with your own image(s) and see what you can come up with. For example, instead of lining the images up left-to-right, you could create four images and put them together in a square!

Related Reading