Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Selected Reading

    Python Pillow - Adding Borders to an Image



    Adding borders to images is one of the common task in image processing. Borders can help to frame the content, draw attention to specific areas, or add a decorative element. In Pillow (PIL) the expand() method of the ImageOps module is used to increase the dimensions of an image by adding a border or padding around it. This can be helpful for various purposes such as adding a border, creating a canvas of a specific size or resizing the image to fit a particular aspect ratio.

    The expand() method

    The expand() method is useful for adding borders to images and adjusting their dimensions while maintaining the aspect ratio. We can customize the size and border color to suit our specific requirements.

    Here's the basic syntax for the expand() method −

    PIL.Image.expand(size, fill=None)
    

    Where,

    • size − A tuple specifying the new dimensions i.e. width and height for the expanded image.

    • fill (optional) − An optional color value to fill the border area. It should be specified as a color tuple (R, G, B) or an integer value representing the color.

    Following is the input image used in all the examples of this chapter.

    expanded image

    Example - Expanded Image with Border

    In this example we are using the expand() method to create the expanded image with the border.

    from PIL import Image, ImageOps
    
    #Open an image
    image = Image.open("Images/handwriting.jpg")
    
    #Define the new dimensions for the expanded image
    new_width = image.width + 40  
    #Add 40 pixels to the width
    new_height = image.height + 40  
    #Add 40 pixels to the height
    
    #Expand the image with a white border
    expanded_image = ImageOps.expand(image, border=20, fill="red")
    
    #Save or display the expanded image
    expanded_image.save("Output_Images/expanded_output.jpg")
    open_expand = Image.open("Output_Images/expanded_output.jpg")
    open_expand.show()
    

    Output

    image with red border

    Example - Expanding Image with Blue Border

    Here this is another example we are expanding the image border with blue color by using the expand() method of the Image module.

    main.py

    from PIL import Image, ImageOps
    
    #Open an image
    image = Image.open("Images/handwriting.jpg")
    
    #Define the new dimensions for the expanded image
    new_width = image.width + 40  
    #Add 40 pixels to the width
    new_height = image.height + 40  
    #Add 40 pixels to the height
    
    #Expand the image with a white border
    expanded_image = ImageOps.expand(image, border=100, fill="blue")
    
    #Save or display the expanded image
    expanded_image.save("Output_Images/expanded_output.jpg")
    open_expand = Image.open("Output_Images/expanded_output.jpg")
    open_expand.show()
    

    Output

    image with blue border
    Advertisements