Skip to main content

A black borders cropping module

Project description

bordercrop

A black borders cropping module

Crop the black borders from any image in a single line of code!

PyPI version PyPI - Downloads PyPI - Python Version PyPI - Status GitHub - License GitHub top language CodeQL Checks Badge Pytest Code Size Repo Size Issues

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

You will need Python 3 to use this module

# vermin output
Minimum required versions: 3.2
Incompatible versions:     2

According to Vermin (--backport typing), Python 3.2 is needed for the backport of typing but some may say that it is available for python versions higher than 3.0

Always check if your Python version works with bordercrop before using it in production

Installing

Option 1: From PyPI

pip install bordercrop

Option 2: From Git

pip install https://github.com/Animenosekai/bordercrop

You can check if you successfully installed it by printing out its version:

$ python -c "import bordercrop; print(bordercrop.__version__)"
# output:
bordercrop v1.0.0

or just:

$ bordercrop --version
# output:
bordercrop v1.0.0

Usage

You can use bordercrop in Python by importing it in your script:

import bordercrop

cropped_image = bordercrop.crop("https://i.imgur.com/lhQe6Bq.jpg")
cropped_image.show() # show the cropped image

Leaving everything with the default values, you may get images with no crop or with no size (entirely cropped).

I recommend testing and tweaking the MINIMUM_THRESHOLD_HITTING and MINIMUM_ROWS values according to your picture (a big picture needs more minimum rows and more pixels to hit before counting a row as dark).

CLI usage

You can use bordercrop in other apps by accessing it through the CLI version:

$ bordercrop --image "https://i.ytimg.com/vi/e_53PHZwQH4/hqdefault.jpg" --output "cropped.jpg"
output

As a Python module

The given image can be a filepath (str, bytes or Path), an URL (which will be downloaded) or a PIL.Image.Image instance

crop()

Autocrops the black borders from the given image

Args:
image: the given image, it can be a filepath (str, bytes or Path), an URL (which will be downloaded), a BytesIO or a PIL.Image.Image instance
THRESHOLD: the black threshold (0 is black while 225 is white)
MINIMUM_THRESHOLD_HITTING: the number of pixels in a row which needs to hit the threshold to count the row as dark (ex: if 5 is given and there is only 4 black pixels in the row, the row won't be counted as black)
MINIMUM_ROWS: the minimum row requirement to count a border (ex: if set to 3, 3 rows need to consecutively be black to count it as a border)

Returns:
A PIL.Image.Image instance containing the cropped image

borders()

Gives back the bounding box of the image without the black borders

Args:
image: the given image, it can be a filepath (str, bytes or Path), an URL (which will be downloaded), a BytesIO or a PIL.Image.Image instance
THRESHOLD: the black threshold (0 is black while 225 is white)
MINIMUM_THRESHOLD_HITTING: the number of pixels in a row which needs to hit the threshold to count the row as dark (ex: if 5 is given and there is only 4 black pixels in the row, the row won't be counted as black)
MINIMUM_ROWS: the minimum row requirement to count a border (ex: if set to 3, 3 rows need to consecutively be black to count it as a border)

Returns:
A tuple containing the coordinates (LEFT, TOP, RIGHT, BOTTOM)

get_image()

Gives back the given image as a PIL.Image.Image instance

Mostly used internally, it can be useful sometimes.

Args:
image: the given image, it can be a filepath (str, bytes or Path), an URL (which will be downloaded), a BytesIO or a PIL.Image.Image instance

Returns:
A PIL.Image.Image instance containing the given image

Exceptions

All of the excpetions inherit from the BordercropException

A WrongType exception can be raised if the wrong type of image is given

Deployment

This module is currently in development and might contain bugs.

Feel free to use it in production if you feel like it is suitable for your production even if you may encounter issues.

Built With

  • Pillow - To manipulate images

Authors

License

This project is licensed under the MIT License - see the LICENSE file for more details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bordercrop-1.0.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bordercrop-1.0.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file bordercrop-1.0.0.tar.gz.

File metadata

  • Download URL: bordercrop-1.0.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for bordercrop-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2cfd078f8214fcecc304ee9bc8e96b38c9decc3db96ee5301e31e60678322990
MD5 ec000ea49de2644da640709bbc865e55
BLAKE2b-256 40d726a0c473fec3f176ba65270768b0ecea867c84cd4471e493de9a000e2367

See more details on using hashes here.

File details

Details for the file bordercrop-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bordercrop-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for bordercrop-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50342a4a7d3b37bd1188faf3bedcb4d4b264c3d7cc51a59d082d3afeaab86c0f
MD5 23cc8ad4f1f6e35b96eecfc928a4e344
BLAKE2b-256 236124d23316a96240628c5563bd5ed33725dc8afa6d1595f2e1128e194ce38e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page