Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remove some colors from an image

Posted on 2006-07-06
6
Medium Priority
?
277 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 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