Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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); }

In reply to Re: Replace colors in an image by tonyc
in thread Replace colors in an image by johnnywang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (13)
    As of 2014-12-22 13:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (118 votes), past polls