Entropy-based image border detection algorithm

Project description

ENtropy-based IMage border Detection Algorithm: finds out if your image has borders or whitespaces around and helps you to trim border providing whitespace offsets for every side of a picture.

Algorithm

For each side of the image starting from the top, clockwise:

• Get upper block with 25% height (indent) of the dimension opposite to current side
• Get lower block with the same height as the upper one (50% of image total)
• Calculate entropy for both blocks
• Find their entropies difference
• Make upper block 1px less
• Repeat from p.2 until we hit image edge
• Get maximum (minimum) of the entropies difference
• Here we have a border center if it lies closer to the edge rather than to the center of image and entropies difference is lower than pre-set threshold

Python 3.5+

Setup

```pip install enimda
```

Usage

Find if image has any borders:

```from enimda import ENIMDA

# Open target image, convert it to grayscale and resize too 300 px
image = ENIMDA(path='test.jpg', mode='L', resize=300)
# Scan for borders existence
image.scan(threshold=0.5, indent=0.25)
# Save image with outlined borders for demonstration
image.save(path='test-outlined.jpg', outline=True)
# Print found image borders (tuple)
print(image.borders)
```

Detect borders with high precision (iterative):

```from enimda import ENIMDA

# Open target image, convert it to grayscale and resize too 300 px
image = ENIMDA(path='test.jpg', mode='L', resize=300)
# Detect borders
image.detect(threshold=0.5, indent=0.25)
# Save image with outlined borders for demonstration
image.save(path='test-outlined.jpg', outline=True)
# Print found image borders (tuple)
print(image.borders)
```

Demo

For demonstration please refer to ENIMDA Demo

Release history Release notifications | RSS feed

Files for enimda, version 1.0.0b2
Filename, size File type Python version Upload date Hashes
Filename, size enimda-1.0.0b2-py3-none-any.whl (5.1 kB) File type Wheel Python version py3 Upload date Hashes
Filename, size enimda-1.0.0b2.tar.gz (4.0 kB) File type Source Python version None Upload date Hashes