Page 1 of 1

"-background transparent" not working

Posted: 2014-07-03T00:22:46-07:00
by yugalgupta
Hi,

We are using ImageMagick-6.8.9-4-Q16 on RHEL Linux machine. Our objective is to turn the background of our image to transparent. For this we have tried the following command.

Code: Select all

convert original.jpg -background transparent transformed.png
however this is not effective in making the transparent background for the images

When we try to use

Code: Select all

convert original.jpg -background RGB(0,0,0) transformed.png
we get the following error message:

Code: Select all

syntax error near unexpected token `('
We alternatively tried the following command:

Code: Select all

convert original.jpg -transparent white transformed.png
This did convert the white background to transparent for images with white backgrounds. However there is a risk that in case the background is not exactly white it may not change. As well as if an image has any white(rgb(255,255,255))content, it shall also be made transparent, which is not desired.

Request you to help find a solution for changing any type of background in the image to transparent without affecting any of the main image content using imagemagick.

Thanks.

Re: "-background transparent" not working

Posted: 2014-07-03T04:03:18-07:00
by snibgo
Your request is more complex than you realise. See the recent thread viewtopic.php?f=24&t=25717

Re: "-background transparent" not working

Posted: 2014-07-03T10:11:52-07:00
by fmw42
If all you want to do is turn a relatively constant background to transparent, then you can do so with -fuzz XX% -fill none -draw "matte x,y floodfill"

see
http://www.imagemagick.org/Usage/draw/#matte

Re: "-background transparent" not working

Posted: 2014-07-03T11:29:00-07:00
by glennrp
fmw42 wrote:If all you want to do is turn a relatively constant background to transparent, then you can do so with -fuzz XX% -fill none -draw "matte x,y floodfill"
That command will turn some of the foreground pixels transparent. The PNG format's "bKGD" chunk only accomodates an opaque color that is used to fill the unused or
transparent areas. If the PNG is a indexed (color-type==3), the transparency/opacity component of the background color is ignored, if the index happens to point to
a non-opaque color, and the background contains only the RGB components. In particular, "-background transparent" writes a black bKGD chunk.

Re: "-background transparent" not working

Posted: 2014-07-03T11:38:08-07:00
by fmw42
fmw42 wrote:If all you want to do is turn a relatively constant background to transparent, then you can do so with -fuzz XX% -fill none -draw "matte x,y floodfill"

see
http://www.imagemagick.org/Usage/draw/#matte

Update: save to PNG32:result.png

Code: Select all

convert image.jpg -fuzz XX% -fill none -draw "matte 0,0 floodfill" PNG32:result.png
The fuzz value allows for the background to be not perfectly constant color. This command will not change any interior colors unless there are some within the fuzz value at the edges of your foreground.

Re: "-background transparent" not working

Posted: 2014-07-03T22:35:43-07:00
by yugalgupta
Thanks [fmw42]. On trying your suggestion from the shell prompt we have been able to get transparent background. We are going to now test this with our API which automates the conversion process to confirm if it works there as well.