picturepyfect package

Submodules

picturepyfect.applyfilter module

exception picturepyfect.applyfilter.FilterDimensionException[source]

Bases: Exception

Creates custom exception for Filter Dimension

exception picturepyfect.applyfilter.FilterTypeException[source]

Bases: Exception

Creates custom exception for File Type

exception picturepyfect.applyfilter.ImageDimensionException[source]

Bases: Exception

Creates custom exception for Image Dimension

picturepyfect.applyfilter.build_filter(kernel_type, kernel_size)[source]

This function can be used to build predefined filters.

Parameters
  • filter_type (string) –

    One of the following values:

    blur: Used to blur the picture sharpen: Used to increase the sharpness of the image

    More options will be added as enhancements

  • filter_size (int) – An integer determining the filter size.

  • Returns

  • ---------

  • image_property (numpy.ndarray) – A kernel_size * kernel_size numpy array representing the filter.

Examples

>>> build_filter("blur", 3)
array([[0.01, 0.01, 0.01],
   [0.01, 0.01, 0.01],
   [0.01, 0.01, 0.01]])
>>> build_filter("sharpen", 7)
array([[ 0,  0,  0, -1,  0,  0,  0],
   [ 0,  0, -1, -1, -1,  0,  0],
   [ 0, -1, -1, -1, -1, -1,  0],
   [-1, -1, -1,  5, -1, -1, -1],
   [ 0, -1, -1, -1, -1, -1,  0],
   [ 0,  0, -1, -1, -1,  0,  0],
   [ 0,  0,  0, -1,  0,  0,  0]])
picturepyfect.applyfilter.filter_pyfect(image, filter_type='blur', filter_size=3, custom_filter=None)[source]

This function applies predefined or custom filters on an image.

The function can be applied on single channel or 3-channel images. The users can choose from predefined filters or can create their new filters. This can be used for various purposes like entertainment application or visualization of convolutional neural network.

Parameters
  • image (numpy.ndarray) – A n1*n2 or n1*n2*3 numpy array to representing single channel or 3-channel image

  • filter_type (string) –

    One of the following values:

    blur: Used to blur the picture sharpen: Used to increase the sharpness of the image custom: Allows users to use their own filter

    More options will be added as enhancements

  • filter_size (int) – An integer determining the filter size. This is used if the filter_type is not custom. Default: 3

  • custom_filter (numpy.ndarray) – A k1*k2 or k1*k2*3 numpy array allows users to pass their own filter. This is only used if the users select filter_type = “custom”

  • Returns

  • ---------

  • filtered_image (numpy.ndarray) – A numpy array representing the transformed image.

Examples

>>> image = np.arange(1, 26).reshape(5, 5)
>>> kernel = np.ones((2,2))
>>> filter_pyfect(image, filter_type="custom", custom_filter=kernel)
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
   [0.27777778, 0.33333333, 0.38888889, 0.44444444],
   [0.55555556, 0.61111111, 0.66666667, 0.72222222],
   [0.83333333, 0.88888889, 0.94444444, 1.        ]])
>>> img = np.arange(1, 76).reshape(5, 5, 3)
>>> kernel = np.ones((2,2,3))
>>> filter_pyfect(img, filter_type="custom", custom_filter=kernel)[:,:,1]
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
   [0.27777778, 0.33333333, 0.38888889, 0.44444444],
   [0.55555556, 0.61111111, 0.66666667, 0.72222222],
   [0.83333333, 0.88888889, 0.94444444, 1.        ]])
>>> img = np.arange(1, 26).reshape(5, 5)
>>> filter_pyfect(img, filter_type="blur")
array([[0.        , 0.08333333, 0.16666667],
   [0.41666667, 0.5       , 0.58333333],
   [0.83333333, 0.91666667, 1.        ]])
picturepyfect.applyfilter.filter_pyfect_2D(image, kernel)[source]

Performs convolution type filtering using 2D kernel on a 2D numpy array.

Parameters
  • image (numpy.ndarray) – A 2D numpy array representing a single channel image

  • kernel (numpy.ndarray) – A 2D numpy array representing a convolution filter

  • Returns

  • ---------

  • filtered_image (numpy.ndarray) – Result of the filtering as a 2D numpy array. Please note that the values are scaled so that they are between range of 0 and 1 using minmax scaler for plotting stability

Examples

>>> image = np.arange(1, 26).reshape(5, 5)
>>> kernel = np.ones((2,2))
>>> filter_pyfect_2D(image, kernel)
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
       [0.27777778, 0.33333333, 0.38888889, 0.44444444],
       [0.55555556, 0.61111111, 0.66666667, 0.72222222],
       [0.83333333, 0.88888889, 0.94444444, 1.        ]])
picturepyfect.applyfilter.filter_pyfect_3D(image, kernel)[source]

Performs convolution type filtering using 3D kernel on a 3D numpy array.

Both the kernel and image should have 3 channels in the 3rd dimension.

Parameters
  • image (numpy.ndarray) – A 3D numpy array representing a 3 channel image

  • kernel (numpy.ndarray) – A 3D numpy array representing a 3D convolution filter with 3 channels

  • Returns

  • ---------

  • filtered_image (numpy.ndarray) – Result of the filtering as a 3D numpy array. Please note that the values are scaled so that they are between range of 0 and 1 using minmax scaler within each channel for plotting stability

Examples

