Page 1 of 1
Remove color from an image on the fly?
Posted: Thu Dec 02, 2004 1:45 pm
by Wakextreme
Does anyone know how i can remove the color from an image on the fly. I have a huge collection of jpeg images that are going to be used for an ecommerce site. The problem is the images all have an extremely ugly background color. I was wondering if anyone knew how to remove this color with the gd library or if it can't be removed, as they are jpegs and wont' support transparency, can the color be coverted to white? There are over 10,000 images and i dont' want to have to go through all of them one by one.
Here is a sample picture:
All of the images have this same unique background color......
Thanks in advance for any help
Posted: Thu Dec 02, 2004 3:10 pm
by kettle_drum
You just have to get the width and height of the image, and then loop through each pixel, using imagecolorat() to get the color at the specified pixel. Then check to see if this pixel is the color that you want to change and if so use imagesetpixel() to change the color.
Posted: Thu Dec 02, 2004 7:07 pm
by Wakextreme
thanks for the reply. is there anyway you could supply me with a sample chunk of code as i am very new to gd library. If not its ok im sure i can figure it out.
Posted: Thu Dec 02, 2004 7:17 pm
by kettle_drum
Not tested it, but it would be something like:
Code: Select all
function editAllPixels($image, $find, $replace){
$details = getimagesize($image);
$new = imagecreatetruecolor($details[0], $details[1]);
for($x = 0;$x < $details[0];$x++){
for($y = 0;$y < $details[1];$y++){
$color = getColorFromColorIndex(imagecolorat($image,$x,$y));
if($color == $find){
$colors = imagecolorallocate($new,$replace['r'],$replace['g'],$replace['b']);
imagesetpixel($new,$x,$y,$colors);
}
}
}
return $new;
}
function getColorFromColorIndex($color){
$array = array();
$array['r'] = ($color >> 16) & 0xFF;
$array['g'] = ($color >> & 0xFF;
$array['b'] = $color & 0xFF;
return $array;
}
Posted: Thu Dec 02, 2004 7:28 pm
by Wakextreme
kettle_drum wrote:Not tested it, but it would be something like:
Code: Select all
function editAllPixels($image, $find, $replace){
$details = getimagesize($image);
$new = imagecreatetruecolor($details[0], $details[1]);
for($x = 0;$x < $details[0];$x++){
for($y = 0;$y < $details[1];$y++){
$color = getColorFromColorIndex(imagecolorat($image,$x,$y));
if($color == $find){
$colors = imagecolorallocate($new,$replace['r'],$replace['g'],$replace['b']);
imagesetpixel($new,$x,$y,$colors);
}
}
}
return $new;
}
function getColorFromColorIndex($color){
$array = array();
$array['r'] = ($color >> 16) & 0xFF;
$array['g'] = ($color >> & 0xFF;
$array['b'] = $color & 0xFF;
return $array;
}
Thank you very very much. I'll give it a try right now.

Posted: Thu Dec 02, 2004 9:07 pm
by Wakextreme
Im prob jus stupid but i can't seem to get it to work. maybe im just using the function wrong......
Posted: Fri Dec 03, 2004 9:56 am
by pickle
Keep in mind that this will only convert the EXACT colour listed. The sample picture you showed has anti-aliasing between the image proper, and the background colour. Removing that single shade of turqoise will leave the darker shades that show up around the subject.
Are you familiar with Photoshop? You can set up a batch action that could go through your image directory, open each image, convert the background colour (and use anti-aliasing so it will likely get more shades of turqoise), and re-save it.
Posted: Fri Dec 03, 2004 8:40 pm
by Wakextreme
I don't have photoshop. Do you know if this is possible with macromedia fireworks? Thanks for the idea!
Posted: Mon Dec 06, 2004 9:48 am
by pickle
No, I really have no experience with Fireworks.
Posted: Mon Dec 06, 2004 10:15 am
by AngusL
I've got quite a bit of Fireworks experience but have to say I've never particulary tried to look for a batch function, as the only occasional time I need it it is covered by a freeware photoediting app. However, that said, I a) haven't seen one and b) don't believe there is one as it is mainly for the creation of images for the web rather than photoediting. Or maybe I'm very wrong and have just put my foot in it.