Skip to main content

Make images with transparent background

Project description

Transparent Background

This is a background removing tool powered by InSPyReNet (ACCV 2022). You can easily remove background from the image or video or bunch of other stuffs when you can make the background transparent!

Image Video Webcam

:inbox_tray: Installation

Dependencies (python packages)

package version (>=)
pytorch 1.7.1
torchvision 0.8.2
opencv-python 4.6.0.66
timm 0.6.11
tqdm 4.64.1
kornia 0.5.4
gdown 4.5.4
pyvirtualcam 0.6.0

Note: If you have any problem with pyvirtualcam, please visit their github repository or pypi homepage. Due to the backend workflow for Windows and macOS, we only support Linux for webcam input.

Dependencies (webcam input)

# Install v4l2loopback for webcam relay
$ git clone https://github.com/umlaeute/v4l2loopback.git && cd v4l2loopback
$ make && sudo make install
$ sudo depmod -a

# Create virtual webcam
$ sudo modprobe v4l2loopback devices=1

Note: If you have any problem with installing v4l2loopback, please visit their github repository.

Install transperent-background

# via pypi
$ pip install transparent-background

# via github
$ pip install git+https://github.com/plemeri/transparent-background.git

# locally
$ pip install -e .

:pencil2: Usage

:computer: Command Line

$ transparent-background --source [SOURCE] --dest [DEST] --type [TYPE] --fast

# for apple silicon mps backend (requires torch >= 1.13)
$ PYTORCH_ENABLE_MPS_FALLBACK=1 transparent-background --source [SOURCE] --dest [DEST] --type [TYPE] --fast
  • --source [SOURCE]: Specify your data in this argument.

    • Single image - image.png
    • Folder containing images - path/to/img/folder
    • Single video - video.mp4
    • Folder containing videos - path/to/vid/folder
    • Integer for webcam address - 0 (e.g., if your webcam is at /dev/video0.)
  • --dest [DEST] (optional): Specify your destination folder. If not specified, it will be saved in current directory.

  • --type [TYPE]: Choose between rgba, map green, blur, overlay, and another image file.

    • rgba will generate RGBA output regarding saliency score as an alpha map. Note that this will not work for video and webcam input.
    • map will output saliency map only.
    • green will change the background with green screen.
    • blur will blur the background.
    • overlay will cover the salient object with translucent green color, and highlight the edges.
    • Another image file (e.g., backgroud.png) will be used as a background, and the object will be overlapped on it.
  • --fast: Fast mode. If specified, it will use low-resolution input and model trained with LR scale. May decrease performance but reduces inference time and gpu memory usage.

    Examples of different TYPE argument choices

:crystal_ball: Python API

  • Usage Example
import cv2

from PIL import Image
from transparent_background import Remover

remover = Remover()

# Usage for image
img = Image.open('samples/aeroplane.jpg') # read image

out = remover.process(img) # default setting - transparent background
out = remover.process(img, type='rgba') # same as above
out = remover.process(img, type='map') # object map only
out = remover.process(img, type='green') # image matting - green screen
out = remover.process(img, type='blur') # blur background
out = remover.process(img, type='overlay') # overlay object map onto the image
out = remover.process(img, type='samples/background.jpg') # use another image as a background

Image.fromarray(out).save('output.png') # save result

# Usage for video
cap = cv2.VideoCapture('samples/b5.mp4') # video reader for input
fps = cap.get(cv2.CAP_PROP_FPS)

writer = None

while cap.isOpened():
    ret, frame = cap.read() # read video

    if ret is False:
        break
        
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 
    img = Image.fromarray(frame).convert('RGB')

    if writer is None:
        writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size) # video writer for output

    out = remover.process(img, type='map') # same as image, except for 'rgba' which is not for video.
    writer.write(cv2.cvtColor(out, cv2.COLOR_BGR2RGB))

cap.release()
writer.release()

:outbox_tray: Uninstall

pip uninstall transparent-background

:page_facing_up: Licence

See LICENCE 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

transparent-background-1.1.2.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

transparent_background-1.1.2-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file transparent-background-1.1.2.tar.gz.

File metadata

  • Download URL: transparent-background-1.1.2.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.15

File hashes

Hashes for transparent-background-1.1.2.tar.gz
Algorithm Hash digest
SHA256 4300d9208bc8b91e40a06ed259729d562f275665cb6e3f71776cea6234c1c4f5
MD5 e04ea8726d04f04ad907a49b6968f873
BLAKE2b-256 9284b2f626f21944cb2caca63474e297003d4e528951499dd650242b3fc6deb5

See more details on using hashes here.

File details

Details for the file transparent_background-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for transparent_background-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cf4e0b1c935dd5271b69209ee79aa377257e72d9b18b64ef1e99c62661fcddb8
MD5 535f0f39e3a1e268f7053786401f2848
BLAKE2b-256 a8d224445d75e2243d0227874c012ab2bbfecc0d8101b717b147bd3aa9034570

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