Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Replace colors in an image

by BrowserUk (Pope)
on Oct 04, 2005 at 18:58 UTC ( #497360=note: print w/ replies, xml ) Need Help??


in reply to Replace colors in an image

It very much depends upon which image format, and which of the various subformats the image is in. Many images are stored in a "palettized format", meaning that there is a lookup table (palette) in the image file that maps the numbers stored in the data portion of the file to the actual colors used when the image is displayed. With this type of image, changing an individual colour is as simple as replacing it's entry in the palette with the new colour.

If the image is stored as "true colour"--ie. 24-bits per perl format--then there is very little option but to scan the entire data portion of the image file and replace the old colour with the new.

However, depending upon the image format you wish to process, this needn't be a particularly slow process. With knowledge of the format of the file and a little care, it is possible to do this a regex-style search and replace process, treating the appropriate portion of the image as a big string, thereby avoiding the overhead of (multiple) function/method calls per pixel.

Which image format(s) do you need to process?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.


Comment on Re: Replace colors in an image
Re^2: Replace colors in an image
by johnnywang (Priest) on Oct 04, 2005 at 20:32 UTC
    just .png or .jpg, thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://497360]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-12-28 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls