Solved

Remove some colors from an image

Posted on 2006-07-06
6
270 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:BGMi
  • 3
6 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17050280
Please have a look at http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21450622.html

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

Author Comment

by:BGMi
ID: 17050449
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
0
 

Author Comment

by:BGMi
ID: 17053531
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
0
 

Author Comment

by:BGMi
ID: 17054767
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
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17105671
Closed, 500 points refunded.
Netminder
Site Admin
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now