Skip to main content
Change of provenance

The repository or configuration that created this release differ from a previous release. View details.

image-array-and-histogram

CI PyPI Python License: MIT

Fast image histograms, exposure statistics and histogram comparison for NumPy and Pillow, with a command-line tool.

If you just need a 256-bin histogram of one image, img.convert("L").histogram() is enough. This library adds what the built-ins don't: per-channel and 16-bit/float histograms, exposure statistics (percentiles, clipping, entropy), histogram comparison, streaming accumulation over whole datasets, and the imghist CLI. It depends only on NumPy and Pillow, and the hot paths are vectorised (np.bincount and Pillow's C code, never Python loops).

Installation

pip install image-array-and-histogram
# with plotting support for `imghist --plot`:
pip install "image-array-and-histogram[plot]"

Requires Python 3.11+ (tested on 3.11–3.15), NumPy 1.23.2+ and Pillow 13+. The minimum Pillow version is the first release without publicly known security vulnerabilities, since this library opens arbitrary image files. The package ships type hints.

Quick start

from PIL import Image

from image_array_and_histogram import HistAccumulator, compare_hist, get_hist, hist_stats

img = Image.open("photo.jpg")

# Histograms: 256 gray bins, or one row per RGB channel
gray = get_hist(img)  # ndarray, shape (256,), int64
rgb = get_hist(img, channels="rgb")  # shape (3, 256)
coarse = get_hist(img, bins=16, density=True)  # 16 bins summing to 1.0

# Exposure statistics, computed from the histogram
stats = hist_stats(img)
print(f"mean {stats.mean:.1f}, median {stats.median}, p99 {stats.percentiles[99]}")
print(f"blown highlights: {stats.clipped_high:.2%}")

# Compare two images (1.0 = identical for intersection)
other = img.rotate(90)
print(compare_hist(get_hist(img), get_hist(other), method="intersection"))

# Accumulate one histogram over many images without holding them in memory
acc = HistAccumulator(channels="rgb")
for path in ["photo.jpg", "photo.jpg"]:
    with Image.open(path) as im:
        acc.update(im)
print(acc.n_images, acc.stats()["r"].mean)

16-bit images keep their full precision:

import numpy as np

from image_array_and_histogram import get_hist, hist_stats

deep = np.random.default_rng(0).integers(0, 65536, size=(480, 640), dtype=np.uint16)
counts = get_hist(deep, bins=1024)  # range defaults to (0, 65536) for uint16
print(hist_stats(deep, bins=65536).median)

Command line

imghist stats photo.jpg
imghist stats ./dataset -r --channels rgb -j 0
imghist stats ./dataset -r --format csv --per-file -o report.csv
imghist compare day.jpg night.jpg --method bhattacharyya
pip install "image-array-and-histogram[plot]" && imghist stats ./dataset -r --plot hist.png

Example output:

imghist: 1,204 images (3 skipped) · channel: gray · 256 bins

count     14,412,000,000
mean      118.42    std 61.07
min       0         max 255
median    112       p1 3  p5 14  p25 69  p75 167  p95 228  p99 249
entropy   7.62 bits
clipped   low 0.41%  high 1.87%

▁▁▂▃▄▅▆▇██▇▆▅▅▄▄▃▃▃▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂
0                                                            255

imghist stats PATH [PATH ...] options:

Option Default Meaning
-r, --recursive off recurse into directories
--ext .png,.jpg,.jpeg,.tif,.tiff,.bmp,.webp,.gif extensions to include (case-insensitive)
--channels gray gray or rgb
--bins 256 number of bins
--range LO HI dtype default histogram range; required for float images and to mix 8/16-bit images
--per-file off also report each file
--format text text, json or csv
--width 64 width of the text histogram
--plot FILE none save a plot (needs the [plot] extra)
-j, --jobs 1 worker processes; 0 = one per CPU
--strict off stop at the first unreadable file instead of skipping it
-o, --output FILE stdout write the report to a file

imghist compare A B accepts files or directories (a directory is accumulated into one histogram) and the same discovery options, plus --method and --format text|json.

Hidden files and directories are skipped. Unreadable files are reported on stderr and skipped. Exit codes: 0 success, 1 no images found / none readable / --strict failure, 2 usage error. python -m image_array_and_histogram works too.

API reference

All functions accept a PIL.Image.Image or an array: (H, W) grayscale, or (H, W, 3) / (H, W, 4) RGB(A) (alpha is ignored). 8-bit, 16-bit, other integer and float data are supported. Booleans are treated as 0/255. NaN/inf values raise ValueError.

get_hist(image, *, channels="gray", bins=256, value_range=None, density=False)

Returns int64 counts (float64 with density=True) of shape (bins,) for "gray" or (3, bins) for "rgb" (rows R, G, B).

  • value_range defaults by dtype: uint8 (0, 256), uint16 (0, 65536), other integers (min, max + 1), floats (0, 1) if all data is in [0, 1], otherwise (min, max).
  • Bin edges are np.linspace(low, high, bins + 1). Bins are half-open except the last, which includes high, and values outside the range are ignored (same as np.histogram).
  • Grayscale conversion uses ITU-R 601-2 luma (0.299 R + 0.587 G + 0.114 B), via Pillow for 8-bit data, so arrays and PIL images give identical results.

hist_stats(image, *, channels="gray", bins=256, value_range=None, percentiles=(1, 5, 25, 50, 75, 95, 99))

