Science and technology

Take away the background from a picture with this Linux command

You have an incredible image of your self and wish to use it on your social media profile, however the background is distracting. Another image has an ideal background on your profile image. How do you mix the 2? Some smartphone apps that do this sort of picture manipulation however are too costly or are riddled with ad-ware. And they don’t seem to be open supply. Rembg is as much as the problem!

Rembg is written in Python, so set up Python 3 in your laptop. Most Linux distributions embody Python 3 by default. You can examine your model with this easy command:

$ python3 --version

Rembg requires no less than Python 3.7 and no better than Python 3.11. In my case, I’ve Python 3.10.6 put in.

Install Rembg on Linux

I created a listing referred to as PythonCoding on my Linux laptop computer after which made a Python digital surroundings:

$ python3 -m venv /house/don/PythonCoding

Next, I put in rembg utilizing pip:

$ python3 -m pip set up rembg

Combine pictures

Time to work some magic. First, I selected the picture containing an image taken at All Things Open in 2019.

(Don Watkins, CC BY-SA 4.0)

I ran the next rembg command to rename it with a shorter filename for comfort:

$ rembg i dgw_ato.jpeg dgw_noback.jpg

The first time you run rembg, it downloads an open supply pattern recognition model. This could be over 100 MB and rembg saves it in your person listing as ~/.u2net/u2net.onnx. The mannequin is the U-2-Net and makes use of the Apache 2.0 license. For extra details about the sample recognition fashions (together with tips on how to prepare your personal), learn the Rembg documentation.

It created my new picture with out the background in about ten seconds. I’ve a Ryzen 7 with 16 GB of RAM. Your expertise could range relying in your {hardware}.

(Don Watkins, CC BY-SA 4.0)

I’ve used GIMP to take away backgrounds previously, however rembg does it faster and extra fully than I’ve skilled with GIMP.

That’s all there’s to eradicating a background. What about including a brand new one?

Add a brand new background

Next, I wish to add a brand new background to the image. There are alternative ways to do this. You can, for example, mix pictures with ImageMagick, however getting the body measurement proper could be advanced. The simplest way is to make use of GIMP or Krita.

I used GIMP. First, open the newly created picture (ato_image.jpg in my case). Now go to the File menu and choose Open as layers. Choose a distinct picture for the background. This picture opens as an overlay above the prevailing picture.

I wished to maneuver the brand new background under my portrait. On the proper of the GIMP window are two thumbnails, one for every picture layer. The background layer is on high. I dragged the background layer beneath my portrait picture, and here is the consequence:

(Don Watkins, CC BY-SA 4.0)

That’s a a lot nicer setting for my profile image!

Try Rembg

Rembg has three subcommands you may assessment within the --help menu:

$ rembg --help

They are:

  • rembg i for information
  • rembg p for folders
  • rembg s for HTTP server

Rembg is launched with an MIT license. Try it the subsequent time you want a background faraway from a picture.

Most Popular

To Top