[SOLVED] convert to gradual alpha channel

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
Art60

[SOLVED] convert to gradual alpha channel

Post by Art60 »

Hi,

I'm a new IM user and I can't see to find how to convert a black background into a transparent one AND compensate antialiasing (black blending)

I found in this forum that the command line I needed was something like
convert original.png -transparent black x_copy.png

But the original image had antialiasing so the thin shape sometimes blends into the black background.
Image

So the resulting picture has dark edges around the shape.
Image

I think this alpha creating process could be enhanced by, as an option, beeing gradual and more, correct pixel color in order to compensate the color it erases.

Or maybe this process already exists and I didn't find how to activate it.

Thank you!
Art.
Last edited by Art60 on 2010-10-14T02:29:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert to gradual alpha channel

Post by fmw42 »

You need to create a binary mask, then blur it a litte, transform the blur so that 100 stays 100, 50 and below goes to 0 (black) so that the partial transparency does not bleed into the black areas, then composite the original with the mask into the alpha channel.

try


convert original.png \( -clone 0 -fill white +opaque black -blur 0x0.5 -level 50,100% \) \
-alpha off -compose copy_opacity -composite original_trans2.png

You can play with adjusting the 0.5 blur sigma to fine tune the transparency
The -fill white +opaque makes everything not black into white

see

http://www.imagemagick.org/Usage/basics/#image_seq
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/compose/#copyopacity
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/color/#level
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert to gradual alpha channel

Post by anthony »

Actually to recover the alpha correctly you need to determine amout of 'alpha' each pixel has,
and also color correct (remove alpha amount of black) from the color of each pixel.

This is a very tricky problem which is very difficult to do.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Art60

Re: convert to gradual alpha channel

Post by Art60 »

In fact, Anthony, this is the deal :
I know background is black
I know wave form should be "pure" color
So, I should determine the alpha value: easy, this is directly the pixel darkness, at the other hand, the color is directly the corresponding saturated one.

Thx!

Fmw42, your answer seems to be very complex at first glance and for what I understood plays with blur. Doesn't seem this cartesian to me :)

For your information, my "working" conversion batch is:

Code: Select all

convert "%~s1" -transparent black "%~dpn1_x.png"
So, I tried to write down, without trying to understand, your command line in a similar conversion batch file:

Code: Select all

convert "%~s1" \( -clone 0 -fill white +opaque black -blur 0x0.5 -level 50,100% \) \ -alpha off -compose copy_opacity -composite "%~dpn1_x.png"
But it throws errors:
Magick: unable to open image `\(': No such file or directory @ error/blob.c/OpenBlob/2572.
Magick: no decode delegate for this image format `\(' @ error/constitute.c/ReadImage/532.
Magick: option requires an argument `-level' @ error/convert.c/ConvertImageCommand/1773.

So I removed backslashes:

Code: Select all

convert "%~s1" ( -clone 0 -fill white +opaque black -blur 0x0.5 -level 50,100% ) -alpha off -compose copy_opacity -composite "%~dpn1_x.png"
But I still get an error:
Magick: option requires an argument `-level' @ error/convert.c/ConvertImageCommand/1773.
I don't understand much for -level is included :/

This is too complex for me as a first approach, sorry.
I think I'll undestand this when I have a week or two of writing scripts...

Thk you very much for your efforts, though, much appreciated.

Best regards,
Art.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: convert to gradual alpha channel

Post by anthony »

The use of '\' to 'escape' the parenthesis is needed for UNIX shells, and PHP shell escapes. IT is not needed for windows.

For Unix shell to window differences see IM Examples, Windows Usage, Conversion
http://www.imagemagick.org/Usage/windows/#conversion


As for determining your alpha values. As you are using saturated colors on a black background you may be able to use the results of of of the Hue colorspaces, such as HSL. The 'S' channel (green channel normally) is the color saturation and make give a good representation of just how transparent each pixel should be. The 'L' or luminance channel may also be usable. The 'H' channel would then tell you what the actual color of the transparent pixel should be.

Note that these are just guesses on my part, but would be a good starting point for the specific image you have given as an example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Art60

Re: convert to gradual alpha channel

Post by Art60 »

HSL !
This is THE trick, Anthony :)

Yes, I think this is the way I have to compute things with this kind of images.
A<-L
L<-maxvalue
S<-maxvalue
or something like that, may be
A<-maxvalue-L
should work better, I don't know which sign is Alpha vs transparency/opacity

You're guiding me to the clarity, dear friend ;)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert to gradual alpha channel

Post by fmw42 »

Seems to me that your colors are anti-aliased against black, so some bleeding of the colors into the black will occur which you cannot remove perfectly and won't know an exact mixture as it depends upon the anti-aliasing method. So what I tried to do was blur the mask used for transparency just a little to make the edges of your colors slightly transparent where black mixed in with the color and to remove the color bleed into the black areas.

Can you not make your line drawing directly onto a transparent background rather than a black one?
Art60

Re: convert to gradual alpha channel

Post by Art60 »

fmw42,

You may be true, I didn't succeed in running your script...
If you can help me in running it, may be I could tweak it a bit.
Thx!

I'm not the author of the pics.
These are directly from http://www.freesound.org/index.php pages

