Solved

Remove some colors from an image

Posted on 2006-07-06
6
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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