remove background image command-line

Good morning!

I downloaded gmic to remove the photo from the image through commands in the windows cmd.

I tried that way but I couldn’t

gmic “foto1.jpg” -remove[0] [1],0,0 -o “foto1-removebg.png”

[gmic]-0./ Start G’MIC interpreter.
[gmic]-0./ Input file ‘foto1.jpg’ at position 0 (1 image 720x1280x1x3).
[gmic]-1./ Remove image [0] (0 images left).
[gmic]./ *** Error *** Command ‘input’: Invalid selection ‘[1]’ (no item available).

what could i be doing wrong?

Grateful

Greetings @Rodrigo_Baltazar_Ogi. Welcome to the forum.

Perhaps you have just begun using the command line tool, gmic. It can present considerable challenges to those unfamiliar with its workings. Start Here provides a very basic introduction. The commands which can be used to construct image processing pipelines that operate on image lists are completely, if tersely, covered in the List of Commands.

What you have done with this pipeline is:

  1. Placed an initial image on the image list called foto1.jpg. The processing log (lines beginning with [gmic]) informs you that it has been input onto the image list and describes its shape: 720 pixels wide, 1280 pixels high, it has one slice or frame, and that such slices each have three channels — typically, but not necessarily the red, green and blue channels (3). See Images for an introduction to gmic image terminology.
  2. You then invoked the remove command, which removes selected images from the image list. “Selected images”, those designated as the ones on which the remove command operates, appear in the square-bracket notation appended to the remove command. In this particular case, the first (and only) image on the list is foto1.jpg — which you have selected — as zero (0), the index to the first image on the list, is within the square brackets; the remove command dutifully removes this selected image. There are now no images on the list.
  3. remove takes no arguments, but you have provided some: [1],0,0. Because of this, I am led to suspect that you have mixed up remove with some other command. gmic cannot guess your intents, however, and is expecting a third command (having completed remove). Getting an argument list instead, gmic guesses that you are attempting to input something new. This is a mistaken guess, of course (a nice try that didn’t work); that is the basis of the error message you have received.

I am guessing, from the foto1-removebg.png output file name, that your real intent is to detect (and remove) the background regions of foto1.jpg. Is the command line equivalent of Easy Foreground Extraction in GIMP with the G’MIC Plug-in what you want?

2 Likes

That’s right! I want to remove the background from the image and make it either all white or transparent. I couldn’t understand which command I should use.
Thanks for the help.

Whilst I almost always limit my commentary here to the G’MIC command line tool, and which of its commands might do what, on the subject of background detection and removal, almost every suggestion I could make entail some sort of interactive dialog. The reason for that is plain:


Homage To Women, Mico Kaufman. Downtownstreets, photographer, Market Street Park, Lowell, Massachusetts

Intelligent folk such as you or I intuitively know what pixels in the image make up the statue and what make up the “not-statue” (background); automation has no such intuition. Automation only has an array of pixels, here comprising of so-called “red”, “green” and “blue” components. There are no indicators identifying statue pixels, brick walkway pixels, window glass pixels, and so forth. If I had a collection of images such as Homage to Women and the task to separate background from foreground, I would despair at coming up with any numerical rule set that could be trusted in automation to distinguish a background pixel from a foreground. Instead, I would resort to some intelligently guided interactive tool:

  1. Use GIMP’s AI Technology To Remove Backgrounds Easily Intelligent scissors are native to GIMP.
  2. Gimp Tutorial: Remove Background From Image Using GMIC In Gimp G’MIC interactive approach. By designating enough foreground regions (green points) and background regions (red points), you can — to a fairly high degree, if not to perfection — separate out arbitrarily complex backgrounds from similarly complex foregrounds, using the “Contours ⇒ Extract Foreground [Interactive]” filter in the G’MIC-Qt plug-in.

These approaches require judgement to directly or indirectly detect an edge separating background from foreground.

But if hundreds of images are in play, then an interactive background-foreground approach loses appeal. Automation might come to the rescue — perhaps. If the “background pixels” exhibit some identifiable numerical property — all the same or nearly the same color, or all with low local variance, which blurred backgrounds manifest — and if such “continuous regions” very nearly encompass all other regions, then G’MIC’s label command can likely tag background pixels (probably assisted by a separate “detection filter”). Pixels, so tagged, provide a basis making a background mask. The background mask cuts the image. A rough approach follows:

cutplainbkgnd.gmic

cutplainbkgnd:
   -foreach {
      nm={n}
      -name. current
      # Force an alpha channel
      -to_rgba[current]
      # Assume ≈constant color
      # cartoon-like images.
      # Background gets zero label
      +label[current] 30,1,1
      # Not zero? Opaque. Otherwise clear
      -neq. 0
      -mul. 255
      -name. mask
      # Replace alpha channel with mask
      -shared[current] {s#$current-1}
      -fill. [mask]
      # Cleanup pipeline
      -keep[current]
      -name. $nm
   }

$ gmic -command cutplainbkgnd.gmic -input_glob *.png cutplainbkgnd -output cutback_.png

Example:

gmic -command cutplainbkgnd.gmic -input Downloads/rocketship.png +cutplainbkgnd. display_rgba. d , _parse_cli_images 1 append x o /dev/shm/pipeline.jpg,65

cutplainbkgnd.gmic automates well for flat color images with all-encompassing backgrounds. Photographs are orders of magnitude more nuanced. For such, a batch process is likely to be not very reliable.

Good afternoon! Thanks for the answer.
I need something that works with windows CMD, with a command line. Because I will not run in gimp. I’m going to run it on another system where I’m going to choose the image, and process it removing the background. Would it be possible?
Grateful

Hi,

this is indeed a CLI solution. Just copy/paste (without last line) the script in a cutplainbkgnd.gmic file, then you can call it like this (if you cd to the path where you save the .gmic file and put all your PNG images there too):

gmic -command cutplainbkgnd.gmic -input_glob *.png cutplainbkgnd -output cutback_.png

Syntax should be quite the same on windows i think.

Good morning!! Thanks for the answer!

I applied what you told me, now it didn’t give any error, however, the result was a little strange.
I will send the photo.
Grateful

It’s probably because some edges on the phone are about the same color as the background.

Try changing the value of this line in the file:
+label[current] 30,1,1
30 is the tolerance value, I think that the higher you set it, the more pixels will be labeled as background, so maybe you should lower it?

@Rodrigo_Baltazar_Ogi Welcome to the forum!

The best way to learn is to provide us an example image or the one you are actually working on. That way we are on the same page and we can show you how we would approach the problem.

Good morning!! Thanks for the reply,

Decreased as indicated, and it worked at first, with a white background. But testing it on another photo, with a background of a landscape for example, it didn’t work. Would there be any way to remove only the background of the landscape?
Grateful