In this video, we will discuss processing images.
One of the more powerful libraries for working with images in
Python is Pillow, it has an extensive catalog of very useful
functionality that covers everything from creating new images to
manipulating existing ones.
We're going to use it now to demonstrate how to use Pillow to convert from one
image format to another, resize the image, and extract useful metadata. Pillow
is installed using the PIP 3 Package Manager.
Converting from one file format to another takes a few quick lines of code.
First,
we load the file into a Pillow object and then we save it using the
file extension that we want to convert it to.
It really is that simple. One of the use cases for image resizing is to
make smaller images for list pages and thumbnails.
Resizing an image is as simple as the conversion process.
In this case,
we again load the image and then we take the existing width and height
of the image, divide these by two to have the image, and then call the
resize function using the halved parameters.
Finally, we again call the save function.
If we examine the image, we can now see it's been reduced in size.
The last thing we're going to look at with images is EXIF
data. When a photo is taken by a camera,
information can be stored along with the photo image as metadata or EXIF data.
This can contain information about the photograph itself,
such as the camera settings used to produce the image or it
can contain other information such as copyright or the
geolocation that the image was taken at.
EXIF data is not only restricted to images, but can also be seen
in video and audio files depending on the device that's used to
create the media in the first place.
I've created an image with a fictitious name of the image creator that
we can extract. EXIF data is available to us using the getexif method
and is presented in key value format.
If we print it out, we can see the name of the image
creator that I inserted beforehand.