Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
There's more than one way to do things.
 
PerlMonks

Replace colors in an image

by johnnywang (Priest)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Oct 04, 2005 at 18:37 UTC ( #497355=perlquestion: print w/ replies, xml ) Need Help??
johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Given an image, I'd like to replace some color (say red) with another color (say white). What's the best way to go about it? I've looked into PerlMagick (are there more complete documentations? the one at http://www.imagemagick.org/script/perl-magick.php doesn't explain function parameters in detail). I guess I could get the rgb value at every pixel and replace it one by one, isn't there something easier, or fuzzy(i.e., replace colors in certain rgb range)? Thanks.

Comment on Replace colors in an image
Re: Replace colors in an image
by BrowserUk (Apostle) on Oct 04, 2005 at 18:58 UTC

    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.
      just .png or .jpg, thanks.
Re: Replace colors in an image
by zentara (Chancellor) on Oct 04, 2005 at 21:04 UTC
    Imager is pretty good for that. For an example of manipulation see Tk ImageMap-color-zones. That just tints images. To replace some color, you could go thru every pixel, test for the rgb value to change, then adjust each pixel. I don't see anyway other than going thru the image pixel by pixel. Tinting is fast. Testing each pixel is slow.

    Here is a snippet to get pixel rgb values with GD, and I'm sure Imager has a similar method.

    #!/usr/bin/perl use GD; my $image = newFromPng GD::Image('z.png'); ($x,$y)=$image->getBounds(); $maxColors = $image->colorsTotal; $pixel=$image->getPixel(23,34); ($r,$g,$b)=$image->rgb($pixel); print "$r\t$g\t$b\n";
    And check out gif colour changes

    I'm not really a human, but I play one on earth. flash japh
Re: Replace colors in an image
by tonyc (Monk) on Oct 05, 2005 at 00:06 UTC

    A not so simple way of doing this in Imager:

    #!perl -w use strict; use Imager; my $from = shift; my $to = shift; my $in = shift; my $out = shift or die "Usage: $0 fromcolor tocolor inimage outimage\n"; my $from_color = Imager::Color->new($from) or die "Cannot convert fromcolor $from into a color: ", Imager->errs +tr, "\n"; my $to_color = Imager::Color->new($to) or die "Cannot convert tocolor $to into a color: ", Imager->errstr, +"\n"; my $img = Imager->new; $img->read(file=>$in) or die "Cannot read image $in: ", $img->errstr, "\n"; my $result = replace_color($img, $from_color, $to_color) or die "Cannot replace colors: ", Imager->errstr, "\n"; $result->write(file=>$out) or die "Cannot write image $out: ", $result->errstr, "\n"; sub replace_color { my ($img, $from_color, $to_color) = @_; my ($from_red, $from_green, $from_blue) = $from_color->rgba; my ($to_red, $to_green, $to_blue) = $to_color->rgba; my $rpnexpr = <<'EOS'; x y getp1 !pix @pix red from_red eq @pix green from_green eq @pix blue from_blue eq and and to_red to_green to_blue rgb @pix ifp EOS my %constants = ( from_red => $from_red, from_green => $from_green, from_blue => $from_blue, to_red => $to_red, to_green => $to_green, to_blue => $to_blue, ); return Imager::transform2({ rpnexpr => $rpnexpr, constants => \%constants }, $img); }

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: perlquestion [id://497355]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (20)
ikegami
Corion
GrandFather
Your Mother
toolic
holli
JavaFan
kennethk
thezip
Eyck
NodeReaper
tubaandy
dirving
ssandv
VinsWorldcom
jonasbn
elTriberium
MikeDexter
d5e5
brp4h
As of 2010-02-09 21:05 GMT
Sections?
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

What level of existential comfort do you require?

Palace
Executive suite at the best hotel
Regular hotel in a decent part of town
Motel
Boarding house
Sleeping Bag on Couch in Basement
Any port in a storm
Camping under the freeway overpass
Jail
Other

Results (279 votes), past polls