Basic Image Operations
00:00 Basic Image Operations With the Pillow Library. The Pillow library is a fork of an older library called PIL. PIL stands for Python Imaging Library, and it’s the original library that enabled Python to deal with images.
00:39 Other popular libraries for image processing are OpenCV, scikit-image, and Mahotas. Some of these libraries are faster and more powerful than Pillow. However, Pillow remains an important tool for dealing with images.
00:54 It provides image processing features that are similar to ones found in image processing software such as Photoshop and GIMP. Pillow is often the preferred option for high-level image processing tasks that don’t require more advanced image processing expertise.
01:08 It’s also often used for exploratory work when dealing with images. Pillow has the advantage of being widely used in a Python community, and it doesn’t have the steep learning curve as some other image processing libraries.
02:06 Now that you’ve installed the package, you are ready to start familiarizing yourself with the Pillow library and perform basic manipulations of images. For the code in this part of the course, you’ll be using the image seen on-screen, which you can find in the course materials.
Make sure you’ve placed this image file in the project folder that you are working in. The main class defined in Pillow is the
Image class. When you read an image using Pillow, the image is stored in an object of type
You might expect to import from
Pillow instead of from
PIL. You did install Pillow after all and not PIL, but Pillow is a fork of the PIL library. Therefore, you’ll still need to use PIL when importing into your code.
You use a
with statement to create a context manager to ensure the file is closed as soon as it’s no longer needed. In this example, the object is a JPEG image-specific type that’s a subclass of the
The format of an image shows what type of image you’re dealing with. In this case, the format of the image is
'JPEG'. The size shows the width and height of the image in pixels, and the mode of this image is
The argument to
.crop() must be a 4-tuple that defines the left, upper, right, and bottom edges of the region that you wish to crop. The coordinate system used in Pillow assigns the coordinates (0, 0) to the pixel in the upper-left corner.
You can also change the resolution of the cropped image using
.resize(), which needs a tuple as a required argument. The tuple that you use as an argument defines the new width and height of the image in pixels.
The size of the thumbnail will be smaller than or equal to the size that you set. Note that the
.thumbnail() method changes the
Image object in place and doesn’t return a new object, but
.reduce() all return a new
The extension that you use as a filename automatically determines the file format, or you can specify the format as an additional optional argument. Note that there is no confirmation given if you’re going to overwrite an existing file, so you need to be careful when using the
Become a Member to join the conversation.