sharpedge

Submodules

Attributes

__version__

Functions

reposition_image(img[, flip, rotate, shift_x, shift_y])

Flip, rotate, and shift an image based on the specified requested action.

frame_image(img[, h_border, w_border, inside, color])

Add a decorative frame around the image with a customizable color.

modulate_image(img[, mode, ch_swap, ch_extract])

Convert or manipulate image color channels with flexibility for grayscale and RGB.

pooling_image(img, window_size[, pooling_method])

Perform pooling on an image using a specified window size and pooling function.

pca_compression(img[, preservation_rate])

Compress the input image using Principal Component Analysis (PCA) via the Singular Value Decomposition (SVD) method.

seam_carve(img, target_height, target_width)

Seam carve an image to resize it to the target dimensions.

Package Contents

sharpedge.__version__
sharpedge.reposition_image(img, flip='none', rotate='up', shift_x=0, shift_y=0)[source]

Flip, rotate, and shift an image based on the specified requested action.

This function allows repositioning of an image by applying one or more transformations (flipping, rotating, and shifting). Each transformation can be controlled by the respective parameters.

Parameters:
  • img (numpy.ndarray) – The input image as a 2D numpy array (grayscale) or 3D numpy array (RGB).

  • flip (str, optional) – Argument used to flip the image. It can be - ‘none’: No flipping. - ‘horizontal’: Flip the image horizontally. - ‘vertical’: Flip the image vertically. - ‘both’: Flip the image both horizontally and vertically. Default is ‘none’.

  • rotate (str, optional) – Argument used to rotate the image. It can be - ‘up’: No rotation. - ‘left’: Rotate the image 90 degrees counter-clockwise. - ‘right’: Rotate the image 90 degrees clockwise. - ‘down’: Rotate the image 180 degrees (flip upside down). Default is ‘up’.

  • shift_x (int, optional) – Argument used to shift the image along the x-axis. Default is 0.

  • shift_y (int, optional) – Argument used to shift the image along the y-axis. Default is 0.

Returns:

The repositioned image that has been flipped, rotated, and/or shifted based on the parameter values.

Return type:

numpy.ndarray

Raises:
  • ValueError

    • If the input image is not a 2D or 3D numpy array.

    • or if the flip or rotate arguments are invalid.

  • TypeError – If shift_x or shift_y is not an integer.

Warning

UserWarning
  • If the shift values are larger than the image dimensions.

  • If resizing causes unexpected behavior due to large shifts.

Examples

>>> img = np.random.rand(100, 100)
>>> repositioned_img = reposition_image(img, flip='horizontal', rotate='left', shift_x=10, shift_y=20)

For an RGB image: >>> img_rgb = np.random.rand(100, 100, 3) >>> repositioned_img = reposition_image(img_rgb, flip=’both’, rotate=’down’, shift_x=-5, shift_y=10)

sharpedge.frame_image(img, h_border=20, w_border=20, inside=False, color=0)[source]

Add a decorative frame around the image with a customizable color.

This function adds a border around the input image, either inside the image (preserving its original size) or outside (increasing its size). The border color can be specified for both grayscale and RGB images.

Parameters:
  • img (ndarray) – The input image as a 2D numpy array (grayscale) or 3D numpy array (RGB).

  • h_border (int, optional) – The height of the border in pixels. Default is 20.

  • w_border (int, optional) – The width of the border in pixels. Default is 20.

  • inside (bool, optional) – If True, the border is added inside the image (maintaining the image size). If False, the border is added outside the image (increasing the image size). Default is False.

  • color (int or tuple of int, optional) – The color of the border. Can be: - A single value for grayscale frames (e.g., 0 for black, 255 for white). - A tuple of 3 values for RGB frames (e.g., (0, 0, 0) for black). Default is 0 (black) for grayscale frames.

Returns:

The framed image with the applied border.

Return type:

ndarray

Examples

>>> img = np.random.rand(100, 100)
>>> framed_img = frame_image(img, h_border=30, w_border=30, inside=True, color=255)
>>> framed_img_rgb = frame_image(img_rgb, h_border=20, w_border=20, inside=False, color=(255, 0, 0))
sharpedge.modulate_image(img, mode='as-is', ch_swap=None, ch_extract=None)[source]

Convert or manipulate image color channels with flexibility for grayscale and RGB.

This function allows you to perform various color transformations on an image, including: - Converting between grayscale and RGB formats. - Swapping RGB channels to rearrange the color channels. - Extracting specific RGB channels (e.g., Red, Green, or Blue).

It supports both grayscale (2D) and RGB (3D) images. If a grayscale image is provided, channel swapping or extraction will not be applicable, and a notification will be given.

If the input image is already in the target mode (e.g., ‘gray’ or ‘rgb’), the function will notify that no conversion is necessary and return the original image.

Parameters:
  • img (numpy.ndarray) – Input image array. This can be either a 2D numpy array (grayscale image) or a 3D numpy array (RGB image). The dimensions of the image should be (height, width) for grayscale or (height, width, 3) for RGB images.

  • mode (str, optional) –

    The desired target color scale. This can one of the three: - ‘as-is’: No conversion needed - ‘gray’: Convert the image to grayscale. - ‘rgb’: Convert the image to RGB. Default is ‘as-is’.

    If the input image is already in the target mode, a notification will be printed, and the function will return the input image as-is without any conversion.

  • ch_swap (list/tuple of int, optional) –

    A list or tuple of integers representing the new order of the RGB channels. The list should contain exactly three elements, each of which is an index corresponding to the RGB channels: - [0, 1, 2] or (0, 1, 2) will keep the channels in their original order (Red, Green, Blue). - [2, 1, 0] or (2, 1, 0) will swap Red and Blue channels.

    If None, no channel swapping occurs. Default is None.

  • ch_extract (list/tuple of int, optional) –

    A list or tuple of integers representing the indices of the RGB channels to extract. For example: - [0] or (0): Extract only the Red channel. - [1, 2] or (1, 2): Extract the Green and Blue channels. - [2, 0] or (2, 0): Extract the Blue and Red channels.

    If None, no channel extraction occurs. Default is None.

