Image Utilities Examples
Examples on using functions from toolbox.im_utils
¶
this notebook offers some visual examples on how some of the functions in toolbox.im_utils
can be used. For the complete and extensive description on all the image utility functions, please check out the Reference Page
from toolbox.im_utils import *
im = get_im('https://picsum.photos/id/177/500/300')
person_bbox = {'x0': 227, 'y0': 152, 'x1': 284, 'y1': 298}
Basic Image Processing Functions¶
We can get images of different size
get_pil_im(im)
get_pil_im(im_max_long_edge(im, 300))
get_pil_im(im_min_short_edge(im, 700))
Cropping an Image¶
im_person = get_pil_im(im_crop(im, **person_bbox))
print(im_person.size); im_person
(57, 146)
Padding an Image¶
get_pil_im(
im_centre_pad(im_person, (150,150))
)
Concatenating two images in different flavours¶
Vertically...
get_pil_im(
image_stack('https://picsum.photos/id/20/400/300', 'https://picsum.photos/id/26/400/350',
do_vstack = True, black_line_thickness= 2)
)
Horizontally...
get_pil_im(
image_stack('https://picsum.photos/id/20/200/300', 'https://picsum.photos/id/26/400/300',
do_vstack = False, black_line_thickness= 2)
)
or splitting according to a percentage...
get_pil_im(
image_stack('https://picsum.photos/id/20/400/300', 'https://picsum.photos/id/26/400/300',
split_pct = 0.5,
do_vstack = False, black_line_thickness= 2)
)
get_pil_im(im_write_text(im, "Hello World!", x = 200, y = 10, tup_font_rgb = (250,0,0)))
Drawing Bounding Box¶
you can do so simply using PIL
and can even get a label for your bounding box!
get_pil_im(
im_draw_bbox(im, **person_bbox, color = 'white', width = 1, caption = 'person')
)
or you can use the bbox_visualizer
option which draws more visually pleasing bounding boxes
get_pil_im(
im_draw_bbox(im, **person_bbox, color = 'white', width = 1, caption = 'person', use_bbv = True)
)
Masking a bounding box¶
you can mask out the area outside the bounding box with your color of choice
get_pil_im(
im_mask_bbox(im, l_bboxes = [person_bbox], mask_rgb_tup= (250,0,0))
)
or you can mask out the bounding box itself... which could be useful in a privacy setting!
get_pil_im(
im_mask_bbox(im, l_bboxes = [person_bbox], b_mask_bg = False)
)
Mask-related Functions¶
import numpy as np
person_mask = np.zeros(im.shape[:2], dtype = np.uint8)
person_mask[152:298 ,227:284] = 1
you can apply an color to a mask...
get_pil_im(
im_color_mask(im, mask_array = person_mask, alpha = .5)
)
to color the inverse of the mask you can do something like this...
get_pil_im(
im_color_mask(im, mask_array = np.logical_not(person_mask).astype('uint8'), alpha = 0.7)
)
to do background removal given a mask...
get_pil_im(
im_apply_mask(im, mask_array = person_mask, bg_rgb_tup = [50,205,50])
)
alternatively you can blur out the background
get_pil_im(
im_apply_mask(im, mask_array = person_mask, bg_blur_radius = 5)
)
or greyscale it out
get_pil_im(
im_apply_mask(im, mask_array = person_mask, bg_greyscale = True)
)
and if your mask's edges are too shape, you can soften it too!
get_pil_im(
im_apply_mask(im, mask_array = person_mask, mask_gblur_radius= 5, bg_greyscale = True)
)