Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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

Are you sure you need to extract all possible colors into that @col? There can be up to 2**24 of them in a PNG... A couple alternatives to mask areas with both R and B not more than 128 (if I'm reading your code and guessing your intent correctly):

use strict; use warnings; use feature 'say'; use Data::Dump 'dd'; use PDL; use PDL::NiceSlice; use PDL::IO::Image; #system 'convert rose: -resize 50% rose.png'; my $image = PDL::IO::Image-> new_from_file('rose.png') -> pixels_to_pdl; my $mask = ($image(,,(0)) <= 128) & ($image(,,(2)) <= 128); print $mask; __END__ [ [1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 +1] [1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 +1] [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 0 0 0 +1] [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 +1] [1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 +1] [1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0] [1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0] [1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0] [1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 +0] [1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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png' ); my $w = $i-> getwidth; my $m = Imager::transform2( { rpnexpr => << 'END' x y getp1 !pix @pix red 128 le @pix blue 128 le and 255 0 if 0 0 rgb END }, $i )-> convert( preset => 'red' ); $m-> write( data => \my $data, type => 'raw' ); $data =~ tr/\0\377/01/; say for unpack "(a$w)*", $data; __END__ 11111111111111000000000011111100111 11111111111111000000000011111110111 11111111111111100000100001111110001 11111111111111110011011001111111111 11111111111110000000010000000110001 11111111111100000000000000000000000 11110001111000000000000000000000000 11110000000000000000000000000000000 11111000000000000000000000000000000 11111100000000000000000000000011100 11111100000000000000000000000010100 11110000000000000000000000000111111 11100000100000000000000000000111111 11100001100000000000000000001101111 11100001100000000000000000011011111 11111111000000000000000000011111111 11111111000000000000000010011111111 11111111100000001011001110011111111 11111100111000000011111110111111111 11111000010000000111111111111111111 11111000000000000111111111111111111 11111000000000000111111111111111111 11111000000000000011111111111111111

Here RPN DSL (described to compile to very fast code) is used to compute result and put it into red channel, which then is extracted to a one channel image. Last 3 lines are there to produce demo output only.

Both results match of course, but dump of your @sw is different. Either I misread your code, or it has problems (it emits warnings, by the way), I didn't investigate.


In reply to Re: working with png data by vr
in thread working with png data by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 perusing the Monastery: (7)
    As of 2019-11-15 07:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (80 votes). Check out past polls.

      Notices?