"-background transparent" not working

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
yugalgupta
Posts: 5
Joined: 2014-06-24T02:55:51-07:00
Authentication code: 6789

"-background transparent" not working

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: "-background transparent" not working

Post by snibgo »

Your request is more complex than you realise. See the recent thread viewtopic.php?f=24&t=25717
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "-background transparent" not working

Post 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
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: "-background transparent" not working

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "-background transparent" not working

Post 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.
yugalgupta
Posts: 5
Joined: 2014-06-24T02:55:51-07:00
Authentication code: 6789

Re: "-background transparent" not working

Post 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.
Post Reply