Posted 19 July 2006 - 01:33 PM
I am almost killing myself....
I am trying to change all the magenta to another color (doesnt matter) in an image.
how can I do that?
thanks a lot
Posted 19 July 2006 - 03:43 PM
<?php $image = imagecreatefromgif("image.gif"); $from = "ff0000"; $to = "0000ff"; $c1 = sscanf($from,"%2x%2x%2x"); $c2 = sscanf($to,"%2x%2x%2x"); $index = imagecolorexact($image,$c1,$c1,$c1); imagecolorset($image,$index,$c2,$c2,$c2); header("Content-Type: image/png"); imagepng($image); ?>
If you are wanting to do this with a true colour image, such as a jpeg, it's a little more involving - The only way I have thought about doing this is checking/changing the colour of each pixel (pixel by pixel).
Posted 19 July 2006 - 08:12 PM
$c1 = sscanf($from,"%2x%2x%2x");
$c2 = sscanf($to,"%2x%2x%2x");
I'll adapt and use that as
<?php $color = "#ff0000"; list ($r, $g, $b) = sscanf($color,"#%2x%2x%2x"); ?>
Donations gratefully received
|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
Posted 20 July 2006 - 05:22 AM
but if the color i want to change is in different opacities?
Posted 20 July 2006 - 07:05 AM
I'm personally working on a colour replacement script at the moment for replacing colours in photographs, although my script only replaces the hue (still working on the saturation and value - Having difficulty trying to get it to work there).
So here's what I have found out so far - You'll need a range of colours you want to replace - a 'threshold'. The best awy I could work out how to do this is to convert the colour from hexadecimal, to RGB, and then from RGB into HSV. I chose HSV because of how it works makes it a lot easier to select 'similar' colours, or in my case hues. If you are not clued up on the HSV model there's a good explination on Wikipedia, but basically, HSV is made up of Hue, Saturation and Value. Hue ranges from 0 - 360 degrees (normally displayed as a 'ring' of merging colours), Saturation and Value are pretty much other names for Brightness and Contrast I believe and each range from 0 - 100%.
When seeing the colour wheel, you'll see why this is going to be the easier option for working out the 'range' or colours you want to change.
You'll need to loop through each pixel in the image with two for loops, one nested inside the other to loop through both the x and y coordinates, and then check each colours hue to see if it is in your selected 'range' by doing the conversion and checking if its hue (degrees) lies within the threshold of your predefined (converted) hue. If so, change the pixels hue (this time, doing the conversion in reverse - HSV -> RGB -> HEX).
Thats pretty much it, in a nutshell. I think it's a good starting point for you
The formulae for the conversions, HEX - > RGB can be seen in my above example, made a bit clearer in Barands adaptation. And RGB -> HSV, there is a formula on the Wikipedia entery for HSV I think...
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users