>>> image = np.arange(1, 76).reshape(5, 5, 3)
>>> kernel = np.ones((2,2,3))
>>> result = filter_pyfect_3D(image, kernel)
>>> result[:,:,0]
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
   [0.27777778, 0.33333333, 0.38888889, 0.44444444],
   [0.55555556, 0.61111111, 0.66666667, 0.72222222],
   [0.83333333, 0.88888889, 0.94444444, 1.        ]])
>>> result[:,:,1]
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
   [0.27777778, 0.33333333, 0.38888889, 0.44444444],
   [0.55555556, 0.61111111, 0.66666667, 0.72222222],
   [0.83333333, 0.88888889, 0.94444444, 1.        ]])
>>> result[:,:,2]
array([[0.        , 0.05555556, 0.11111111, 0.16666667],
   [0.27777778, 0.33333333, 0.38888889, 0.44444444],
   [0.55555556, 0.61111111, 0.66666667, 0.72222222],
   [0.83333333, 0.88888889, 0.94444444, 1.        ]])

picturepyfect.compression_pyfect module

exception picturepyfect.compression_pyfect.DimensionError[source]

Bases: Exception

Raised when when a numpy array has the wrong shape.

picturepyfect.compression_pyfect.check_values(image, kernel_size)[source]

This function checks that the image and kernel size are valid inputs and raises an error if not.

Parameters
  • image (numpy.ndarray) – A n*n or n*n*3 numpy array representing a single channel or 3-channel image.

  • kernel_size (int) – The size of the kernel to be passed over the image. The resulting filter moving across the image will be a 2D array with dimensions kernel_size x kernel_size.

Examples

>>> check_values(image, kernel_size=3)
picturepyfect.compression_pyfect.compression_pyfect(image, kernel_size=2, pooling_function='max')[source]

This function uses a lossy pooling algorithm to compress an image.

The function can be applied to single channel or 3-channel images. The user passes an image which is to be compressed and the resulting compressed numpy array is returned. The user can also specify the pooling algorithm to be used and the size of the kernel to apply over the image.

Parameters
  • image (numpy.ndarray) – A n*n or n*n*3 numpy array representing a single channel or a 3-channel image.

  • kernel_size (int) – The size of the kernel to be passed over the image. The resulting filter moving across the image will be a 2D array with dimensions kernel_size x kernel_size. Default: 2

  • pooling_function (str) – The pooling algorithm to be used within a kernel. There are three options: “max”, “min”, and “mean”. Default: “max”

  • Returns

  • ---------

  • numpy.ndarray – A numpy array representing the compressed image.

Examples

>>> compression_pyfect(image, kernel_size=3, pooling_function="max")
array([[0.04737957, 0.04648845, 0.04256656, 0.04519495],
   [0.04657273, 0.04489012, 0.04031093, 0.04047667],
   [0.04641026, 0.04106843, 0.04560866, 0.04732271],
   [0.0511907 , 0.04518351, 0.04946411, 0.04030291]])
picturepyfect.compression_pyfect.pool_band(band, kernel_size, pool_func)[source]

This function is to be used in conjunction with compression_pyfect and compresses a single colour band of an image.

The function applies a lossy pooling algorithm to compress the specified colour band and the resulting compressed numpy array is returned.

Parameters
  • band (numpy.ndarray) – A n*n numpy array representing a single colour band of an image.

  • kernel_size (int) – The size of the kernel to be passed over the image. The resulting filter moving across the image will be a 2D array with dimensions kernel_size x kernel_size.

  • pooling_function (str) – The pooling algorithm to be used within a kernel. There are three options: “max”, “min”, and “mean”.

  • Returns

  • ---------

  • numpy.ndarray – An n*n numpy array representing the compressed image.

Examples

>>> pool_band(image, kernel_size=3, pooling_function="max")
array([[0.04737957, 0.04648845, 0.04256656, 0.04519495],
   [0.04657273, 0.04489012, 0.04031093, 0.04047667],
   [0.04641026, 0.04106843, 0.04560866, 0.04732271],
   [0.0511907 , 0.04518351, 0.04946411, 0.04030291]])

picturepyfect.get_property module

picturepyfect.get_property.get_property(image)[source]

Extract RGB or RGBA image properties. The output properties includes means and medians of RGB channels along with the file dimension and total pixels.

Parameters
  • image (numpy.ndarray) – A n*n*3 numpy array to representing 3 or 4-channel RGB or RGBA image

  • Returns

  • ---------

  • image_property (dictionary) – a dictionary of image properties for dimension of width and height, total pixels, and each channel’s mean and median value stored as a list for each channel.

Examples

>>> get_property(image)
{dimension: [1280, 720], total_pixels: 921600,
r_channel: [80, 90], g_channel: [120, 90], b_channel: [155, 160]}

picturepyfect.rotate_pyfect module

picturepyfect.rotate_pyfect.rotate_pyfect(image, n_rot=1)[source]

This rotate function can be used to apply a rotational transformation on an image.

The function can be applied on greyscale or 3-channel images. The users can choose some degree, theta, which is used in a rotational matrix operation to transform the image.

Parameters
  • image (numpy.ndarray) – A n*n or n*n*3 numpy array representing an 3-channel image

  • n_rot (int) – The number of 90 degree rotations to implement

  • Returns

  • ---------

  • rotated_image (numpy.ndarray) – A n*n or n*n*3 numpy array which is the input image rotated by a multiple of 90 degrees

Examples

>>> np.random.seed(42)
>>> image = np.random.rand(2, 2, 1)
>>> rotate_pyfect(image, deg=1)
array([[[0.73199394],
    [0.37454012]],
[[0.59865848],

[0.95071431]]])

Module contents