PIL
libraryPIL
or Python imaging library is an image processing library in Python. This library supports a wide range of file formats, is designed for an efficient internal representation of image data, and provides powerful image processing capabilities.
ImageOps
moduleThe ImageOps
module in PIL
contains ‘ready-made’ image processing operations and most operations only work on L (grayscale or single channel image) and RGB images.
expand
functionThe expand
function is used to add a border to the image.
PIL.ImageOps.expand(image, border=0, fill=0)
image
: It is the image to expand or add the border to.border
: It is the width of the border in pixels.fill
: It is the border color.Let’s look at the code below:
from PIL import Image, ImageOpsimg = Image.open('tree.png')img_with_border = ImageOps.expand(img, border=50, fill='red')img_with_border.save('output/tree-with-border.png')
ImageFilter
and Image
modules.man.png
into memory using open()
in Image
module.expand()
method and add a red color (fill) border of size 30
to the image.