Command line? No idea.
You can however do foreground select using the tool in Gimp and invert the selection clearing the background (make sure your image has an alpha channel if you want a transparent background after) . Or if you have GMIC there is a similar filter called Extract foreground (Interactive). You can also do it with masks in gimp.
Edit: Also forgot about fuzzy wand selector

or same colour

if the image you want to keep doesn't have the same colour as the background in it
_________________