Image Tool

An utility model for image augmentation.

Example usage:

from singa import image_tool

tool = image_tool.ImageTool()
imgs = tool.load('input.png').        resize_by_list([112]).crop5((96, 96), 5).enhance().flip().get()
for idx, img in enumerate(imgs):
    img.save('%d.png' % idx)
class singa.image_tool.ImageTool

A tool for image augmentation.

For operations with inplace=True, the returned value is the ImageTool instance self, which is for chaining multiple operations; Otherwise, the preprocessed images would be returned.

For operations that has countable pre-processing cases, argument num_case could be set to decide the number of pre-processing cases to apply. Typically, it is set to 1 for training phases and to the max for test phases.

color_cast(offset=20, inplace=True)

Add a random value from [-offset, offset] to each channel

Parameters
  • offset – cast offset, >0 and <255

  • inplace – inplace imgs or not ( return new_imgs)

crop3(patch, num_case=1, inplace=True)

Crop a max square patch of the input image at given position and scale it into given size.

According to img size, crop position could be either (left, center, right) or (top, middle, bottom).

Parameters
  • patch (tuple) – the width and height the output image

  • num_case – num of cases, must be in [1,3]

  • inplace – inplace imgs or not ( return new_imgs)

crop5(patch, num_case=1, inplace=True)

Crop at positions from [left_top, left_bottom, right_top, right_bottom, and center].

Parameters
  • patch (tuple) – width and height of the result image.

  • num_case – num of cases, must be in [1,5]

  • inplace – inplace imgs or not ( return new_imgs)

crop8(patch, num_case=1, inplace=True)

This is a union of patch_5 and patch_and_scale.

You can follow this example to union any num of cases of imgtool methods

enhance(scale=0.2, inplace=True)

Apply random enhancement for Color,Contrast,Brightness,Sharpness.

Parameters
  • scale (float) – enhancement degree is from [1-scale, 1+scale]

  • inplace – inplace imgs or not ( return new_imgs)

flip(num_case=1, inplace=True)

Randomly flip a img left to right.

Parameters
  • num_case – num of cases, must be in {1,2}; if 2, then add the orignal and flipped img

  • inplace – inplace imgs or not (return new_imgs)

flip_down(num_case=1, inplace=True)

Randomly flip a img top to bottom. :param num_case: num of cases, must be in {1,2}; if 2, then add the orignal

and flip_down img

Parameters

inplace – inplace imgs or not (return new_imgs)

num_augmentation()

Return the total number of augmentations to each image

random_crop(patch, inplace=True)

Crop the image at random offset to get a patch of the given size.

Parameters
  • patch (tuple) – width and height of the patch

  • inplace (Boolean) – replace the internal images list with the patches if True; otherwise, return the patches.

random_crop_resize(patch, inplace=True)
Crop of the image at a random size between 0.08 to 1 of input image

and random aspect ratio between 3/4 to 4/3. This crop is then resized to the given patch size.

Parameters
  • patch (tuple) – width and height of the patch

  • inplace (Boolean) – replace the internal images list with the patches if True; otherwise, return the patches.

resize_by_list(size_list, num_case=1, inplace=True)
Parameters
  • num_case – num of resize cases, must be <= the length of size_list

  • inplace – inplace imgs or not ( return new_imgs)

resize_by_range(rng, inplace=True)
Parameters
  • rng – a tuple (begin,end), include begin, exclude end

  • inplace – inplace imgs or not ( return new_imgs)

rotate_by_list(angle_list, num_case=1, inplace=True)
Parameters
  • num_case – num of rotate cases, must be <= the length of angle_list

  • inplace – inplace imgs or not ( return new_imgs)

rotate_by_range(rng, inplace=True)
Parameters
  • rng – a tuple (begin,end) in degree, include begin, exclude end

  • inplace – inplace imgs or not ( return new_imgs)

singa.image_tool.color_cast(img, offset)

Add a random value from [-offset, offset] to each channel

singa.image_tool.crop(img, patch, position)

Crop the input image into given size at given position.

Parameters
  • patch (tuple) – width and height of the patch

  • position (list(str)) – left_top, left_bottom, right_top, right_bottom

  • center. (and) –

singa.image_tool.crop_and_resize(img, patch, position)

Crop a max square patch of the input image at given position and resize it into given size.

Parameters
  • patch (tuple) – width, height

  • position (list(str)) – left, center, right, top, middle, bottom.

singa.image_tool.enhance(img, scale)

Apply random enhancement for Color,Contrast,Brightness,Sharpness.

Parameters

scale (float) – enhancement degree is from [1-scale, 1+scale]

singa.image_tool.load_img(path, grayscale=False)

Read the image from a give path

singa.image_tool.resize(img, small_size)

Resize the image to make the smaller side be at the given size