"Invert" 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
Posts: 1
Joined: 2011-05-27T16:44:24-07:00
Authentication code: 8675308

"Invert" transparency

Post by autoblinker »


My problem should be easy to solve as it isn't very complicated but everything I tried didn't work.

I have a image like this:
It has a transparent background and one solid color (white). The rounded borders have alpha transparency.

Now to my problem:
I want to "invert" the transparent and the white area so that the transparent area gets white and the white area gets transparent. The semi-transparent areas are my problem.

The things I tried:

Code: Select all

convert 1cut.png +level-colors red, out.png
--> the alpha channel gets lost so the shape is inaccurate.

Code: Select all

convert 1cut.png -transparent white out.png
--> the semi-transparent area isn't touched, so this way doesn't work either

Any hints how to solve this problem?


User avatar
Posts: 25951
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Invert" transparency

Post by fmw42 »

not sure I fully understand. so try these. the issue may still be your partially transparent areas. if so, you will need to threshold the alpha channel or use -morphology to erode some of it.

convert 1cut.png -channel a -negate +channel 1cut_nega.png

convert 1cut.png -channel rgba -negate +channel 1cut_nega2.png

if either of these is not what you want, please clarify further.

User avatar
Posts: 8884
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: "Invert" transparency

Post by anthony »

I assume you want the areas that is currently transparent white!
You must remember that transparenct pixels have a undefined color, as such you will need to ensure that they are white.

So Negate the alpha channel (as Fred previously pointed out) then set all pixels white.

Code: Select all

convert 1cut.png -channel a -negate +channel -fill white -colorize 100% 1cut_nega.png
Note the order of the two actions does not matter as you are appling them to each of the channel types in the image separately. But it is important that you ensure all pixels get colors white.

An alternative is just make sure all fully transparency pixels are white (before negating) using...

Code: Select all

convert 1cut.png -background white -alpha background -channel a -negate +channel 1cut_nega.png
See http://www.imagemagick.org/Usage/maskin ... background
In this can the final +channel setting does not actually matter as you save the image, but it is a good thing to do if you want to continue processing the image, instead of saving.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages

Post Reply