Remove black (add transparency)

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
Marcel71
Posts: 49
Joined: 2011-04-13T13:57:34-07:00
Authentication code: 8675308

Remove black (add transparency)

Post by Marcel71 »

Hi everybody,

Does anybody know if it is possible with imagemagick to remove the black part and that the white stripes also become semi transparent?
Image

I searched, but could not find a 'ready to use' convert command...

Regards,
Marcel
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by GreenKoopa »

I can't see your image or otherwise connect to that domain.
Marcel71
Posts: 49
Joined: 2011-04-13T13:57:34-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by Marcel71 »

That is strange. I see the image an i can connect to glitterbase.com

But okay, i uploaded the image to imgur :)

Image
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by GreenKoopa »

Do you want?
  • black becomes fully transparent
  • white stays white and fully opaque
  • grays stay gray and become partially transparent
Or something a little different? I ask because the lightest pixel is very dark and so would be less than half transparent.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove black (add transparency)

Post by fmw42 »

is this what you want?

convert 6SyqTgj.png \( +clone -fill white -colorize 100 \) +swap -alpha off -compose copy_opacity -composite test1.png

or

This will make the white more opaque by a factor of 2. You can change that value for more opaque if you want.

convert \( 6SyqTgj.png -evaluate multiply 2 \) \( +clone -fill white -colorize 100 \) +swap -alpha off -compose copy_opacity -composite test2.png


I am taking your grayscale image, multiplying it by some factor, then cloning it and filling it completely with white, then putting modified grayscale image into the alpha channel of the white image.
Marcel71
Posts: 49
Joined: 2011-04-13T13:57:34-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by Marcel71 »

I would like that the black turns transparent and the white becomes semi transparent. (so there is nothing black anymore, just white semi transparent stripes).

*Will try fmw's solution. Did not see that before posting this :)
Marcel71
Posts: 49
Joined: 2011-04-13T13:57:34-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by Marcel71 »

This left me with an empty canvas

Code: Select all

convert 6SyqTgj.png \( +clone -fill white -colorize 100 \) +swap -alpha off -compose copy_opacity -composite test1.png
This looks indeed what i want, i will need to play with the multiply (like set it to 3 or higher) to get the correct result.

Code: Select all

convert \( 6SyqTgj.png -evaluate multiply 2 \) \( +clone -fill white -colorize 100 \) +swap -alpha off -compose copy_opacity -composite test2.png
Thanks a lot fer helping out.

Regards,
Marcel
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Remove black (add transparency)

Post by GreenKoopa »

Marcel71,
An alternative to -evaluate multiply is -auto-level, which would apply a multiply of 3.5 in this case.

Fred,
I had a different solution in mind but I only ran into problems. Any insight?

These create proper output as viewed by IM, but GIMP doesn't show a histogram for them, which couldn't be weirder.

Code: Select all

convert 6SyqTgj.png -alpha Copy out1.png
convert 6SyqTgj.png -alpha Copy PNG32:out2.png
Here -colorize removes the alpha, which seems incorrect.

Code: Select all

convert 6SyqTgj.png -alpha Copy -fill white -colorize 100 out3.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove black (add transparency)

Post by fmw42 »

convert 6SyqTgj.png -alpha Copy out1.png
That should work, but the alpha channel will only have partial transparency and background will have very dark areas under the transparency. So it will all appear dark.

The histogram will have very small bins especially when weighted with the alpha channel. In GIMP histogram change the selector to Alpha and you will see very small bins even in just the alpha channel. Because there is such a large bin at black all the other bins are scaled very small.


But try

convert 6SyqTgj.png -alpha Copy -alpha off -fill white -colorize 100 -alpha on out1.png

That should be the same as my first result which puts a full white image under the alpha channel.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove black (add transparency)

Post by anthony »

You are all making it more complicated than it really is... ;-)

Just use it as a transparency mask for a white image... (or black if that is what you want).

Code: Select all

convert 6SyqTgj.png -background white -alpha shape rain_overlay.png
you can also level adjust before hand if you like to make the rain streaks stronger.

Code: Select all

convert 6SyqTgj.png -auto-level -background white -alpha shape rain_overlay.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply