Locate RGB values in a picture! Up to 10x faster than NumPy, 100x faster than PIL.
Project description
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file locate_pixelcolor_cpp-0.10.tar.gz
.
File metadata
- Download URL: locate_pixelcolor_cpp-0.10.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee42e4912987620f719b082561617ce4f7d08f740356b86caa6bf517531e2fdc |
|
MD5 | facab53f7939b32dceae8e3284597866 |
|
BLAKE2b-256 | 774c9de1e115d6da2aa5f66007b82cafb63e436b499e05f947fc6d7f2cef033a |
File details
Details for the file locate_pixelcolor_cpp-0.10-py3-none-any.whl
.
File metadata
- Download URL: locate_pixelcolor_cpp-0.10-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4842591ebdcdbba547b45a8ed468a2e58f4465769bd30f9bf3eda4dda84885a3 |
|
MD5 | 366fec87c41548b8a95e567c8c51151f |
|
BLAKE2b-256 | a50f1a831713c50a6f79ca891a64ff4b599e9f678dd208774e311a5c09818af5 |