Crops an image based on allowed colors
pip install cycolorcrop
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)
Metadata
Release files for cycolorcrop 0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cycolorcrop-0.10.tar.gz | 89.7 kB | Without provenance | Details for cycolorcrop-0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cycolorcrop-0.10-py3-none-any.whl | Python 3 | none | any | Details for cycolorcrop-0.10-py3-none-any.whl |
Total release size: 180.4 kB
Release files / cycolorcrop-0.10.tar.gz
| Download URL | cycolorcrop-0.10.tar.gz |
|---|---|
| Size | 89.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc997f7e632f1829ce3d9dc954f7e224decd755665cf893b4c0d6aefe441cda2
|
|
BLAKE2b-256 checksum How to use checksums |
3ed562c00f07d8d8e75c00d9884156ee1345ee86634e80e1d93949c0a11fded8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Release files / cycolorcrop-0.10-py3-none-any.whl
| Download URL | cycolorcrop-0.10-py3-none-any.whl |
|---|---|
| Size | 90.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c18f1e2a7c290edfd20e58021e9864a06e96dda86c8aab03516ef6ba3cc5247
|
|
BLAKE2b-256 checksum How to use checksums |
b9af1874af92e470b1a7a83c343546145ace295f7ce50ffb76e6c2e427aadf83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.7
|
Security for cycolorcrop 0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.