Link to home
Start Free TrialLog in
Avatar of BGMi
BGMi

asked on

Remove some colors from an image

Hi experts !

  I have a script that download an image (a radar view for rain), and resize/crop it.  That part is fine.  The image is saved in PNG.

  What I would like to do, is to remove some color of that image ... or replace it with a common color and make it transparent.  So, how can I achieve that ?  Let's say, for the purpose of this example, that I have an image for which I want to make the black and white transparent.

Thanks a lot for your precious help !
Have a nice day !
Xor
Avatar of Raynard7
Raynard7

Please have a look at https://www.experts-exchange.com/questions/21450622/Resample-and-make-transparent-or-vice-versa.html

You need to open it - depending on the type of file and image libraries loaded and then run filters over it.
Avatar of BGMi

ASKER

I would much appreciate a code snippet ... I will not give 500 points for a link ... sorry !

And I get a fast look at it, will return later, and I'm not sure that it's exactly what I need.  There is two colors that I want to make transparent, maybe three ... so if I'm not mistaken, I just can make one color transparent ... so I need to convert those two/three colors to a common color and then, make this one transparent.  But as I said, I haven't red the whole thread, I will a little later today.

Again, an example will be much appreciate !

Thanks a lot !
Xor
Avatar of BGMi

ASKER

I've looked at the link you provided, and it is not what I want.

What I need is for certains colors, switch them to the transparent index.  But there is no easy way like "imageColorIndexSet()" ... or I haven't seen it.  So what I need, is to replace a certain color index by another, which would be the defined transparent index.

Thanks for your help !
Have a nice day !
Xor
Avatar of BGMi

ASKER

Finally, I did it in another way.  Instead of removing the color that I don't want from the image, I read each pixel from the image and keep the one with the color table that I want to keep.  That way, I have a new image with only the colors I want (I could have did it with color I don't want, but with that method, it's easier that way as I have about 20 colors to keep).  So I set a background to my image first, then print thoses pixels, and make the background transparent.

Here an example:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      #Create transparency
      $cTrans = imageColorAllocate($newImg, 255, 0, 255);
      imageColorTransparent($newImg, $cTrans);

      #Allocate colors table
      foreach($colors[$typeColors] AS $i => $c)
      {
            $arrayIndexes[$i] = imageColorAllocate($newImg, $c[0], $c[1], $c[2]);
      }

      #Scan image and print wanted pixels
      for($x = $pX; $x < ($width - $pX); $x++)
      {
            for($y = $pY; $y < ($height - $pY); $y++)
            {
                  $rgb = imageColorsForIndex($tmpImg,imageColorAt($tmpImg,$x,$y));
                  $check = array($rgb['red'], $rgb['green'], $rgb['blue']);
                  if(in_array($check,$colors[$typeColors]))
                  {
                        $index = array_search($check,$colors[$typeColors]);
                        imageSetPixel($newImg, $x, $y, $arrayIndexes[$index]);
                  }
            }
      }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

My images are not that big, so it's not really a problem to read pixel by pixel.  The biggest one I will have is 480x480.  It take about 30sec for 10 images that size, so not that bad anyway !

Xor
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial