39

I have an image in .jpg format with white background color. I want to remove the white background color to transparent in Imagemagick. I tried many ways but still the white background can not be removed. Can some one help me to solve this.

NewUser
  • 11,759
  • 36
  • 127
  • 221
  • What did you try? You may have to convert it to "indexed color" mode (e.g. PNG) first. Regardless of how you do it, you can expect artifacts to appear at the boundary between image and transparent. – msw Sep 14 '12 at 13:39
  • Transparent to white: http://stackoverflow.com/questions/2322750/replace-transparency-in-png-images-with-white-background – Ciro Santilli新疆棉花TRUMP BAN BAD Oct 05 '15 at 22:01

4 Answers4

66

You cannot have transparent background colors in your JPEGs. The JPEG file format doesn't support transparency.

If you need transparent background, you need to convert the JPEG to

  • either PNG (high quality, filesize possibly larger than JPEG)
  • or GIF (in case you can tolerate low quality and a range of maximally 255 colors).

Example command:

convert  your.jpg  -transparent white  your.png
Kurt Pfeifle
  • 78,224
  • 20
  • 220
  • 319
  • 1
    i must have read your post, and not your actual command; thought you were suggesting "manually converting" sorry for the mix-up, I voted up your answer. I'm not very familiar with the syntax but i am with its capabilities. – Jim22150 Dec 17 '12 at 16:57
  • This works great...but if you have any white in the image then it'll convert that to be transparent as well. You could work around that by manually filling in the background to another color then making that color transparent. I don't think this is what OP is looking for but I'll probably use a non-CLI tool like https://www.remove.bg – Brady Dowling Dec 29 '19 at 17:05
57

First, you need to convert the image format from .jpg to .png format, because JPEG does not support transparency. Then use this command:

convert image1.png -fuzz 20% -transparent white result.png

The -fuzz option allows the specified percentage deviation from the pure white colour to be converted to transparent as well. This is useful, for example, when your image contains noise or subtle gradients.

Erik
  • 3,314
  • 27
  • 45
NewUser
  • 11,759
  • 36
  • 127
  • 221
  • 17
    What was wrong with my answer? (You didn't tell in your question that you required some degree of fuzziness, nor did you provide a sample file -- so it was impossible to guess...) – Kurt Pfeifle Sep 17 '12 at 21:12
  • 8
    It's not necessary to convert the files *first* from JPEG to PNG, and only *then* convert white to transparent. My command does it all in *one* go. – Kurt Pfeifle Sep 17 '12 at 21:14
  • 2
    @KurtPfeifle I understand he could have helped you improve your answer instead of answering his own; but without the `-fuzz 20%` it did not work here; I think your answer could be improved with the fuzz option btw, I see absolutely no problem on doing that; also his answer is not completely right as I could go straight from a jpg to a png result; and at last, both answers brings a serious problem; the contour background color (that is black here) becomes transparent; but also all black inside the actual image becomes transparent too; what actually messed my image... :> – Aquarius Power Jun 30 '14 at 19:52
10

I just found a very neat thing!

magicwand 1,1 -t 20 -f image -r outside -m overlay -o 0 image.jpg imgOutput.png

It is a Fred Weinhaus bash script that can be downloaded from here (for non commercial use only). Also there has about 250 scripts!! and this one is amazing! it did exactly the trick, to remove all background while keeping the inner image dots untouched!

At his page, there are several images as examples so you pick what you need to put on the command line!

The initial position 1,1 is a general guesser saying all the contour is background.

Pay attention that the output must be ".png"

Aquarius Power
  • 3,212
  • 5
  • 26
  • 56
2

This is my solution without magicwand (replace magick by convert for im < 7.0):

magick img.png -fuzz 20% -fill none -draw "alpha 1x1 floodfill" result.png
roipoussiere
  • 2,808
  • 1
  • 18
  • 29
  • 2
    at ubuntu imagemagick v8:6.7.7.10 - error `convert.im6: non-conforming drawing primitive definition 'alpha' @ error/draw.c/DrawImage/3158.`, any idea? – Aquarius Power Mar 23 '17 at 23:05
  • Here is the right command for imagemagick 6: convert "./input.jpg" -fill none -draw 'color 1,1 floodfill' output.png – The Flying Dutchman Jan 07 '18 at 01:01