Tag Archives: images

PyDev of the Week: Adrian Rosebrock

This week we welcome Adrian Rosebrock (@PyImageSearch) as our PyDev of the Week. Adrian is the author of several books on Python and OpenCV. He is also the author of PyImageSearch.com, a very popular Python blog that focuses on computer vision. Let’s take some time to get to know him a bit better!

Can you tell us about yourself and PyImageSearch?

Hi Mike, thank you for the opportunity to be interviewed on PyDev of the Week.

My name is Adrian Rosebrock. I have Ph.D in computer science with a focus in computer vision and machine learning from the University of Maryland, Baltimore County (UMBC).

I blog over at PyImageSearch.com about Python, computer vision, and deep learning. Over the past few years running PyImageSearch I have written a handful of books/courses, including:

Continue reading PyDev of the Week: Adrian Rosebrock

Enhancing Photos with Python

Sometimes when you take a photo you will find that it isn’t quite what you wanted. The picture looks great, but it’s just a little too dark. Or it’s a little blurry and you need to add sharpness. The sharpness issue isn’t anywhere near as bad nowadays because a lot of cameras will add sharpness automatically for you after they take the photo.

Anyway, in this article, we will learn how to do the following:

  • How to adjust the brightness of a photo
  • How to change the contrast of your image
  • How to sharpen a photo

The first thing you will need is the Pillow package which you can install using pip:

pip install Pillow

Now that we have Pillow installed, we can get started! Continue reading Enhancing Photos with Python

How to Watermark Your Photos with Python

When you look up photos online, you will notice that some of them are watermarked. A watermark is usually some text or a logo overlaid on the photo that identifies who took the photo or who owns the rights to the photo. Some professionals recommend adding watermarks to your photos before sharing them on social media to prevent other people from using your photos as their own and profiting off your work. Of course, watermarking can be removed fairly easily, so this isn’t as useful as it used to be as a digital rights tool.

Anyway, the Pillow package provides the tools you need to add watermarks to your photos! The first thing you need to do is install Pillow if you haven’t already:

pip install pillow

Once that’s installed, we can continue! Continue reading How to Watermark Your Photos with Python

How to Resize a Photo with Python

Sometimes you will find yourself wanting to resize a photo. I usually want to do this for photos that I want to email or post on a website since some of my images can be quite large. Normal people use an image editor. I usually do as well, but for fun I thought I would look into how to do it with the Python programming language.

The quickest way to do this is to use the Pillow package which you can install with pip. Once you have it, open up your favorite code editor and try the following code:

from PIL import Image

def resize_image(input_image_path,
    original_image = Image.open(input_image_path)
    width, height = original_image.size
    print('The original image size is {wide} wide x {height} '
          'high'.format(wide=width, height=height))

    resized_image = original_image.resize(size)
    width, height = resized_image.size
    print('The resized image size is {wide} wide x {height} '
          'high'.format(wide=width, height=height))

if __name__ == '__main__':
                 size=(800, 400))

Continue reading How to Resize a Photo with Python

Convert a Photo to Black and White in Python

Black and white images aren’t for everyone. I personally like to play around with them as you can sometimes take a boring photo and turn it into something dramatic. I have also rescued a drab photo by turning it black and white. If you want to change a photo that you took into a black and white photo programmatically, the Pillow package has you covered. In this article we will look at the two simple ways to convert a photo to black and white and then we will also learn how to make a sepia-toned photo.

Making it Black and White

The first obstacle is finding a photo that you would like to edit. For this example, we will use the following fuzzy caterpillar: Continue reading Convert a Photo to Black and White in Python

How to Crop a Photo with Python

If you like taking photos than you will probably also find yourself cropping your photos from time to time. I will crop photos to get rid of background noise or to just focus more on the subject I was trying to capture. I also like to take high resolution photos of insects or other small creatures and then crop it down to make it seem like I was even closer to the insect than I really was.

Now most people will use a photo editing application to crop their image, such as Photoshop Elements. I use these kinds of tools too, but you can also use the Python programming language to do the cropping for you. One good example where you might want to use Python is if you have thousands of scanned images of the same type, then it makes more sense to just write a script to do the cropping for you.

The most popular package for image manipulation in Python is the Pillow package, a “Friendly fork of the Python Imaging Library (PIL)”. You can install Pillow using pip: Continue reading How to Crop a Photo with Python

How to Create a Diff of an Image in Python

For the past couple of years, I’ve been writing automated tests for my employer. One of the many types of tests that I do is comparing how an application draws. Does it draw the same way every single time? If not, then we have a serious problem. An easy way to check that it draws the same each time is to take a screenshot and then compare it to future versions of the same drawing when the application gets updated.

The Pillow library provides a handy tool for this sort of thing that is called ImageChops. If you don’t already have Pillow, you should go install it now so you can follow along with this short tutorial.

Comparing Two Images

The first thing we need to do is find two images that are slightly different. You can create your own by using burst mode on your camera and taking a bunch of photos of animals as they move, preferably while using a tripod. Or you can take an existing photo and just add some kind of overlay, such as text. I’m going to go with the latter method. Here is my original photo of Multnomah Falls in Oregon:


Continue reading How to Create a Diff of an Image in Python

An Intro to the Python Imaging Library / Pillow

The Python Imaging Library or PIL allowed you to do image processing in Python. The original author, Fredrik Lundh, wrote one of my favorite Python blogs when I first started learning Python. However PIL’s last release was way back in 2009 and the blog also stopped getting updated. Fortunately, there were some other Python folks that came along and forked PIL and called their project Pillow. The Pillow project is a drop-in replacement for PIL that also supports Python 3, something PIL never got around to doing.

Please note that you cannot have both PIL and Pillow installed at the same time. There are some warnings in their documentation that list some differences between PIL and Pillow that get updated from time to time, so I’m just going to direct you there instead of repeating them here since they will likely become out of date.

Install Pillow

You can install Pillow using pip or easy_install. Here’s an example using pip:

pip install Pillow

Note that if you are on Linux or Mac, you may need to run the command with sudo.

Opening Images


Pillow makes it easy to open an image file and display it. Let’s take a look:

from PIL import Image

image = Image.open('/path/to/photos/jelly.jpg')

Here we just import the Image module and ask it to open our file. If you go and read the source, you will see that on Unix, the open method saves the images to a temporary PPM file and opens it with the xv utility. On my Linux machine, it opened it with ImageMagick, for example. On Windows, it will save the image as a temporary BMP and open it in something like Paint.

Continue reading An Intro to the Python Imaging Library / Pillow