image-array-and-histogram
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_rangedefaults 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 includeshigh, and values outside the range are ignored (same asnp.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_arrayreturns uint16 andget_histuses the range(0, 65536). If you relied on 8-bit output, convert first withimg.convert("L"). array_to_imagenow 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/my-packages.git
cd my-packages
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.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| image_array_and_histogram-2.1.0.tar.gz | 38.4 kB | With provenance | Details for image_array_and_histogram-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| image_array_and_histogram-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for image_array_and_histogram-2.1.0-py3-none-any.whl |
Total release size: 66.5 kB
Release files / image_array_and_histogram-2.1.0.tar.gz
| Download URL | image_array_and_histogram-2.1.0.tar.gz |
|---|---|
| Size | 38.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4802b3a4999f0f52b40b5c2be64b36b044fc9a24d2a6ac4c49e7fcacba03d369
|
|
BLAKE2b-256 checksum How to use checksums |
c7f003149eb6591e2ce231b256ed10a6fee9e1c2396f02a8963aed2160af1d5b
|
| 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 logRelease files / image_array_and_histogram-2.1.0-py3-none-any.whl
| Download URL | image_array_and_histogram-2.1.0-py3-none-any.whl |
|---|---|
| Size | 28.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e77a0024fa6b1ac6cb1f767eec4fe69ec21f6217cf88b1cee7a0b19017db3350
|
|
BLAKE2b-256 checksum How to use checksums |
12bbe31df77e73062bff3b22774939e3127bc882c8812d7d92319046d74d1264
|
| 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 logSecurity for image-array-and-histogram 2.1.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for image_array_and_histogram-2.1.0.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 logA 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.