Working with Images in Python (original) (raw)

Last Updated : 4 Jun, 2024

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:

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:

from PIL import Image

**Operations with Images:

#img = Image.open(path)

On successful execution of this statement,

an object of Image type is returned and stored in img variable)

try:
img = Image.open(path)
except IOError:
pass

Use the above statement within try block, as it can

raise an IOError if file cannot be found,

or image cannot be opened.

`

#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.

format is optional, if no format is specified,

#it is determined from the filename extension
`

if name == "main":
main()
` rotating an image in python 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.

if name == "main":
main()
` cropping an image in python

if name == "main":
main()
` resizing an image in python

if name == "main":
main()
##An additional argument for an optional image mask image is also available.
` pasting an image on other in Python

if name == "main":
main()
`
getting-a-histogram-of-image-in-python-copy

if name == "main":
main()
` transposing an image in python

if name == "main":
main()
` split an image in python

if name == "main":
main()
` converting image to bitmap in pythonconverting image to bitmao in python

if name == "main":
main()
`

creating thumbnail of image in python