core

Some utility functions for working with PIL Images

source

get_img_files

 get_img_files (img_dir:pathlib.Path, img_fmts=['jpg', 'jpeg', 'png'])

Get all the image files in the given directory.

Returns: img_paths (list): A list of pathlib.Path objects representing the image files

Type Default Details
img_dir Path The directory to search for image files
img_fmts list [‘jpg’, ‘jpeg’, ‘png’] The list of image formats to search for

Set the path for the images directory

img_dir = Path('../images/')
img_dir
Path('../images')

Get a list of image file paths

img_paths = get_img_files(img_dir)
img_paths
[Path('../images/cat.jpg'), Path('../images/depth-cat.png')]

source

resize_img

 resize_img (img:<module'PIL.Image'from'/opt/hostedtoolcache/Python/3.9.18
             /x64/lib/python3.9/site-packages/PIL/Image.py'>,
             target_sz:int=512, divisor:int=32)

Resize the image to the target size, keeping aspect ratio and crop the image if the size is not divisible by divisor.

Returns: img (PIL.Image): The resized and possibly cropped image

Type Default Details
img Image The image to be resized
target_sz int 512 The target size of the image
divisor int 32 The divisor value to crop the image

Open sample image

img_path = img_paths[0]
src_img = Image.open(img_path).convert('RGB')
print(f"Image Size: {src_img.size}")
src_img
Image Size: (768, 512)

Resize image

resized_img = resize_img(src_img, target_sz=384, divisor=32)
print(f"New Image Size: {resized_img.size}")
resized_img
New Image Size: (576, 384)


source

stack_imgs

 stack_imgs (imgs:[<module'PIL.Image'from'/opt/hostedtoolcache/Python/3.9.
             18/x64/lib/python3.9/site-packages/PIL/Image.py'>])

Stacks a list of images horizontally or vertically, depending on which dimension is larger.

Returns: stack (PIL.Image): A single image containing all the input images stacked horizontally or vertically.

Type Details
imgs [‘Image’] A list of PIL.Image objects.

Stack images

stacked_imgs = stack_imgs([resized_img, resized_img])
print(f"Stacked Image Size: {stacked_imgs.size}")
stacked_imgs
Stacked Image Size: (576, 768)


source

img_save_path

 img_save_path (image, save_dir, suffix='', hashlen=8, fmt='PNG')

Generate the file path to save the image.

Returns: path (pathlib.Path): The file path to save the image

Type Default Details
image The image to be saved
save_dir The directory where the image will be saved
suffix str A suffix to add to the file name
hashlen int 8 The length of the image hash to use in the file name
fmt str PNG The format of the image
img_save_path(src_img, save_dir="./", suffix='cat')
Path('3dec29fe-cat.png')

source

avg_images

 avg_images (img_1:<module'PIL.Image'from'/opt/hostedtoolcache/Python/3.9.
             18/x64/lib/python3.9/site-packages/PIL/Image.py'>, img_2:<mod
             ule'PIL.Image'from'/opt/hostedtoolcache/Python/3.9.18/x64/lib
             /python3.9/site-packages/PIL/Image.py'>, weight:float=0.5)

This function takes two input images and a weight as input and returns the average of two images.

Type Default Details
img_1 PIL.Image First image
img_2 PIL.Image Second image
weight float 0.5 Weightage given to the first image while averaging
img_1, img_2 = (Image.open(path) for path in img_paths)
avg_images(img_1.convert('L'), img_2, 0.05)


source

crop_square

 crop_square (img:<module'PIL.Image'from'/opt/hostedtoolcache/Python/3.9.1
              8/x64/lib/python3.9/site-packages/PIL/Image.py'>)

This function takes a PIL.Image as input and returns a center square cropped version of the image.

Type Details
img Image The PIL Imag to crop
crop_square(src_img)