Skip to main content

Document whitening (foreground separation)

This package tries to separate text/line foreground and background by 2D median filter.

original foreground background

Installation

Install from PyPI. Works on Python 3.

pip install whitening

Example usage

Python API

It works with images represented as PIL.Image or as a numpy array. Images can be either RGB or grayscale.

import numpy as np
import PIL.Image

from whitening import whiten

# possible to use numpy array as input/output
image = np.asarray(PIL.Image.open('image.jpg'), dtype='uint8')
foreground, background = whiten(image, kernel_size=20, downsample=4)
PIL.Image.fromarray(foreground).save('foreground.jpg', 'jpeg')

# or directly a PIL image
image = PIL.Image.open('image.jpg')
foreground, background = whiten(image, kernel_size=20, downsample=4)
foreground.save('foreground.jpg', 'jpeg')

CLI

It install an entry point called whiten.

# help
$ whiten -h

# whiten an image and save the foreground output
$ whiten input.jpg foreground.jpg

# specify the kernel size
$ whiten input.jpg foreground.jpg -k 100

# work in grayscale instead of RGB (3x faster)
$ whiten input.jpg foreground.jpg -g

# downsample the image 4x (faster, but a bit less precise)
$ whiten input.jpg foreground.jpg -d 4

# save also the background
$ whiten input.jpg foreground.jpg -b background.jpg

We assume the original images is a product of foreground and background, thus we can recover the foreground by dividing the image by the background: I = F * B => F = I / B. We try to approximate the background by 2D median filtering the original image which suppresses sparse features such as text and lines.

Select kernel size that's enough for not making artifacts while small enough to keep computation fast. A good starting point is 50 pixels.

A 9.5 Mpx image can be processed on a MacBook in 15 s, with grayscale and downsampling 4x the run time can be reduced to 1 s! Quite good results can be obtained even with kernel size 10 and downsampling 16x.

More info: http://bohumirzamecnik.cz/blog/2015/image-whitening/

Development

See the Makefile for various development tasks.

License

Author: Bohumír Zámečník bohumir.zamecnik@gmail.com

Supported by Rossum, creating a world without manual data entry.

Metadata

Release files for whitening 0.2

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

Source distribution (sdist)

Source distribution for whitening 0.2
File Size Uploaded Provenance
whitening-0.2.tar.gz 4.7 kB Without provenance Details

Release files / whitening-0.2.tar.gz

Download URL whitening-0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0f19f4e41b37c08ecef1ad7f262322c632e532475113aa7e03e5c544cc55a5ef
BLAKE2b-256 checksum
How to use checksums
5d61eb6442e547f6e429fe325955e6bd4e33b27095156eb4db4579e56fe0561c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Security for whitening 0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.2 This release

1 release file No provenance

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