Skip to main content

Crops an image based on allowed colors

Project description

Crops an image based on allowed colors

pip install cycolorcrop

[!CAUTION] Cython and a C++ compiler must be installed! The function will be compiled the first time you import it!

How to use

    Crops an image based on allowed colors using external C++ routines.

    This function determines the cropping boundaries of an image by searching for specified allowed colors.
    It leverages external functions from "colorcrop.hpp" to compute the starting and ending coordinates along
    both the x and y axes. The function accepts an image and an array of colors, ensures proper type and
    memory alignment, and returns the cropped image along with the crop coordinates.

    Parameters
    ----------
    img : numpy.ndarray
        The input image as a NumPy array with shape (height, width, channels). The channels must be either 3 (RGB)
        or 4 (RGBA). If the image has 3 channels, an alpha channel with full opacity (255) will be appended.
    search_for_colors : numpy.ndarray
        A 2D NumPy array where each row represents a color in RGB or RGBA format. If provided with 3 channels,
        an alpha channel with full opacity (255) is added to each color.
    parallel : bool, optional
        Flag indicating whether to use parallel processing for computing crop coordinates. If True, the function
        uses a parallel loop (with Cython's prange) to compute the boundaries. Default is False.

    Returns
    -------
    list
        A two-element list where:
          - The first element is the cropped image (NumPy array).
          - The second element is a dictionary containing the crop boundaries:
                {
                    "start_x": int,  # Starting x-coordinate
                    "end_x": int,    # Ending x-coordinate
                    "start_y": int,  # Starting y-coordinate
                    "end_y": int     # Ending y-coordinate
                }

    Raises
    ------
    ValueError
        If either the image or the search_for_colors array is empty, or if the channel configuration is not supported
        (only RGB/RGBA images are allowed).

    Notes
    -----
    - The function ensures that both input arrays are of type np.uint8 and are C-contiguous and aligned.
    - When necessary, the image and search_for_colors arrays are copied to meet these requirements.
    - If parallel processing is enabled, the crop coordinates are computed using a parallel loop with
      the external function 'find_coord'.
    - The actual cropping is performed by slicing the original image based on the computed coordinates.
    - External functions used (from "colorcrop.hpp"):
          find_start_y, find_end_y, find_start_x, find_end_x, and find_coord.
    - RGBA is faster than RGB

Usage example

from cycolorcrop import crop_image
import cv2
import numpy as np

img = cv2.imread(
    r"C:\ProgramData\anaconda3\envs\image2crop.png", cv2.IMREAD_UNCHANGED
)  # (RGB or RGBA)
search_for_colors = np.array(
    [[255, 255, 255]], dtype=np.uint8
)  # more than one color can be specified (RGB or RGBA)

croppedimg, crop_coords1 = crop_image(img, search_for_colors, parallel=False)
cv2.imwrite(r"C:\ProgramData\anaconda3\envs\image2cropped_normal.png", croppedimg)

croppedimg2, crop_coords2 = crop_image(img, search_for_colors, parallel=True)
cv2.imwrite(r"C:\ProgramData\anaconda3\envs\image2cropped_parallel.png", croppedimg2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cycolorcrop-0.10.tar.gz (89.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cycolorcrop-0.10-py3-none-any.whl (90.7 kB view details)

Uploaded Python 3

File details

Details for the file cycolorcrop-0.10.tar.gz.

File metadata

  • Download URL: cycolorcrop-0.10.tar.gz
  • Upload date:
  • Size: 89.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cycolorcrop-0.10.tar.gz
Algorithm Hash digest
SHA256 bc997f7e632f1829ce3d9dc954f7e224decd755665cf893b4c0d6aefe441cda2
MD5 e3485658da883f30b0893ffb6ac92471
BLAKE2b-256 3ed562c00f07d8d8e75c00d9884156ee1345ee86634e80e1d93949c0a11fded8

See more details on using hashes here.

File details

Details for the file cycolorcrop-0.10-py3-none-any.whl.

File metadata

  • Download URL: cycolorcrop-0.10-py3-none-any.whl
  • Upload date:
  • Size: 90.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cycolorcrop-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 1c18f1e2a7c290edfd20e58021e9864a06e96dda86c8aab03516ef6ba3cc5247
MD5 3ca989abebbaff9ecc2b35fb3e6a014b
BLAKE2b-256 b9af1874af92e470b1a7a83c343546145ace295f7ce50ffb76e6c2e427aadf83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page