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.
Usage
from simple_background_remover import remove_background
remove_background(input_filename='images/myPhoto.jpg',
output_filename='images/processed/myPhoto',
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
- To do: post some images
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
- 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
Hashes for simple_background_remover-0.0.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 608c2b3bd4972f314f83e3f0a28cbd101df4bf5ef9713a4537b0d9eb442dd7e8 |
|
MD5 | e85e76fbfef825197d9f27f32a9045a4 |
|
BLAKE2b-256 | 6480eb629cffadd3333d132c55b3ef4e379cf9db5632658633c2355f43865957 |
Hashes for simple_background_remover-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40db15e02c925c15c27fecf55c5074ee44acf5adbb7b9a55ae726003bc070b27 |
|
MD5 | 55f0b56f5f06bbc7ae3e3e6e87e937b9 |
|
BLAKE2b-256 | 34baf30ab59dbf898e5aa52a3d9ef47ac93b0c15bc8ad52c800270cc1a2f041d |