NewUser
NewUser

Reputation: 13323

How to replace white background color with transparent of an image in ImageMagick?

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.

Upvotes: 60

Views: 57028

Answers (5)

Antomur
Antomur

Reputation: 81

Get the background automatically and remove it :

bg=$(convert input.png -format "%[pixel:p{0,0}]" info:)
convert input.png -fuzz 20% -transparent "$bg" output.png

Upvotes: 5

Kurt Pfeifle
Kurt Pfeifle

Reputation: 90335

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

Upvotes: 86

NewUser
NewUser

Reputation: 13323

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.

Upvotes: 84

roipoussiere
roipoussiere

Reputation: 5976

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

Upvotes: 2

Aquarius Power
Aquarius Power

Reputation: 3985

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"

Upvotes: 15

Related Questions