Category Archives: Image Editing

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,
                 output_image_path,
                 size):
    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))
    resized_image.show()
    resized_image.save(output_image_path)
 
if __name__ == '__main__':
    resize_image(input_image_path='caterpillar.jpg',
                 output_image_path='caterpillar_small.jpg',
                 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