in reply to working with png data
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 0 0 0 0 0 0 1 1 1 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 0 0 0 0 0 0 1 0 1 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 1 1 1 1 1 +1] [1 1 1 0 0 0 0 0 1 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] [1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 +1] [1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 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 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 0 0 0 0 0 0 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 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 1 1 1 +1] [1 1 1 1 1 1 0 0 1 1 1 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 0 0 0 0 1 0 0 0 0 0 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 0 0 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 0 0 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 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +1] ]
Or using Imager:
use strict; use warnings; use feature 'say'; use Data::Dump 'dd'; use Imager; my $i = Imager-> new( file => 'rose.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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: working with png data
by Anonymous Monk on Sep 10, 2019 at 19:49 UTC | |
by Anonymous Monk on Sep 11, 2019 at 07:07 UTC | |
by vr (Curate) on Sep 11, 2019 at 08:47 UTC | |
by Anonymous Monk on Sep 26, 2019 at 10:31 UTC |