Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using NumPy With Pillow

00:00 Image Manipulation With NumPy and Pillow. Pillow has an extensive selection of built-in functions and filters, but there are times when you need to manipulate images with features that are beyond those available in Pillow.

00:14 You can manipulate images with the help of NumPy. NumPy is a popular Python library for dealing with numerical arrays, and it’s an ideal tool to use with Pillow.

00:26 You can learn more about it in this Real Python tutorial. When you convert an image into a NumPy array, you can perform any transformations that you require directly on the pixels in the array. Once you’ve completed your processing in NumPy, you can convert the array back to an Image object using Pillow.

00:47 But first, you’ll need to install NumPy.

00:57 Now that NumPy is installed, you are ready to use Pillow and NumPy to spot the difference between two images. Can you spot the differences between the two images seen on-screen? This particular spot-the-difference competition is not difficult, but you decide to cheat anyway and write a Python program to solve the puzzle for you.

01:18 Here you’ll be working with the images house_left and house_right from the course materials. The first step is to read the images using Pillow and convert them to NumPy arrays.

02:09 Since both left_array and right_array are objects of type numpy.ndarray, you can manipulate them using all the tools you have available in NumPy.

02:19 You can subtract one array from the other to show the pixels that differ between the two images. When

02:29 you subtract an array from another one of the same size, the result is another array with the same shape as the originals, you can convert this array into an image using Image.fromarray() in Pillow.

02:48 The result of the subtraction is seen on-screen. The difference image only shows three regions from the original. Those regions highlight the differences between the two.

03:00 You can also see some noise surrounding the cloud and the fence, which is due to small changes in the original JPEG compression in the region surrounding these changes. In the next section of the course, you’ll see another example of how to use NumPy with Pillow by using it to create images.

Become a Member to join the conversation.