Skip to main content

Advanced color extraction and similarity analysis toolkit

Project description

MareArts XColor

PyPI version Python Downloads License: MIT

A robust, modern color extraction library for extracting dominant colors from images with optional GPU acceleration, mask-based region selection, advanced preprocessing, and color similarity analysis. Optimized with Cython for high performance and cross-platform compatibility.

Version 0.0.6 - Enhanced build system with ARM64 support, improved performance, and optional GPU acceleration.

✨ Features

🚀 Dual Performance Modes: CPU-only for compatibility, optional GPU acceleration for speed
🔍 Color Similarity Analysis: Find how much specific colors appear in images
🎭 Mask Support: Extract colors from specific regions using mask images
Modern Algorithms: K-means and DBSCAN clustering with LAB color space
🎨 Accurate Results: Perceptual LAB color space for better color accuracy
🏗️ Cross-Platform: Pre-built wheels for Linux (x86_64, ARM64), macOS, Windows
📦 Easy Installation: Works out-of-the-box with optional GPU dependencies
🔧 Developer Friendly: Dual Python/Cython implementation for easy contribution

📦 Installation

For CPU Users (Recommended - Works Everywhere)

pip install marearts-xcolor

Perfect for most users. Installs quickly with no GPU dependencies.

For GPU Users (Optional Acceleration)

# For CUDA 12.x users
pip install marearts-xcolor[gpu]

# For CUDA 11.x users
pip install marearts-xcolor cupy-cuda11x cuml

# Check your CUDA version first
nvidia-smi

💡 Smart Fallback: Even GPU installations work on CPU-only systems with automatic fallback.

Check Your Installation

from marearts_xcolor.gpu_utils import print_gpu_info
print_gpu_info()

🚀 Quick Start

Basic Usage (Works for Everyone)

from marearts_xcolor import ColorExtractor

# Auto mode - uses GPU if available, CPU otherwise
extractor = ColorExtractor(n_colors=5, use_gpu='auto')
colors = extractor.extract_colors('image.jpg')

print(colors)
# Output: [{'color': (255, 128, 64), 'percentage': 35.2}, ...]

GPU Mode Examples

# Recommended: Auto mode (best of both worlds)
extractor = ColorExtractor(use_gpu='auto')

# CPU only (guaranteed compatibility)
extractor = ColorExtractor(use_gpu='never')

# GPU required (only if you need guaranteed acceleration)
extractor = ColorExtractor(use_gpu='force')

📚 Usage Examples

1. Basic Color Extraction

from marearts_xcolor import ColorExtractor

# Create extractor with auto GPU detection
extractor = ColorExtractor(n_colors=5, use_gpu='auto')

# From file path
colors = extractor.extract_colors('image.jpg')

# From OpenCV image
import cv2
image = cv2.imread('image.jpg')
colors = extractor.extract_colors(image)

# From PIL image
from PIL import Image
import numpy as np
pil_image = Image.open('image.jpg')
image_array = np.array(pil_image)
colors = extractor.extract_colors(image_array)

2. Performance Comparison (CPU vs GPU)

import time
import numpy as np

# Create large test image
large_image = np.random.randint(0, 255, (2000, 2000, 3), dtype=np.uint8)

# Test CPU performance
extractor_cpu = ColorExtractor(n_colors=8, use_gpu='never')
start = time.time()
colors_cpu = extractor_cpu.extract_colors(large_image)
cpu_time = time.time() - start

# Test GPU performance (automatically falls back to CPU if unavailable)
extractor_gpu = ColorExtractor(n_colors=8, use_gpu='auto')
start = time.time()
colors_gpu = extractor_gpu.extract_colors(large_image)
gpu_time = time.time() - start

print(f"CPU time: {cpu_time:.2f}s")
print(f"GPU time: {gpu_time:.2f}s")
if gpu_time < cpu_time:
    print(f"GPU speedup: {cpu_time/gpu_time:.1f}x faster!")

3. With Mask (Region Selection)

# Extract colors only from white areas of mask
colors = extractor.extract_colors('image.jpg', 'mask.jpg')

# Create circular mask
import numpy as np
import cv2

height, width = 500, 500
mask = np.zeros((height, width), dtype=np.uint8)
cv2.circle(mask, (250, 250), 200, 255, -1)
colors = extractor.extract_colors(image, mask)

4. Color Similarity Analysis

# Define colors you want to find
target_colors = {
    'red': (255, 0, 0),
    'white': (255, 255, 255),
    'blue': (0, 0, 255)
}

# Find how much each color appears in the image
results = extractor.analyze_color_similarity('image.jpg', target_colors)