Returns a HistStats (or {"r": ..., "g": ..., "b": ...} for "rgb") with count, mean, std (population), min, max, median, percentiles, entropy (bits), clipped_low and clipped_high (fraction of pixels in the first and last bin). HistStats.to_dict() gives a JSON-ready dict with percentile keys like "p5".

Statistics are computed from the histogram in O(bins). They are exact for integer data with bin width 1 (the uint8 default) and approximate to bin resolution otherwise. Percentiles use the nearest-rank (inverted CDF) method.

stats_from_counts(counts, edges, percentiles=...)

The same statistics for a histogram you already have (counts of shape (bins,), edges of shape (bins + 1,)).

compare_hist(h1, h2, *, method="intersection")

Compares two histograms of the same shape and returns a float. Histograms are normalised to sum to 1 first (except for correlation), so images of different sizes compare fairly. For (channels, bins) input the per-channel scores are averaged.

method range identical definition
intersection [0, 1] 1 sum(min(p, q))
chi2 [0, 1] 0 0.5 * sum((p - q)² / (p + q))
bhattacharyya [0, 1] 0 sqrt(1 - sum(sqrt(p * q))) (Hellinger form)
correlation [-1, 1] 1 Pearson r of the raw counts
emd [0, bins - 1] 0 sum(abs(cumsum(p) - cumsum(q))), Earth Mover's distance in bins

HistAccumulator(*, channels="gray", bins=256, value_range=None)

Streams images into one histogram: update(image), merge(other) (for multiprocessing), counts (read-only), edges, n_images and stats(percentiles). Without value_range, the range comes from the first image's dtype (uint8 or uint16); float and other integer images need an explicit value_range so bins line up across images.

get_image_array(image, *, mode="gray")

Returns a PIL image as an array: "gray" gives (H, W), "rgb" gives (H, W, 3), "keep" gives np.asarray(image) unchanged. The native bit depth is kept (16-bit images give uint16).

array_to_image(arr, width=None, height=None, *, clip=True)

Builds a PIL image from a 1-D array (with width and height), an (H, W) array (mode L, or lossless I;16 for uint16) or an (H, W, 3/4) array (RGB/RGBA). Other dtypes are converted to 8 bits: floats are rounded (not truncated); values outside [0, 255] are clipped with a RuntimeWarning when clip=True, or raise ValueError when clip=False.

Migrating from 1.x

1.x 2.0
getImageArray(img), getHist(x), getImageFromArray(a, w, h) removed; use get_image_array, get_hist, array_to_image
get_hist(x) returns a list returns an np.ndarray; call .tolist() if you need a list
get_hist(x, as_density=True) get_hist(x, density=True)
get_image_array(img, ensure_grayscale=True) get_image_array(img) (mode="gray")
get_image_array(img, ensure_grayscale=False) get_image_array(img, mode="keep")
array_to_image(a, validate_range=False) array_to_image(a, clip=False)
from image_array_and_histogram.main import ... from image_array_and_histogram import ...

Behaviour changes to watch for:

  • 16-bit images are no longer reduced to 8 bits: get_image_array returns uint16 and get_hist uses the range (0, 65536). If you relied on 8-bit output, convert first with img.convert("L").
  • array_to_image now accepts (H, W, 3/4) arrays (RGB/RGBA) instead of raising, and keeps 2-D uint16 arrays as 16-bit images.

Development

git clone https://github.com/rishi-chauhan/imghist.git
cd imghist
pip install -e ".[dev]"
ruff check . && ruff format --check . && mypy
pytest --cov=image_array_and_histogram --cov-fail-under=95
python benchmarks/bench_hist.py  # optional performance check

Contributing

Contributions are welcome. See CONTRIBUTING.md.

License

MIT License. See LICENSE.

Metadata

Release files for image-array-and-histogram 2.1.1

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

Change of provenance

The repository or configuration that created these release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Source distribution (sdist)

Source distribution for image-array-and-histogram 2.1.1
File Size Uploaded Provenance Actions
image_array_and_histogram-2.1.1.tar.gz 38.5 kB With provenance Details for image_array_and_histogram-2.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for image-array-and-histogram 2.1.1
File Interpreter ABI Platform Actions
image_array_and_histogram-2.1.1-py3-none-any.whl Python 3 none any Details for image_array_and_histogram-2.1.1-py3-none-any.whl

Total release size: 66.5 kB

Release files / image_array_and_histogram-2.1.1.tar.gz

Change of provenance

This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Download URL image_array_and_histogram-2.1.1.tar.gz
Size 38.5 kB
Tags Source
SHA-256 checksum
How to use checksums
bc5f5f977a22398a97ff0757c3d20221403bf0988afd74c4a4075cc7026df03c
BLAKE2b-256 checksum
How to use checksums
6824ceec25f7afdcddd70248727efecd67995389322dfe81c420f7cffc284e0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / image_array_and_histogram-2.1.1-py3-none-any.whl

Change of provenance

This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.

We recommend inspecting the repository and configuration before proceeding. View details.

Download URL image_array_and_histogram-2.1.1-py3-none-any.whl
Size 28.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd513db16d3d8e081e6214986b7c90ed9cbcc4033fe1f723e9acd834904ecc91
BLAKE2b-256 checksum
How to use checksums
10d06e43c784b17d302f74464368c4d1f052fd172fa51c14c56e515afcee241d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for image-array-and-histogram 2.1.1

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.

Change of provenance

The repository and configuration that created this release differ from a previous release.

While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.

Select from 2 files

Attestation for image_array_and_histogram-2.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

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.

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