Transparent Background

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Manisha
Posts: 5
Joined: 2011-03-10T22:01:02-07:00
Authentication code: 8675308

Transparent Background

Post by Manisha »

I went through all similar posts and tried the solutions offered along with the examples given in the imagemagick site.

The problem is that I cannot achieve a transparent background for my gif and png images. I can change the background to other colors easily but when I try to make it transparent, it gives me a white background. This white background is also superimposed on other images when I use the composite command which is not desired.

Nothing seems to work.

Is there any issue with the alpha channel of my images. I also tried creating sample objects, but still the white background appears as the smallest bounding rectangle.

The version of Imagemagick I am using is: 6.6.7 - Q16
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparent Background

Post by fmw42 »

please post a link to your example images and your exact command line
Manisha
Posts: 5
Joined: 2011-03-10T22:01:02-07:00
Authentication code: 8675308

Re: Transparent Background

Post by Manisha »

Please check the image at: http://img88.imageshack.us/img88/1148/part5q.png

I tried using the following commands:

convert part5.png -background transparent white part6.png
convert part5.png -transparent white part6.png
convert part5.png -alpha transparent part6.png

Also, I created a transparent canvas of the same size of the image and then tried:

convert part5.png background.png -compose Changemask -composite part6.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparent Background

Post by fmw42 »

This works perfectly fine for me and is the correct way to do it. Are you viewing your result in IM or some browser with a white background? You need a viewer that shows some other color such as gray or check pattern for transparent.


convert part5q.png -transparent white part5q_tmp1.png

identify -verbose part5q_tmp1.png
Image: part5q_tmp1.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 529x800+0+0
Resolution: 28.34x28.34
Print size: 18.6662x28.2287
Units: PixelsPerCentimeter
Type: TrueColorMatte
Endianess: Undefined
Colorspace: RGB
Depth: 16-bit
Channel depth:
red: 16-bit
green: 16-bit
blue: 16-bit
alpha: 1-bit
Channel statistics:
Red:
min: 5562 (0.0848707)
max: 65535 (1)
mean: 61407.5 (0.937018)
standard deviation: 10273.6 (0.156765)
kurtosis: 6.10642
skewness: -2.5485
Green:
min: 4020 (0.0613413)
max: 65535 (1)
mean: 57718 (0.88072)
standard deviation: 18089.4 (0.276026)
kurtosis: 2.06346
skewness: -1.98115
Blue:
min: 5910 (0.0901808)
max: 65535 (1)
mean: 58441.1 (0.891755)
standard deviation: 15916.7 (0.242874)
kurtosis: 1.59891
skewness: -1.86409
Alpha:
min: 0 (0)
max: 65535 (1)
mean: 11395.4 (0.173882)
standard deviation: 24838.3 (0.379008)
kurtosis: 0.961497
skewness: -1.7209


I am on IM 6.6.8.4 Q16 Mac OSX Tiger.

Please note that PNG format has been under changes and improvements for quite some releases. You would be best to upgrade to the latest version and also see all the changes going on at http://www.imagemagick.org/script/changelog.php

P.S. If the background is not pure white, then you need to add -fuzz XX%

convert part5q.png -fuzz XX% -transparent white part5q_tmp1.png

see http://www.imagemagick.org/Usage/color_basics/#replace
Manisha
Posts: 5
Joined: 2011-03-10T22:01:02-07:00
Authentication code: 8675308

Re: Transparent Background

Post by Manisha »

Thanks, the issue was solved.

I switched to cygwin and installed the version you are using and it is working fine now. In my Windows version the color type was "TrueColor" which did not support the alpha channel or transparency, I suppose.


I have a real time problem now. I want to take a picture of a semi-transparent object ( a piece of cloth). I am at liberty to change the background
if any specific colored background can help me remove the background and get the transparent cloth piece only. Can that be done?
HugoRune
Posts: 90
Joined: 2009-03-11T02:45:12-07:00
Authentication code: 8675309

Re: Transparent Background

Post by HugoRune »

Manisha wrote: I have a real time problem now. I want to take a picture of a semi-transparent object ( a piece of cloth). I am at liberty to change the background
if any specific colored background can help me remove the background and get the transparent cloth piece only. Can that be done?
check out this post
viewtopic.php?f=1&t=18235&start=15#p70009
If you can use a background that has a very different color from the object, then this method should work.
Ideally background and object should have complementary colors. The problem with this method is that you might remove too much of the background color.

Take a shot of the object, and the background without the object, and then use

Code: Select all

convert \
   ( object.png background.png -fx " v==1?0: (u - v) / (1-v) " ) \
   ( object.png background.png -fx " v==0?0: (v - u) / v " ) \
  -separate -evaluate-sequence max  object_alpha.png

convert object.png object_alpha.png background.png -alpha Off \
  -fx "v==0 ? 0 : u/v - u[2]/v + u[2]" \
object_alpha.png -compose Copy_Opacity -composite object_without_background.png

if you can take the same picture in front of two backgrounds, this would be the best method:
http://www.imagemagick.org/Usage/maskin ... background
however with cloth, you probably cannot get exactly the same picture twice.


Other methods for background removal are found here, but most of them will have difficulties with semi-transparent objects.
http://www.imagemagick.org/Usage/masking/#bg_remove
Post Reply