for color_name, result in results.items():
    print(f"{color_name}: {result['percentage']:.1f}% "
          f"(similarity: {result['similarity']:.1f}%)")

5. Brand Color Analysis

# Check brand color presence
brand_colors = {
    'brand_blue': (0, 123, 255),
    'brand_red': (220, 53, 69),
    'brand_green': (40, 167, 69)
}

results = extractor.analyze_color_similarity('product.jpg', brand_colors)

6. Performance Optimization Settings

# Fast mode - best for real-time processing
fast_extractor = ColorExtractor(
    n_colors=3,
    algorithm='kmeans',
    preprocessing=False,  # Skip preprocessing
    lab_space=False,      # Use RGB
    use_gpu='auto'        # Use GPU if available
)

# Accurate mode - best for detailed analysis
accurate_extractor = ColorExtractor(
    n_colors=8,
    algorithm='kmeans',
    preprocessing=True,   # Enable preprocessing
    lab_space=True,       # Use LAB color space
    use_gpu='auto'        # Use GPU if available
)

# CPU-only mode - guaranteed compatibility
cpu_extractor = ColorExtractor(
    n_colors=5,
    use_gpu='never'       # Force CPU mode
)

🖥️ Command Line Interface

# Basic extraction
marearts-xcolor image.jpg --colors 5

# With mask and visualization
marearts-xcolor image.jpg --mask mask.jpg --colors 8 --visualize output.png

# Fast mode
marearts-xcolor image.jpg --fast --colors 3

# Different algorithm
marearts-xcolor image.jpg --algorithm dbscan --colors 5

# Check version and GPU status
marearts-xcolor --version

📖 API Reference

ColorExtractor Class

ColorExtractor(n_colors=5, algorithm='kmeans', preprocessing=True, lab_space=True, use_gpu='auto')

Parameters:

  • n_colors: Number of dominant colors to extract (default: 5)
  • algorithm: 'kmeans' or 'dbscan' (default: 'kmeans')
  • preprocessing: Enable noise reduction and lighting normalization (default: True)
  • lab_space: Use LAB color space for accuracy (default: True)
  • use_gpu: GPU usage mode - 'auto', 'never', or 'force' (default: 'auto')

GPU Modes Explained

Mode Behavior Use Case
'auto' Uses GPU if available, falls back to CPU Recommended - Best performance with compatibility
'never' Always uses CPU Guaranteed compatibility, consistent performance
'force' Requires GPU, fails if unavailable Only when GPU acceleration is mandatory

Methods

extract_colors(image, mask=None)

Extract dominant colors from image.

Returns: List of {'color': (R,G,B), 'percentage': float}

analyze_color_similarity(image, target_colors, mask=None)

Analyze how much each target color appears in the image.

Returns: Dict with similarity analysis for each target color

GPU Utilities

from marearts_xcolor.gpu_utils import print_gpu_info, print_installation_guide

# Check GPU status and get installation instructions
print_gpu_info()

# Show complete installation guide
print_installation_guide()

📊 Example Output

Color Extraction:

[
  {"color": [255, 128, 64], "percentage": 35.2},
  {"color": [120, 200, 150], "percentage": 28.7},
  {"color": [80, 80, 80], "percentage": 20.1}
]

Color Similarity Analysis:

{
  "red": {
    "percentage": 20.5,
    "similarity": 95.0,
    "closest_color": [248, 12, 8]
  },
  "blue": {
    "percentage": 15.3,
    "similarity": 88.2,
    "closest_color": [10, 50, 200]
  }
}

⚡ Performance Guide

When GPU Acceleration Helps

  • ✅ Large images (>1000x1000 pixels)
  • ✅ Many colors requested (>10)
  • ✅ Batch processing multiple images
  • ✅ DBSCAN algorithm on large datasets

When CPU is Sufficient

  • ✅ Small to medium images (<500x500 pixels)
  • ✅ Few colors requested (<5)
  • ✅ Single image processing
  • ✅ K-means algorithm

Benchmark Your System

from marearts_xcolor.gpu_utils import print_gpu_info

# Check your GPU status
print_gpu_info()

# Run the performance comparison example above

🔧 Installation Troubleshooting

Common Issues & Solutions

GPU Libraries Not Found

Error: GPU forced but not available

Solution:

# Install GPU dependencies
pip install marearts-xcolor[gpu]

# Or manually
pip install cupy-cuda12x cuml  # for CUDA 12.x
pip install cupy-cuda11x cuml  # for CUDA 11.x

Check Your CUDA Version

nvidia-smi

CuPy Installation Issues

# For conda users
conda install cupy

# For pip users, check CUDA version and install matching CuPy
pip install cupy-cuda12x  # for CUDA 12.x
pip install cupy-cuda11x  # for CUDA 11.x

Still Having Issues?

# Use CPU-only mode (always works)
extractor = ColorExtractor(use_gpu='never')

Platform-Specific Notes

  • Linux: Full GPU support with CUDA
  • Windows: CuPy support available, cuML support limited
  • macOS: CPU-only recommended (no CUDA support)

📋 Requirements

Core Dependencies (Always Installed)

  • Python 3.9, 3.10, 3.11, 3.12
  • opencv-python==4.10.0.84
  • scikit-learn>=1.0.0
  • numpy (version-specific: 1.23.5 for Python <3.12, 1.26.0 for Python ≥3.12)
  • matplotlib>=3.5.0
  • pillow>=8.0.0
  • scipy>=1.7.0

Optional GPU Dependencies

  • cupy-cuda12x (for CUDA 12.x) or cupy-cuda11x (for CUDA 11.x)
  • cuml>=22.12.0 (RAPIDS cuML for GPU-accelerated clustering)

🌍 Platform Support

  • Linux: x86_64 and ARM64 (manylinux2014) with optional GPU acceleration
  • macOS: Intel (x86_64) and Apple Silicon (arm64) - CPU only
  • Windows: AMD64 and ARM64 with optional GPU acceleration

💡 Best Practices

For Most Users

# Recommended approach - works everywhere, fast when possible
extractor = ColorExtractor(use_gpu='auto')

For Production/CI

import os
# Force CPU mode in CI environments for consistency
gpu_mode = 'never' if os.getenv('CI') else 'auto'
extractor = ColorExtractor(use_gpu=gpu_mode)

For GPU-Intensive Applications

# Only use 'force' mode if GPU acceleration is critical
try:
    extractor = ColorExtractor(use_gpu='force')
except RuntimeError:
    print("GPU required but not available. Please install GPU dependencies.")
    sys.exit(1)

🔗 Links

📄 License

MIT License


Made with ❤️ by MareArts - Bringing GPU acceleration to color extraction without the complexity!

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

marearts_xcolor-0.0.7-cp312-cp312-win_amd64.whl (88.0 kB view details)

Uploaded CPython 3.12Windows x86-64

marearts_xcolor-0.0.7-cp312-cp312-macosx_10_13_universal2.whl (199.4 kB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

marearts_xcolor-0.0.7-cp311-cp311-win_amd64.whl (91.6 kB view details)

Uploaded CPython 3.11Windows x86-64

marearts_xcolor-0.0.7-cp311-cp311-macosx_10_9_universal2.whl (205.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

marearts_xcolor-0.0.7-cp310-cp310-win_amd64.whl (91.2 kB view details)

Uploaded CPython 3.10Windows x86-64

marearts_xcolor-0.0.7-cp310-cp310-macosx_10_9_universal2.whl (202.7 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)

marearts_xcolor-0.0.7-cp39-cp39-win_amd64.whl (116.2 kB view details)

Uploaded CPython 3.9Windows x86-64

marearts_xcolor-0.0.7-cp39-cp39-macosx_10_9_universal2.whl (217.2 kB view details)

Uploaded CPython 3.9macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file marearts_xcolor-0.0.7-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 86af4c173a9bb653f4c43cc907f0f9abc58919b4569350ee1fedb448ac26bf69
MD5 0c4154b1720a4ccb431ba332fe5f2ce2
BLAKE2b-256 86582c114b05c2f037411bfb4c16d49b3e8b336a646debedc0bc5203880f417f

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp312-cp312-win_amd64.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp312-cp312-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp312-cp312-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f12769c1cea38c5ea1c5284b0b39caa2d9a5659300da3d6b1ddb4b8ac87e0711
MD5 c28a064f5b769803239e9f50209b7af2
BLAKE2b-256 e643c2587cc9e79899e9d5a6e35c2f9fad7cf89a36504c0c3100938e9bcb1fa7

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp312-cp312-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp312-cp312-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 68eb54542a5f957e679362a3decd39159b41799d3baa5fa6f91c7639673425fe
MD5 4131b4a40e8c73d6ba2ce46ad9a28d6d
BLAKE2b-256 f1aea9b5780f613d5d0d3b77d16f2b35ddf365397328588a4aa82eb66c5d6405

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 dbf41c6f965401829f6c967a1ef94e17bbd5bd606c412f3efc4c9a8cc30fc17e
MD5 e37aedb8e9b8944c0f4e8da202b1bacc
BLAKE2b-256 8e9582aad9acf6e8a4ddb00b37c234ee8b60882475b6e53310e8f24aca155148

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp312-cp312-macosx_10_13_universal2.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5e8d9c8b4f3721842975b651a48015858ff22267e290bc323937204b411bdb23
MD5 0eb9fa9bd53a468484d742c8beb1db7c
BLAKE2b-256 d2efd3d591af087afff15c78acd435dbcf47976f031db8d83a29c72ac1172204

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp311-cp311-win_amd64.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f178ce9771328a25a988a43da4f05da0054dd5d19557a12310539128cf3e0c6
MD5 8f997e3686c9ce1ef80d72ff3843b7b5
BLAKE2b-256 c1ed51443c9b29c21635b62b7e23503a0de1a689eb2a76b7e0142e85ec98e382

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp311-cp311-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp311-cp311-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2f6a79e0b00fdc820e955f115c7c272580e49230cf0732c7a178336a528a4b93
MD5 cda83a126c40fd23abd4bc1449b5d7f5
BLAKE2b-256 3b873044c9762381635094849abcfe4c724e1d405ed612ae9a1c581de5baf940

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 af95e3f6ad86898104a7b0325538c6e2e2e000a96aa071e3f901ffd30538f33e
MD5 39082fc403038cb6730c4716b1a94427
BLAKE2b-256 132612b49b519531a82f6257956a6226b8354e4c013c4eb778f382fc9f069c18

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp311-cp311-macosx_10_9_universal2.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 86119f730cdebc5955396a4e1c2ece3749cd7468f6fab6ad07d2135f4d4a0b3b
MD5 55faeccfde8aa13267f0eec3c65259e9
BLAKE2b-256 d288ebdbe567c44938fe99f8e840f118969fb3839f471e8bd6bdc8dc8aeabbbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp310-cp310-win_amd64.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 41ca74b3c0f740f7f620394dcf21ae54ff1a62f3a10811ba05ec369e4952b059
MD5 430f8979fea20cc27111c358b89f2d3b
BLAKE2b-256 89062b378f97480150099b3ab39be1e3e1073278072d4655c84f26101392de15

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp310-cp310-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp310-cp310-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 09cbcb7e84fae40fb2a29c9273b8ec65c7e93f926d36cf24250ad79d76d624bd
MD5 4bd758ec24f4e49e7736e7520c119e3b
BLAKE2b-256 2011adfefc8eb89cf5db1df1b7d692fead53b2fcb91711c01c86b4f2eb45a6ca

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 dd64e7ace33b69a91f619bd7e1f02ef1388ad095c96b1b31e7c5659329840df3
MD5 459150a7f275f50eddfd712294198484
BLAKE2b-256 fba87e8ccf4099c1b645970d5353173ebb100e1523504a3fe6a73484a3b26538

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp310-cp310-macosx_10_9_universal2.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d44874ccca0cce90c7ecb6c625bac08cbd47337443cd094140c0eb2d29d285b9
MD5 fc6c9680341967a834f76a6ce0193001
BLAKE2b-256 2fbab9164584dc9ac7b280e2ebb2b617000dd08b1552d2a72304f5e300159942

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp39-cp39-win_amd64.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file marearts_xcolor-0.0.7-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f3f8f8e617f16e7d08228fdd54d836b84678645c3e7d681fd802f4196f82825
MD5 496cf3b7852e5e6f95dcb898f453f350
BLAKE2b-256 f74d0359da13630d9a79b8fa0e3b0c02b216c3e758e1c553e9a91b6760cf83a8

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp39-cp39-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp39-cp39-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 083a767133c4560716d3350679ef40da2a1e6c682adb6db03307e0d6a4a5849e
MD5 dd41e8ed047b7d7ad1a4de70e14a1828
BLAKE2b-256 e2a33a7322d8b230e0f97f05f4eb0ebcd1930efad67efaf57634da63e879081b

See more details on using hashes here.

File details

Details for the file marearts_xcolor-0.0.7-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for marearts_xcolor-0.0.7-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 332a53105968916f25e57eaf7956fee6ad8ee18bcb421ced57c21ae8b25a09d9
MD5 b55e8bafe3c6c03f31dbefa5a1fd1a52
BLAKE2b-256 0cffc1caa207851ba890c0300e91ba97b1598fc05494ee8027ba055a6695bdb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for marearts_xcolor-0.0.7-cp39-cp39-macosx_10_9_universal2.whl:

Publisher: workflow.yml on MareArts/marearts-xcolor-pypi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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