Using Transparent to change colors to transparent

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
johnfl68
Posts: 33
Joined: 2012-07-09T18:31:26-07:00
Authentication code: 13

Using Transparent to change colors to transparent

Post by johnfl68 »

Hello:

I am using the following suggested on another site to remove a set of colors from a gif image, by making them transparent:

Code: Select all

my @noise =    (    
             '#646464'
        ,    '#ccffff'
        ,    '#cc99cc'
        ,    '#996699'
        ,    '#663366'
        ,    '#cccc99'
        ,    '#999966'
        ,    '#646464'
        ,    '#04e9e7'
        ,    '#019ff4'
        ) ;

my $fuzz = 32;

$img->Set(colorspace=> RGB, type=> TrueColor) ;
foreach my $color (@noise) {
$img>Transparent(color => $color, fuzz => $fuzz ) ;
 }
I left out the obvious image gif image load and png save for the new version.

This is supposedly working for others, but I can't seem to get it to work for me. I have even tried removing the foreach loop, and using just a single color, and still the target image and the new image are identical, aside from the slight overall color change from the new colorspace.

The version is ImageMagick 6.7.6-4

Anyone have any suggestions as to if something has changed, or why this does not seem to be working?

Thanks!

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

Re: Using Transparent to change colors to transparent

Post by fmw42 »

In command line mode, you would likely need to specify colors with alpha as opaque, that is include opaque alpha to your hex values,

#019ff4 --> #019ff4ff

you may also have to enable -alpha on and/or -channel rgba
johnfl68
Posts: 33
Joined: 2012-07-09T18:31:26-07:00
Authentication code: 13

Re: Using Transparent to change colors to transparent

Post by johnfl68 »

Thanks.

However, the -transparent-color color command is supposed to take care of the alpha and channel settings so that you don't need to do that when converting a color to a transparency.

"The -transparent operator also ensures that the image has an alpha channel enabled, as per "-alpha set", and does not require you to modify the -channel to enable alpha channel handling."

And, as I said, this is known to work in some version of ImageMagick/PerlMagick, I just do not know what version this works in, and why it does not seem to work in the version I am using.

As always, there is plenty of documentation for the command line, but very little for PerlMagick. So many things do not line up between the two unfortunately, and one has to spend days and days scouring the internet in hopes to find someone who has gotten something to work, and then there are no guarantees, as often what works in one version does not work in another.

I'll keep searching around...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Transparent to change colors to transparent

Post by fmw42 »

You are right about Transparent. For some reason I looked at it too quickly and immediately thought you were using the equivalent of -fill. Sorry, I am not a Perlmagick user, so probably cannot help further.

Did you initialize your img, since you don't show that in your code?
johnfl68
Posts: 33
Joined: 2012-07-09T18:31:26-07:00
Authentication code: 13

Re: Using Transparent to change colors to transparent

Post by johnfl68 »

Yes, I didn't show all the code, because there is a lot of other image magick stuff going on in the same script. Everything else works but that.

It's just a pain because right now I have to save the image out, do the conversion via command line once for each of the 10 colors, read the image back into the script, and continue on. This happens for over a dozen images every 5 minutes.

Obviously it would be nice to work with the image while it is still in the image magick buffer, and not have to go outside of the box so to speak.

Well, thanks for at least trying to help.

If I ever figure it out. I'll post back here with the answer.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Transparent to change colors to transparent

Post by fmw42 »

$img>Transparent(color => $color, fuzz => $fuzz ) ;
I do not know Perlmagick, but is there a typo? Should it be

$img->Transparent(color => $color, fuzz => $fuzz ) ;
johnfl68
Posts: 33
Joined: 2012-07-09T18:31:26-07:00
Authentication code: 13

Re: Using Transparent to change colors to transparent

Post by johnfl68 »

Sorry - that is a typo in what was posted here, as I shortened the variable name for posting here.
It does not exist in my code.

Thanks though, I wish it was something that simple.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Transparent to change colors to transparent

Post by fmw42 »

Have you tried to just make one color transparent rather than using a loop over a list to be sure it is not something else in your script? Make the simplest script you can to test the command.
Post Reply