Working with the Image Data Type in Python Pillow


An effective library for working with picture data in Python is called Python Pillow. Users may open, edit, and save photographs in a variety of formats. You can open a picture in Pillow, access its characteristics like size and format, and then either show or save the altered version. You may change the size, rotate the image, trim it, add filters, and enhance the colours. Pillow is adaptable for image processing applications since it supports a broad variety of picture formats. Pillow offers a user-friendly interface and thorough documentation to meet all of your image processing requirements, whether they include straightforward picture tweaks or intricate transformations.

Installation Command

You must install the Python library Pillow on your computer system before using it. Using Python's package manager, pip, run the following command to do this:

pip install Pillow

Features of Pillow library

  • Image Opening and Saving: Pillow can save photos to many formats and open images in a variety of formats, including JPEG, PNG, GIF, and BMP.

  • Image Manipulation:It offers a variety of picture modification features, including cropping, flipping, rotating, resizing, and changing the colour mode.

  • Image Filters: A broad variety of picture filters, including edge enhancement, blur, and sharpen, are supported by Pillow.

  • Image Enhancements: It enables improving visual qualities including sharpness, contrast, colour balance, and brightness.

  • Image Drawing: Drawing on photos, adding text, lines, shapes, and watermarks are all supported by Pillow.

  • Image Metadata: Like Exif data, picture metadata is accessible and editable.

  • Image Formats: A wide range of image formats are supported by Pillow, ensuring interoperability with different file types.

  • Image Data Analysis: Pillow gives users access to an image's pixel data so they may analyse or modify it.

  • Image Transforms: Pillow supports affine transformations, perspective transformations, and other geometric transformations.

  • Colour Operations: It provides manipulation and conversion of colour spaces.

  • Image Sequences: Pillow may use animated pictures as image sequences, such as GIFs.

Basic Approach 

Install Pillow: Ensure that your Python environment has Pillow installed. Install it using the following code if you haven't already:

pip install Pillow

Import Pillow: Import the modules required to deal with photos from Pillow.

from PIL import Image, ImageFilter, ImageEnhance, ImageDraw

Open an Image: To open an image file, use the Image.open() function.

image = Image.open('path/to/image.jpg')

Display the Image (Optional): To display the opened image, use the show() function.

image.show()

Access Image features: You have access to the image's many features, including its size and format.

width, height = image.size
image_format = image.format

Perform Image Manipulations: Use Pillow's many techniques to alter the picture to do image manipulations. As an illustration, consider picture scaling, rotation, filtering, and colour enhancement.

new_width, new_height = 200, 150
resized_image = image.resize((new_width, new_height))

angle = 45
rotated_image = image.rotate(angle)

blurred_image = image.filter(ImageFilter.BLUR)

enhancer = ImageEnhance.Color(image)
enhanced_image = enhancer.enhance(1.5)

Save the picture: After editing the picture, use the save() function to save it to a new file.

resized_image.save('path/to/resized_image.jpg')

Image Drawing (Optional): Use Pillow's ImageDraw module to add drawings to the picture.

draw = ImageDraw.Draw(image)
draw.text((50, 50), "Hello, Pillow!", fill=(255, 0, 0))

Shut the Image (Optional): After processing, it's a good idea to shut the image.

resized_image.save('path/to/resized_image.jpg')

Attributes of an Image object

  • size() method

  • mode() method

  • format() method

  • filename() method

  • width() method

  • height() method

size() method

Algorithm

  • Receive the Image object as input.

  • Use the ‘size’ attribute of the Image object to get the width and height of the image in pixels.

  • Return the width and height as a tuple.

Example

from PIL import Image

image985 = Image.open('path/to/imageToUpload.jpg')

width, height = get_image_size(image985)

print(f"Image width: {width} pixels")
print(f"Image height: {height} pixels")

Output

Image width: 800 pixels
Image height: 600 pixels

mode() method

Algorithm

  • Receive the Image object as input.

  • Use the ‘mode’ attribute of the Image object to get the colour mode of the image.

  • Return the colour mode as a string.

Example

from PIL import Image

image678 = Image.open('path/to/imageToUpload.jpg')

color_mode = get_image_mode(image678)
print(f"Image color mode: {color_mode}")

Output

Image color mode: RGB

format() method

Algorithm

  • Receive the Image object as input.

  • Use the `format’ attribute of the Image object to get the format of the image.

  • Return the format as a string.

Example

from PIL import Image

image66 = Image.open('path/to/imageToUpload.jpg')

imageFormat = get_image_format(image66)

print(f"Image format: {imageFormat}")

Output

Image format: JPEG

filename() method

Algorithm

  • Receive the Image object and the filename as input.

  • Set a custom attribute 'filename' on the Image object and store the filename.

  • Return the Image object with the custom attribute.

Example

image22 = open_image_with_filename('path/to/imageToUpload.jpg')

if hasattr(image22, 'filename'):
   print(f"Image filename: {image22.filename}")
else:
   print("Filename attribute not available for this image.")

Output

Image filename: path/to/image.jpg

width() method

Algorithm

  • Receive the Image object as input.

  • Use the 'width' attribute of the Image object to get the width of the image in pixels.

  • Return the width as an integer.

Example

from PIL import Image

image121 = Image.open('path/to/imageToUpload.jpg')

width = get_image_width(image121)

print(f"Image width: {width} pixels")

Output

Image width: 800 pixels

height() method

Algorithm

  • Receive the Image object as input.

  • Use the 'height' attribute of the Image object to get the height of the image in pixels.

  • Return the height as an integer.

Example

from PIL import Image

image2 = Image.open('path/to/imageToUpload.jpg')

height = get_image_height(image2)

print(f"Image height: {height} pixels")

Output

Image height: 600 pixels

Conclusion

In conclusion, utilising the Pillow module to work with image data in Python offers a flexible approach to picture processing and modification. Pillow has a broad range of features, including the ability to conduct complicated operations like resizing, rotating, filtering, and colour enhancing in addition to opening and storing photos in a variety of formats. Custom image alterations and computer vision tasks are made possible by the capacity to obtain pixel data and implement transformations. The support for metadata and dynamic images also makes it feasible to manage a variety of visual data types. Because of how easy it is to use and the breadth of its documentation, Pillow is a well-liked choice for developers using Python to work with images. Pillow's robust feature set equips users to manage picture data effectively and creatively, whether it's a straightforward resize operation or sophisticated image analysis.

Updated on: 03-Aug-2023

544 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements