Skip to main content

Locate RGB values in a picture! Up to 10x faster than NumPy, 100x faster than PIL.

How to install

pip install locate-pixelcolor-cpp

Please install this C++ compiler:

MSVC ..... C++ x64/x86 build tools from: https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community&channel=Release&version=VS2022&source=VSLandingPage&passive=false&cid=2030

Localize the following files (Version number might vary) and copy their path:

vcvarsall_bat = r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"

cl_exe = r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x64\cl.exe"

link_exe = r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x64\link.exe"

Compile the code

from locate_pixelcolor_cpp import compile_localize_picture_color_with_cpp

compile_localize_picture_color_with_cpp(

    vcvarsall_bat=r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat",

    cl_exe=r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x64\cl.exe",

    link_exe=r"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x64\link.exe",

)

Benchmark

# Let's use a 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/



from locate_pixelcolor_cpp import search_colors # The function can only be imported when the compilation was successful ( compile_localize_picture_color_with_cpp )

import cv2

path=r"C:\Users\Gamer\Documents\Downloads\pexels-alex-andrews-2295744.jpg"

im = cv2.imread(path)





colors=[(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),]

#%timeit search_colors(im, colors=colors)

##127 ms ± 3.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)



from locate_pixelcolor import search_colors as search_colors2

# first version with numexpr

# https://github.com/hansalemaos/locate_pixelcolor

#%timeit search_colors2(im,colors)

##400 ms ± 18.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)



import numpy as np 

b,g,r = im[...,0],im[...,1],im[...,2]

#%timeit np.where(((b==66)&(g==71)&(r==69))|((b==62)&(g==67)&(r==65))|((b==144)&(g==155)&(r==153))|((b==52)&(g==57)&(r==55))|((b==127)&(g==138)&(r==136))|((b==53)&(g==58)&(r==56))|((b==51)&(g==56)&(r==54))|((b==32)&(g==27)&(r==18))|((b==24)&(g==17)&(r==8)))

##1 s ± 16.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)





from PIL import Image

img = Image.open(path)

img = img.convert("RGB")

datas = img.getdata()



def pi():

    newData = []

    for item in datas:

        if (item[0] == 66 and item[1] == 71 and item[2] == 69) or (item[0] == 62 and item[1] == 67 and item[2] == 65) or (item[0] == 144 and item[1] == 155 and item[2] == 153) or (item[0] == 52 and item[1] == 57 and item[2] == 55) or (item[0] == 127 and item[1] == 138 and item[2] == 136) or (item[0] == 53 and item[1] == 58 and item[2] == 56) or (item[0] == 51 and item[1] == 56 and item[2] == 54) or (item[0] == 32 and item[1] == 27 and item[2] == 18) or (item[0] == 24 and item[1] == 17 and item[2] == 8):

            newData.append(item)

    return newData

%timeit pi()



##10.6 s ± 51.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)





## One color 



from locate_pixelcolor_cpp import search_colors

import cv2

path=r"C:\Users\Gamer\Documents\Downloads\pexels-alex-andrews-2295744.jpg"

im = cv2.imread(path)

#%timeit search_colors(im, colors=[(255,255,255)])

#75.3 ms ± 247 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)







# first version with numexpr

# https://github.com/hansalemaos/locate_pixelcolor

from locate_pixelcolor import search_colors

import cv2

path=r"C:\Users\Gamer\Documents\Downloads\pexels-alex-andrews-2295744.jpg"

im = cv2.imread(path)

# %timeit search_colors(im, colors=[(255,255,255)])

# 98 ms ± 422 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)





b,g,r = im[...,0],im[...,1],im[...,2]

# %timeit np.where(((b==255)&(g==255)&(r==255)))

# 150 ms ± 209 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)







from PIL import Image

img = Image.open(path)

img = img.convert("RGB")

datas = img.getdata()

def get_coords_with_pil(col):

    newData = []

    for item in datas:

        if item[0] == col[0] and item[1] == col[1] and item[2] == col[2]:

            newData.append(item)

    return newData

%timeit get_coords_with_pil(col=(255,255,255))

3.41 s ± 14.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Metadata

Release files for locate-pixelcolor-cpp 0.10

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

Source distribution (sdist)

Source distribution for locate-pixelcolor-cpp 0.10
File Size Uploaded Provenance Actions
locate_pixelcolor_cpp-0.10.tar.gz 7.4 kB Without provenance Details for locate_pixelcolor_cpp-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for locate-pixelcolor-cpp 0.10
File Interpreter ABI Platform Actions
locate_pixelcolor_cpp-0.10-py3-none-any.whl Python 3 none any Details for locate_pixelcolor_cpp-0.10-py3-none-any.whl

Total release size: 16.2 kB

Release files / locate_pixelcolor_cpp-0.10.tar.gz

Download URL locate_pixelcolor_cpp-0.10.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ee42e4912987620f719b082561617ce4f7d08f740356b86caa6bf517531e2fdc
BLAKE2b-256 checksum
How to use checksums
774c9de1e115d6da2aa5f66007b82cafb63e436b499e05f947fc6d7f2cef033a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / locate_pixelcolor_cpp-0.10-py3-none-any.whl

Download URL locate_pixelcolor_cpp-0.10-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4842591ebdcdbba547b45a8ed468a2e58f4465769bd30f9bf3eda4dda84885a3
BLAKE2b-256 checksum
How to use checksums
a50f1a831713c50a6f79ca891a64ff4b599e9f678dd208774e311a5c09818af5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for locate-pixelcolor-cpp 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.

Release history Release notifications | RSS feed

This release

0.10 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