Remove color from an image on the fly?

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
Wakextreme
Forum Newbie
Posts: 5
Joined: Thu Dec 02, 2004 1:39 pm

Remove color from an image on the fly?

Post 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:

Image

All of the images have this same unique background color......

Thanks in advance for any help
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Wakextreme
Forum Newbie
Posts: 5
Joined: Thu Dec 02, 2004 1:39 pm

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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;
	}
Wakextreme
Forum Newbie
Posts: 5
Joined: Thu Dec 02, 2004 1:39 pm

Post 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.
:lol:
Wakextreme
Forum Newbie
Posts: 5
Joined: Thu Dec 02, 2004 1:39 pm

Post by Wakextreme »

Im prob jus stupid but i can't seem to get it to work. maybe im just using the function wrong......
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Wakextreme
Forum Newbie
Posts: 5
Joined: Thu Dec 02, 2004 1:39 pm

Post by Wakextreme »

I don't have photoshop. Do you know if this is possible with macromedia fireworks? Thanks for the idea!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

No, I really have no experience with Fireworks.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
AngusL
Forum Contributor
Posts: 155
Joined: Fri Aug 20, 2004 4:28 am
Location: Falkirk, Scotland

Post 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.
Post Reply