A quick and dirty background remover for simple images with solid(ish) background colors.
Project description
simple_background_remover
This library is meant to be a quick and dirty background remover for simple images with solid(ish) background colors. Basically, a green screen. It does not have any sort of 'foreground detection'; it simply transparent-ifies pixels which are similar to the provided (or determined) background color.
The processed files automatically get saved as PNGs.
Installation
This library is available on PyPi and can be installed with python -m pip install simple_background_remover
.
Usage
from simple_background_remover import *
simple_background_remover.remove_background(input_filename='images/myPhoto.jpg',
output_filename='images/processed/myPhoto', # Note no file extension
wiggle_room=0.15,
background_color=(0, 0, 0))
# Alternatively
from simple_background_remover.simple_background_remover import remove_background
remove_background(input_filename='images/myPhoto.jpg',
output_filename='images/processed/myPhoto', # Note no file extension
wiggle_room=0.15,
background_color=(0, 0, 0))
wiggle_room
wiggle_room
is an optional parameter between 0 and 1. It determines how similar a pixels color can be to the background color and still be considered 'background'. For example, a value of 0.2 means that pixels can be +/- 20% similar to the background color and still be considered background pixels, and therefore made transparents. Note: This 20% range is applied to the R, G, and B value of the background color, so it's not perfect. Ideally it would use redmean/Euclidean distance like the auto-generated wiggle_room
(more on that below) uses.
If wiggle_room is not passed/left as None
, the wiggle room will be auto-determined by roughly calculating the contrast between the average foreground and background color. The larger the contrast (i.e. foreground black, background white), the higher wiggle_room
can be. The smaller the constrast (i.e. foreground black, background dark gray), the lower wiggle_room
must be, otherwise you risk accidentally transparent-ifying foreground pixels.
background_color
background_color
is an optional parameter for the background color of the image (passed as an RGB color, as tuple[int, int, int]
-- i.e. (0, 0, 0) for white). If not passed background_color
will be estimated based on the average color of the top left 5% of the image. Ideally this could be flexible for the caller in the future.
Examples
- See the demos folder for some examples. The Shirt was done with auto wiggle room and background determination, and worked almost perfectly. The Apple has some bleed over. The library originally tried a wiggle room of ~0.37, but there was way too much bleed over, so I manually set it to 0.2. I also tried 0.13, which had less bleeding but a bit more background leftover.
Todo
Lots more can be done to improve this repo.
- General clean up -- I wrote this pretty quickly and many places could be cleaned up/refactored
- Improve logging to use python
logging
library - Validation on user inputs
- Improvements on the auto determination of background color and wiggle room
- Could use the redmean to determine the closeness to background per pixel instead of doing +/- wiggle room -- although may be very slow
- Allow user to pass in which corner to read background from
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
Built Distribution
File details
Details for the file simple_background_remover-1.0.1.tar.gz
.
File metadata
- Download URL: simple_background_remover-1.0.1.tar.gz
- Upload date:
- Size: 1.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80247e659db7b6648d9665ad1e573e5e95b5825bf654189109b525e3e920193f |
|
MD5 | 223cb320a7285e8312b1b705b355e723 |
|
BLAKE2b-256 | 465ee9d04d367da732d9605f603e26a6398b4b990289ec8c963bcb1a66293c4e |
File details
Details for the file simple_background_remover-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: simple_background_remover-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc886246d8b435d0e2ca9d570a264520882228660e65ace8f6d055bb42e5a5df |
|
MD5 | 185fc7ca7fd3fe21405d24b2bd8fbbc0 |
|
BLAKE2b-256 | e97ff1db9b9706533b7c60162070dca866789088cf7e714008c9ab4d6098344d |