Skip to content

API reference

immaculate.find_issues

find_issues(
    input_dir: str
    | PathLike[str]
    | Iterable[str | PathLike[str]],
    issues: Iterable[Check] | None = None,
    *,
    progress: Callable[[int, int], object] | None = None,
) -> Issues

Find duplicates and other issues in a folder of images or a list of image files.

input_dir is either a folder, searched recursively for image files (bmp, gif, jpeg, jpg, png, tif, tiff, webp), or an iterable of image file paths, which are used as given whatever their extension; a path listed twice counts once.

issues lists the checks to run, each with its settings, such as Dark() or Dark(brightness=0.2). By default every check runs with its default settings:

  • ExactDuplicates: images with identical pixels.
  • NearDuplicates: near-duplicate images, such as resized or flipped copies.
  • Dark and Light: images that are almost all dark or light.
  • Blurry: blurry images.
  • LowInformation: images with few distinct colors, such as blank images.
  • OddAspectRatio: unusually long or tall images.
  • OddSize: images much larger or smaller than the others.
  • Grayscale: grayscale images.
  • LowContrast: washed-out, foggy or otherwise flat-looking images.
  • Truncated: images whose files end early, such as interrupted downloads.

Each image is decoded once, however many checks run. The Issues returned has a field for each check, named after it, which is None if the check didn't run. Duplicate sets hold two or more paths, sorted, and are ordered by their first path.

progress, if given, is called as progress(done, total) about every 0.1 seconds while images are checked, where done of the total images found are finished, and once more when the search succeeds. It is only called when done has changed. If it raises, the search stops and the exception is raised. Pressing Ctrl-C also stops it.

Raises FileNotFoundError or NotADirectoryError if input_dir is a path but not a folder, OSError if part of the folder cannot be read (listed files that cannot be read are skipped instead), ValueError for a check given twice, TypeError if issues holds anything that isn't a check, ModuleNotFoundError if the "sscd" or "sscd_distilled" method is used without the sscd extra, and RuntimeError if ONNX Runtime can't load the model.

Immaculate.

IssueName

IssueName = Literal[
    "exact_duplicates",
    "near_duplicates",
    "dark",
    "light",
    "blurry",
    "low_information",
    "odd_aspect_ratio",
    "odd_size",
    "grayscale",
    "low_contrast",
    "truncated",
]

A check's name, as in its name, the fields of Issues and the command line.

Check

Check = (
    ExactDuplicates
    | NearDuplicates
    | Dark
    | Light
    | Blurry
    | LowInformation
    | OddAspectRatio
    | OddSize
    | Grayscale
    | LowContrast
    | Truncated
)

A check find_issues can run, with its settings.

Method

Method = Literal['pdq', 'phash', 'sscd', 'sscd_distilled']

How near duplicates are found; see NearDuplicates.

Issues dataclass

What find_issues found, with a field named after each check. A check's field is None if it didn't run, and otherwise holds paths, sorted.

exact_duplicates instance-attribute

exact_duplicates: list[list[Path]] | None

Sets of images with identical pixel data; see ExactDuplicates.

near_duplicates instance-attribute

near_duplicates: list[list[Path]] | None

Sets of near-duplicate images; see NearDuplicates.

dark instance-attribute

dark: list[Path] | None

Dark images; see Dark.

light instance-attribute

light: list[Path] | None

Light images; see Light.

blurry instance-attribute

blurry: list[Path] | None

Blurry images; see Blurry.

low_information instance-attribute

low_information: list[Path] | None

Images with little information; see LowInformation.

odd_aspect_ratio instance-attribute

odd_aspect_ratio: list[Path] | None

Images with an odd aspect ratio; see OddAspectRatio.

odd_size instance-attribute

odd_size: list[Path] | None

Images with an odd size compared to the others; see OddSize.

grayscale instance-attribute

grayscale: list[Path] | None