Returns:

A numpy array representing the manipulated image. The output could be: - A grayscale image (2D array). - An RGB image (3D array). - A subset of RGB channels as a 3D image, where unextracted channels are set to 0. - A rearranged RGB image with swapped channels.

Return type:

numpy.ndarray

Raises:

ValueError – If the input image is not in grayscale or RGB format, or if any invalid channel indices are provided for extraction or swapping.

Notes

  • Grayscale images (2D arrays) do not have multiple color channels, so channel extraction or swapping will not be possible. These operations will be skipped with a corresponding notification.

  • If no operations are specified (i.e., no conversion or channel manipulation), the function will return the original image.

  • If the input image is already in the target mode (e.g., ‘gray’ or ‘rgb’), the function will notify that no conversion is necessary and return the image as-is.

Examples

>>> grayscale_image = modulate_image(rgb_image, mode='gray')  # Convert an RGB image to grayscale
>>> rgb_image_again = modulate_image(grayscale_image, mode='rgb')  # Convert a grayscale image back to RGB
>>> red_channel = modulate_image(rgb_image, ch_extract=[0])  # Extract the Red channel from an RGB image
>>> red_green_channels = modulate_image(rgb_image, ch_extract=[0, 1])  # Extract the Red and Green channels from an RGB image
>>> swapped_image = modulate_image(rgb_image, ch_swap=(2, 0, 1))  # Swap the Red and Blue channels in an RGB image
sharpedge.pooling_image(img, window_size, pooling_method=np.mean)[source]

Perform pooling on an image using a specified window size and pooling function.

Parameters:
  • img (numpy.ndarray) – The input image as a 2D numpy array (grayscale) or 3D numpy array (RGB).

  • window_size (int) – The size of the pooling window (e.g., 10 for 10x10 windows).

  • pooling_method (callable, optional) – The pooling function to apply to each window. Common options include numpy.mean, numpy.median, numpy.max, and numpy.min. Default is numpy.mean.

Returns:

The resized image, reduced by the pooling operation based on the specified window size and pooling function. For grayscale images, the result is a 2D array. For RGB images, the result is a 3D array normalized to the range [0.0, 1.0].

Return type:

numpy.ndarray

Raises:
  • TypeError – If window_size is not an integer or pooling_method is not callable.

  • ValueError – If the image dimensions are not divisible by the window size.

Examples

>>> img = np.random.rand(100, 100)
>>> pooled_img = pooling_image(img, window_size=10, pooling_method=np.mean)

For an RGB image: >>> img_rgb = np.random.rand(100, 100, 3) >>> pooled_img = pooling_image(img_rgb, window_size=20, pooling_method=np.max)

sharpedge.pca_compression(img, preservation_rate=0.9)[source]

Compress the input image using Principal Component Analysis (PCA) via the Singular Value Decomposition (SVD) method. This function first applies SVD to decompose the image array into its principal components, and then retains a specified portion of the eigenvectors based on the preservation rate. The output is the compressed image in 2D array. This function supports only grayscale (2D) images.

Parameters:
  • img (numpy.ndarray) – Input image array. This needs to be a 2D numpy array (grayscale image).

  • preservation_rate (float, optional) – The proportion of eigen values to preserve in the compressed image. Must be a value between 0 and 1. Higher values preserve more details from the original image, while lower values result in greater compression. Default is 0.9.

Returns:

A numpy array representing the manipulated image. The output would be: - A grayscale image (2D array).

Return type:

numpy.ndarray

Raises:
  • TypeError – If preservation_rate is not a float.

  • ValueError – If the input image is not valid (not a 2D array). If preservation_rate is not between 0 and 1.

  • Warning – If preservation_rate is very low (< 0.1), as it may result in significant quality loss.

Examples

Compress a grayscale image by retaining 80% of the variance: >>> compressed_img = pca_compression(img, preservation_rate=0.8)

Compress an RGB image with the default preservation rate (90%): >>> compressed_img = pca_compression(img)

sharpedge.seam_carve(img, target_height, target_width)[source]

Seam carve an image to resize it to the target dimensions.

Parameters:
  • img (numpy.ndarray) – A 3D array representing the original image (height, width, 3).

  • target_height (int) – The desired height of the resized image.

  • target_width (int) – The desired width of the resized image.

Returns:

The resized image with dimensions (target_height, target_width, 3).

Return type:

numpy.ndarray

Raises:

ValueError

  • If the input img is not a 3D numpy array with 3 channels.

  • If target_height or target_width is not an integer.

  • If target_height is greater than the original height or less than 1.

  • If target_width is greater than the original width or less than 1.

Warning

UserWarning
  • If the target size is the same as the original size (no resizing needed).

  • If only one dimension is resized (height or width remains the same).

  • If the original image or target size is reduced to a single pixel.

  • If the resizing is significant (difference of 200+ pixels), which may cause long processing times.

Examples

>>> img = np.random.rand(5, 5, 3)
>>> resized_img = seam_carve(img, 3, 3)
>>> print(resized_img.shape)
(3, 3, 3)