PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. It was developed by Fredrik Lundh and several other contributors. Pillow is the friendly PIL fork and an easy to use library developed by Alex Clark and other contributors. We’ll be working with Pillow.
Installation:
![Open Open](https://www.pyimagesearch.com/wp-content/uploads/2014/06/compare_original_contrast.jpg)
Before you can develop predictive models for image data, you must learn how to load and manipulate images and photographs. The most popular and de facto standard library in Python for loading and working with image data is Pillow. Pillow is an updated version of the Python Image Library, or PIL,. Pillow tutorial shows how to use Pillow in Python to work with images. Pillow is a Python Imaging Library (PIL), which adds support for opening, manipulating, and saving images.
- Linux: On linux terminal type the following:
Installing pip via terminal:
- Windows:Download the appropriate Pillow package according to your python version. Make sure to download according to the python version you have.
We’ll be working with the Image Module here which provides a class of the same name and provides a lot of functions to work on our images.To import the Image module, our code should begin with the following line:
Operations with Images:
- Open a particular image from a path:
# On successful execution of this statement,
# an object of Image type is returned and stored in img variable)
try
:
except
IOError:
# Use the above statement within try block, as it can
# or image cannot be opened.
- Retrieve size of image: The instances of Image class that are created have many attributes, one of its useful attribute is size.
with Image.
open
(filename) as image:
#Image.size gives a 2-tuple and the width, height can be obtained
Some other attributes are: Image.width, Image.height, Image.format, Image.info etc.
- Save changes in image: To save any changes that you have made to the image file, we need to give path as well as image format.
# format is optional, if no format is specified,
- Rotating an Image: The image rotation needs angle as parameter to get the image rotated.
try
:
img
=
Image.
open
(
'picture.jpg'
)
#Angle given
img.save(
'rotated_picture.jpg'
)
pass
if
__name__
=
=
'__main__'
:
Note: There is an optional expand flag available as one of the argument of the rotate method, which if set true, expands the output image to make it large enough to hold the full rotated image.
As seen in the above code snippet, I have used a relative path where my image is located in the same directory as my python code file, an absolute path can be used as well. - Cropping an Image: Image.crop(box) takes a 4-tuple (left, upper, right, lower) pixel coordinate, and returns a rectangular region from the used image.
try
:
img
=
Image.
open
(
'picture.jpg'
)
img
=
img.crop(area)
#Saved in the same relative location
pass
if
__name__
=
=
'__main__'
:
- Resizing an Image: Image.resize(size)- Here size is provided as a 2-tuple width and height.
try
:
img
=
Image.
open
(
'picture.jpg'
)
img.save(
'resized_picture.jpg'
)
pass
if
__name__
=
=
'__main__'
:
- Pasting an image on another image: The second argument can be a 2-tuple (specifying the top left corner), or a 4-tuple (left, upper, right, lower) – in this case the size of pasted image must match the size of this box region, or None which is equivalent to (0, 0).
try
:
#Image on which we want to paste
#Image which we want to paste
img.paste(img2, (
50
,
50
))
#Saved in the same relative location
pass
if
__name__
=
=
'__main__'
:
##An additional argument for an optional image mask image is also available.
- Getting a Histogram of an Image: This will return a histogram of the image as a list of pixel counts, one for each pixel in the image. (A histogram of an image is a graphical representation of the tonal distribution in a digital image. It contains what all the brightness values contained in an image are. It plots the number of pixels for each brightness value. It helps in doing the exposure settings.)
from PIL import Imagetry
:
img
=
Image.
open
(
'picture.jpg'
)
#Getting histogram of image
pass
if
__name__
=
=
'__main__'
:
- Transposing an Image: This feature gives us the mirror image of an image
try
:
img
=
Image.
open
(
'picture.jpg'
)
#transposing image
transposed_img
=
img.transpose(Image.FLIP_LEFT_RIGHT)
#Save transposed image
except
IOError:
main()
- Split an image into individual bands: Splitting an image in RGB mode, creates three new images each containing a copy of the original individual bands.
try
:
img
=
Image.
open
(
'picture.jpg'
)
#splitting the image
except
IOError:
main()
- tobitmap: Converting an image to an X11 bitmap (A plain text binary image format). It returns a string containing an X11 bitmap, it can only be used for mode “1” images, i.e. 1 bit pixel black and white images.
from PIL import Imagetry
:
img
=
Image.
open
(
'picture.jpg'
)
print
img.tobitmap()
print
type
(img.tobitmap())
pass
if
__name__
=
=
'__main__'
:
- Creating a thumbnail: This method creates a thumbnail of the image that is opened. It does not return a new image object, it makes in-place modification to the currently opened image object itself. If you do not want to change the original image object, create a copy and then apply this method. This method also evaluates the appropriate to maintain the aspect ratio of the image according to the size passed.
from PIL import Imagetry
:
img
=
Image.
open
(
'picture.jpg'
)
#In-place modification
except
IOError:
main()
This article is contributed by Mohit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.