hello serdar yagudapira for infoworld at idg in this episode of smart python we're going to look
at the basics of the pillow library a commonly used library for image processing in python
a pillow is derived from an earlier library named pil sometimes pronounced pill which is
short for the python image library but you don't need to know anything about pill to use pillow
in the examples i'm going to show will be importing pillow as pil
and pillow works that way to be backwards compatible with existing scripts that use
pill but again you don't need to know anything about the predecessor
with pillow you can work with existing images in common formats or you can create entirely
new images programmatically for the sake of this demo i'm going to focus on manipulating existing
images since that's a more common scenario and a little bit more straightforward but in
a future installment i'll show some examples of how to create new images entirely from scratch
so let's start with a simple task we're going to load an existing image and we're going to display
it one nice thing about pillow is that we don't have to know any of the technical details about
any image format it supports encoding and decoding are all handled by pillow behind the scenes
to work with an image we use the image class and we can use image.open to read a file from disk
and again common formats like jpeg are supported natively and we store the results
in a python object as a bitmap the object's properties like format and size give us useful
information about it mode tells us what the color information in the file is in this case it's rgb
the show method invokes whatever program is registered with the system as the default
image handler to display the image and this computer that program is our fan view
the results may vary on your machine i should note that the way that it does this is by taking
the raw image data in the object writing it out to a temporary file in the lossless png format
and then invoking the imageviewer to show that this will become important a little bit later
for now though let's try to do something actually useful with this image let's take this image
let's load it into memory and let's resize it to make it more manageable to do this we use the
thumbnail method which resizes an image in place and the way i'm invoking it here it's set to be
800 pixels on its longest side and the results will then be re-saved back out to disk as a jpeg
the original image we're working with by the way is huge it's several thousand pixels on the side
that's nice if we're working with print but it's really unwieldy if we want to put this in
a web page once it's resized and saved though you can see from the image properties here
that it is a good deal smaller in both its image and file size
by the way i should note that for this script we're using the os dot start file method to show
the image what this does is it takes in a file name and then uses the operating system's default
handler for that file extension so this way we can actually display the file that we wrote out
to disk we're not just displaying a temporary copy of the in-memory data that we created
because that may not be an actual reflection of the file that we wrote out
another common operation that we can perform is compositing images
in this example i'm going to open two images one of them is the droplets image that we were working
on before and another is a color gradient background and then we resize both of them
make one smaller than the other and use the paste function to add the smaller one to the larger one
at specific coordinates this math here by the way is how we're centering one image inside the other
pillow does not have a native way to do this but we can do it ourselves without too much work we
just need to know the width and height of each image and we can get that from its properties
also you'll notice that when we save out the composite file to a jpeg we're passing a whole
lot more arguments to the save function the jpeg output module in pillow in fact all of
the image output modules in pillow have knobs that we can tweak control the image quality
in the case of jpeg we can control whether or not we want a progressively rendered image which
is useful for slow downloading or whether we want to perform extra optimizations on the saved image
data the cost of the save process taking a little longer so when we run this we start off with our
gradient which is gigantic and then we have our final composite downsized and saved as a jpeg
pillow also lets you perform detailed operations on the components of an image
namely its color channels in this example here i'm going to load in that droplets image and
i'm going to split it into its red green and blue color components with the split function
we can then re-merge the color channels and we're going to swap red for green green for
blue and blue for red first time we run this first thing we're going to see is the original image
then we're going to see the green color channel which is black and white because the color channel
itself has no color information it's just all the data that goes into the green channel of the image
so there's no way to present it except as a black and white image and then finally we have
the merged result which looks a little like we've been messing with the color controls on our tv
one more thing pillow can do is add text to an image
to do this we use two more modules from pillow the image font module and the image draw module
image draw sets up interfaces for a given image for us to draw to
an image font lets us set up a font at a given size to be used in the drawing process
so with this example we're going to draw two pieces of text one up in the corner of the image
and a second one centered and again to center the text we need to get information about its size
and we can do that with the get size method for the selected font and the text that we're using
once we do that we can calculate its position and draw it there you'll also notice when we
run this that our text options are not just limited to size but also to color the inner
color the stroke color the stroke width and so on and so forth this is not a full substitute for
say photoshop but it is a convenient way to add text to an image programmatically
now everything that i've shown here barely scratches the surface many other things are
possible with pillow i mentioned before generating entire images programmatically and i'll be taking
a deeper dive into that in a future video but what we have here should give you an idea of
why pillow is immediately useful it gives you a high level programmatic way to work with images
in python and lets you perform common complex operations on them without having to know anything
about the details of the image formats themselves so that's it for this video if you like it please
leave a comment below don't forget to subscribe to the idg tech talk channel on youtube and for
more smart python and more dev with serdar be sure to join me on facebook youtube and infoworld.com