Solved

Remove some colors from an image

Posted on 2006-07-06
6
271 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.php tree directory? 5 54
php call to a non-object 3 31
mysqli 3 18
I am Updating mysql where id =something ...Not working  Jquery/PHP 4 34
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

929 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