Grayscale images; see Grayscale.

low_contrast instance-attribute

low_contrast: list[Path] | None

Images with low contrast; see LowContrast.

truncated instance-attribute

truncated: list[Path] | None

Images whose files end early; see Truncated.

skipped instance-attribute

skipped: list[Path]

Images that could not be read or decoded, or are empty, whatever checks ran. No other field includes them.

to_dict

to_dict() -> dict[str, list[str] | list[list[str]]]

The fields of the checks that ran, and skipped, with paths as strings, ready for json.dumps.

ExactDuplicates dataclass

Find sets of images with identical pixel data, so the same image saved with different metadata or in a different lossless format still matches.

NearDuplicates dataclass

Find sets of near-duplicate images, as matched by method:

  • "pdq" (default): images whose 256-bit PDQ hashes (Meta's perceptual hash) differ in at most 31 bits, in any rotation by a multiple of 90° or flip. Catches resized, recompressed, rotated and flipped copies. Matches chain, so if A matches B and B matches C, all three are one set.
  • "phash": images with identical 64-bit perceptual hashes, as in the imagehash library. Catches resized and recompressed copies.
  • "sscd": images whose embeddings from Meta's SSCD copy detection model have cosine similarity of at least 0.75. Catches heavier edits than hashes, such as crops and overlays, but is much slower. The model, quantised to int8, and ONNX Runtime, which runs it, come with the sscd extra: pip install "immaculate[sscd]". Matches chain, as with "pdq".
  • "sscd_distilled": as "sscd", with the same 0.75 threshold, but using a small MobileNetV4 model trained to reproduce SSCD's embeddings. About 4x faster than "sscd", at some cost in recall.

When exact duplicates are found too, near-duplicate sets identical to an exact set are left out.

Dark dataclass

Find dark images: those where fewer than fraction of the pixels are at least brightness bright.

A pixel's brightness, from 0 to 1, is sqrt(0.241 R² + 0.691 G² + 0.068 B²) / 255.

Light dataclass

Find light images: those where fewer than fraction of the pixels are at most brightness bright, measured as in Dark.

Blurry dataclass

Find blurry images: those whose sharpness is below sharpness.

Sharpness is the standard deviation of the Laplacian (4 times each pixel less its 4 neighbours) of the image in grayscale, shrunk to at most 256 pixels on its longer side by averaging. The default flags images blurred by a Gaussian of about 2 of those pixels, but also some smooth, hazy scenes. Images too flat to have edges, which have low information instead, and images under 3 pixels wide or tall are never blurry.

LowInformation dataclass

Find images with low information: those whose color histogram has an entropy below entropy bits.

The histogram is the red, green and blue channels' 256-bin histograms joined into one, or the single gray channel's for grayscale images, as Pillow's Image.entropy measures.

OddAspectRatio dataclass

Find images with an odd aspect ratio: those whose shorter side divided by their longer side is below ratio.

OddSize dataclass

Find images with an odd size compared to the others: those whose size, the square root of the area, is more than iqr_factor interquartile ranges below the first quartile of all the images' sizes, or above the third. If at least half the images have the same size, any image of a different size is odd.

Grayscale dataclass

Find grayscale images: those stored as grayscale, or whose pixels' red, green and blue values all differ by at most tolerance.

LowContrast dataclass

Find images with low contrast, such as washed-out or foggy ones: those whose RMS contrast, the standard deviation of their grayscale values divided by 255, is below contrast.

Grayscale values are computed as by Pillow's convert("L").

Truncated dataclass

Find truncated images, such as interrupted downloads: those whose file ends before its end-of-image marker (JPEG), its IEND chunk (PNG) or the length its header gives (WebP). Data after the end, such as a motion photo's video, doesn't matter.

Truncated JPEGs still decode, with whatever is missing filled in gray, so they are checked like other images too. Truncated files in other formats usually fail to decode, so they are skipped instead.