I won't ask them to draw all their samples with alpha instead of black background ;)
In fact I did :D , but I'm pretty sure they don't need to, so I have to do with what exists.

Thank you very much for your attention.
Art.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert to gradual alpha channel

Post by fmw42 »

I don't understand your usage of %~ in the following:

convert "%~s1" -transparent black "%~dpn1_x.png"

I suspect that is Windows syntax and I use a Mac and don't really know Windows scripting.

If you are on unix or mac then just take one image (not some wildcard series of images as IM does not do that well with convert) and run

convert original.png \( -clone 0 -fill white +opaque black -blur 0x1 -level 50,100% \) \
-alpha off -compose copy_opacity -composite original_trans2.png

Image

convert original.png \( -clone 0 -fill white +opaque black -blur 0x0.5 -level 50,100% \) \
-alpha off -compose copy_opacity -composite original_trans2.png

Image

If on Windows, then try

convert original.png ( -clone 0 -fill white +opaque black -blur 0x0.5 -level "50,100%%" ) ^
-alpha off -compose copy_opacity -composite original_trans2.png

see http://www.imagemagick.org/Usage/api/ for difference between running IM on Windows and Unix/Mac

Hopefully a Windows user will correct any mistakes I have above.

Note the \ or ^ at the end of the first line, is a line continuation for unix and windows, respectively. You can remove it if you put everthing on one line.

Parenthesis in Unix/Mac must be escaped with \, but they don't need escaping in Windows.


You have not identified your platform nor your version of IM, so it is hard to help as your IM may be old and not support some of these things.

If the above works with one image, then we can look into how you can process multiple image with a loop or with mogrify.
Art60

Re: convert to gradual alpha channel

Post by Art60 »

fmw42 wrote:I don't understand your usage of %~ in the following:
convert "%~s1" -transparent black "%~dpn1_x.png"
%1 is the first parameter to a DOS/Windows batch file. Since a while (may be W98), some extensions (~) make batch files easier to write :
So we now got s=short, d=drive, p=path, n=name, e=extension for each part of a filename one may pass as a parameter.

IM doesn't seem to like long names as input file so I used %~s1 to tell it to take the short (8.3) name of 1st batch command line parameter.
If you call my batch file with D:\ImageMagick\thelongfilename.png as a parameter, %~s1 value will be some kind of D:\IMAGE~1\THELON~1.PNG thing.

%~dpn1 tells to produce drive+path+name from 1st arg, so it removes the extension. Then I add _x.png to form the full output filename.
So, if you call my batch file with D:\ImageMagick\thelongfilename.png as a parameter, %~dpn1 value will be D:\ImageMagick\thelongfilename
Then, %~dpn1_x.png value will be D:\ImageMagick\thelongfilename_x.png
fmw42 wrote:You have not identified your platform nor your version of IM, so it is hard to help as your IM may be old and not support some of these things.
I'm running Windows XP SP3 and I downloaded IM the same day I asked for some help, so I think it was the V6.6.4-10 yet ;)
fmw42 wrote:If the above works with one image, then we can look into how you can process multiple image with a loop or with mogrify.
I don't need to make batch conversion of a bunch of files.
The batch file I wrote (with the %~dnp1 thing) should be enough for my software to transform the black background to transparent for each file I get from the freesound website at the time I need to draw it onto my application display. I just have to call something like black2x.bat thepicture.png and I get thepicture_x.png. as easy as this. ;)

Thank you again for your answer, I'll try it tomorrow. (not the same home/work machines)

All the best,
Art.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert to gradual alpha channel

Post by fmw42 »

Sorry, Windows batch files are out of my domain. Hopefully one of the Windows experts can help you further with translating my command line and your bat file.
Art60

Re: convert to gradual alpha channel

Post by Art60 »

Hi,

So I put your command line in a Windows batch file:

Code: Select all

convert "%~s1" ( -clone 0 -fill white +opaque black -blur 0x0.5 -level "50,100%%" ) -alpha off -compose copy_opacity -composite "%~dpn1_x.png"
And it does work:
Image
Original pic

Image
black -> transparent

Image
your command line

Black edges are less visible, thank you very much :)

But this method adds blur and I'm pretty sure HSL tweaking would do the trick.
I'll be working on that as a second approach.

Thank you again.

All the best,
Art
Art60

convert to gradual alpha channel [SOLVED]

Post by Art60 »

To All :

Thank you.

I finally found it was very easy to transform darkness into transparency with a single and simple command line parameter : -alpha Copy
My equivalant command line is now this one :

Code: Select all

convert original.png -alpha Copy NiceTransparent.png
Image
Really nice job, isn't it?

Thank you once again!

All the very best,
Art.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [SOLVED] convert to gradual alpha channel

Post by anthony »

It is close but not perfect.

-alpha copy will use the greyscale of the image, which with the colors may not be quite right.

Try extracting a lumince from HSL instead.

But if you are happy with the result. Fair enough.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Art60

Re: [SOLVED] convert to gradual alpha channel

Post by Art60 »

GreyScale is not Luminance?
I didn't know.

Yes, I thought my picture was good enough

So I'm may dig a little further, may be not right now, though.

Thx for enlighting this.

Art.
Post Reply