Skip to main content

Checks if colors are in image / Detects multiple colors in images - Fast Cython algorithm

pip install cythoncolortools

Tested against Windows 10 / Python 3.11 / Anaconda

Important!

The module will be compiled when you import it for the first time. Cython and a C/C++ compiler must be installed!

How to use it in Python

import numpy as np
import cv2
from cythoncolortools import search_colors, are_any_colors_in_picture

# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
picpath = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picpath)
colors0 = np.array([[255, 255, 255]], dtype=np.uint8)
resus0 = search_colors(pic=pic, colors=colors0)
colors1 = np.array(
    [
        (66, 71, 69),
        (62, 67, 65),
        (144, 155, 153),
        (52, 57, 55),
        (127, 138, 136),
        (53, 58, 56),
        (51, 56, 54),
        (32, 27, 18),
        (24, 17, 8),
    ],
    dtype=np.uint8,
)
resus1 = search_colors(pic=pic, colors=colors1)
print(resus1)


# %timeit search_colors(pic=pic, colors=colors1, add_results=True, cpus=5)
# %timeit search_colors(pic=pic, colors=colors1, add_results=False, cpus=5)

# %timeit search_colors(pic=pic, colors=colors0, add_results=True, cpus=5)
# %timeit search_colors(pic=pic, colors=colors0, add_results=False, cpus=5)

# %timeit search_colors(pic=pic, colors=colors1, add_results=True, cpus=1)
# %timeit search_colors(pic=pic, colors=colors1, add_results=False, cpus=1)

# %timeit search_colors(pic=pic, colors=colors0, add_results=True, cpus=1)
# %timeit search_colors(pic=pic, colors=colors0, add_results=False, cpus=1)
print(search_colors(pic=pic, colors=colors1, add_results=True, cpus=5))
print(search_colors(pic=pic, colors=colors1, add_results=False, cpus=5))
print(search_colors(pic=pic, colors=colors0, add_results=True, cpus=5))
print(search_colors(pic=pic, colors=colors0, add_results=False, cpus=5))
print(search_colors(pic=pic, colors=colors1, add_results=True, cpus=1))
print(search_colors(pic=pic, colors=colors1, add_results=False, cpus=1))
print(search_colors(pic=pic, colors=colors0, add_results=True, cpus=1))
print(search_colors(pic=pic, colors=colors0, add_results=False, cpus=1))


print(are_any_colors_in_picture(pic, colors1, cpus=-1))
print(are_any_colors_in_picture(pic, colors0, cpus=-1))
print(are_any_colors_in_picture(pic, colors1, cpus=1))
print(are_any_colors_in_picture(pic, colors0, cpus=1))

print(are_any_colors_in_picture(pic, [[111, 111, 121]], cpus=-1))
print(are_any_colors_in_picture(pic, [[111, 111, 121]], cpus=1))

# 57 ms ± 2.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 47.9 ms ± 1.02 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 22 ms ± 43.6 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 18.8 ms ± 162 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# 260 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 256 ms ± 283 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
# 25.7 ms ± 47.2 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
# 25.8 ms ± 110 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# [[  38    0  136  138  127]
#  [   1    1  153  155  144]
#  [  40    1  153  155  144]
#  ...
#  [1973 5903   65   67   62]
#  [1952 5904   65   67   62]
#  [2868 6041   65   67   62]]
# [[4522    0   69   71   66]
#  [   1    1  153  155  144]
#  [  40    1  153  155  144]
#  ...
#  [4522 6622    8   17   24]
#  [4523 6622    8   17   24]
#  [4524 6622    8   17   24]]
# [[  38    0]
#  [4522    0]
#  [   1    1]
#  ...
#  [2844 6622]
#  [2854 6622]
#  [2865 6622]]
# [[2085  832  255  255  255]
#  [1692  858  255  255  255]
#  [1688  896  255  255  255]
#  ...
#  [3526 5491  255  255  255]
#  [3527 5491  255  255  255]
#  [2491 5525  255  255  255]]
# [[2085  832]
#  [1692  858]
#  [1688  896]
#  ...
#  [3526 5491]
#  [3527 5491]
#  [2491 5525]]
# [[4522    0   69   71   66]
#  [4522    3   69   71   66]
#  [4523    3   69   71   66]
#  ...
#  [4522 6622    8   17   24]
#  [4523 6622    8   17   24]
#  [4524 6622    8   17   24]]
# [[4522    0]
#  [4522    3]
#  [4523    3]
#  ...
#  [4522 6622]
#  [4523 6622]
#  [4524 6622]]
# [[2085  832  255  255  255]
#  [1692  858  255  255  255]
#  [1688  896  255  255  255]
#  ...
#  [3526 5491  255  255  255]
#  [3527 5491  255  255  255]
#  [2491 5525  255  255  255]]
# [[2085  832]
#  [1692  858]
#  [1688  896]
#  ...
#  [3526 5491]
#  [3527 5491]
#  [2491 5525]]
# True
# True
# True
# True
# False
# False

Metadata

Release files for cythoncolortools 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cythoncolortools 0.11
File Size Uploaded Provenance Actions
cythoncolortools-0.11.tar.gz 24.2 kB Without provenance Details for cythoncolortools-0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cythoncolortools 0.11
File Interpreter ABI Platform Actions
cythoncolortools-0.11-py3-none-any.whl Python 3 none any Details for cythoncolortools-0.11-py3-none-any.whl

Total release size: 48.5 kB

Release files / cythoncolortools-0.11.tar.gz

Download URL cythoncolortools-0.11.tar.gz
Size 24.2 kB
Tags Source
SHA-256 checksum
How to use checksums
88998c8a13d1209951cadafe1f59719d91ee88059a37585d457cbfaded331b26
BLAKE2b-256 checksum
How to use checksums
0941d38ed0c220a6983b26d0b457f2f9b059a834d6270f5a313c2e621eaea67c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / cythoncolortools-0.11-py3-none-any.whl

Download URL cythoncolortools-0.11-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c52dca62e97f39aef52212f72138a3f081a9bb664ee0ff024bc4bd0076277e73
BLAKE2b-256 checksum
How to use checksums
ab74e98bee9eee579fe74bc9c946f67c01921044d9ad6eb13c5f3dac3ef7e5c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Security for cythoncolortools 0.11

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.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page