Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to create and save an image from RGB values

by vr (Curate)
on Dec 28, 2021 at 12:35 UTC ( #11139983=note: print w/replies, xml ) Need Help??


in reply to How to create and save an image from RGB values

I think Imager is good enough for this task. Though starting point looks strange to me, but assuming there already is, as in OP, a Perl array of (unusual) ARGB packed colour values, read the 4 channels, then just drop useless 0th channel. Otherwise the hassle with "combine" (or something similar, TMTOWTDI), etc. is unnecessary, of course.

use strict; use warnings; use Imager; use constant IMAGESIZE => 300; use constant ROSYBROWN => 0x00BC8F8F; my @result = map { # make us some picture to find when ( ROSYBROWN ) x $_, # examining output, let it be random int( rand 0xFFFFFFFF ), # colour diagonal on uniform BG ( ROSYBROWN ) x ( IMAGESIZE - $_ - 1 ), } 0 .. IMAGESIZE - 1; my $img = Imager-> new( type => 'raw', xsize => IMAGESIZE, ysize => IMAGESIZE, data => pack( 'L>*', @result ), raw_interleave => 0, raw_datachannels => 4, raw_storechannels => 4, ); Imager-> combine( src => [ $img, $img, $img ], channels => [ 1, 2, 3 ], )-> write( file => 'test.png' );

Replies are listed 'Best First'.
Re^2: How to create and save an image from RGB values
by bliako (Monsignor) on Dec 28, 2021 at 13:02 UTC

    Thanks, that is what I was looking for, last night: pass Imager a data array or pack'ed binary data. I could not hit that in any doc (with my limited bandwidth). And even now Imager::Draw manages to confuse me more than clear it up.

    Is the following equivalent without the functionality of switching the channels (I can create data with any channel arrangement)? Note how I had to change ROSYBROWN to RGBA and that non-transparent seems to be 0xFF!

    use strict; use warnings; use Imager; use constant IMAGESIZE => 300; # RBGA use constant ROSYBROWN => 0xBC8F8FFF; my @result = map { # make us some picture to find when ( ROSYBROWN ) x $_, # examining output, let it be random int( rand 0xFFFFFFFF ), # colour diagonal on uniform BG ( ROSYBROWN ) x ( IMAGESIZE - $_ - 1 ), } 0 .. IMAGESIZE - 1; my $img = Imager-> new( type => 'raw', xsize => IMAGESIZE, ysize => IMAGESIZE, data => pack( 'L>*', @result ), raw_interleave => 0, raw_datachannels => 4, raw_storechannels => 4, ); $img->write(file=>'out.png');

      Opaque is "1", transparent is "0", that's correct. When 0th (alpha in that case) channel was discarded, its content was irrelevant. Your code is not equivalent, it produces RGBA PNG image (partial random transparency over diagonal). Simply write:

      raw_storechannels => 3,

      then Imager will discard 4th (3d, counting from 0) i.e. alpha channel on reading, and then, too, its content would be irrelevant (ROSYBROWN's 4th byte could be any). Then file created will be opaque RGB PNG.

      As an aside, I find Imager documentation excellent, but then all people are different, they say :)

        This looks a lot like an application of "Einstein-inspired notation operations" (aka einops) would have helped here. I've been toying with the idea of making a translator from einops notation to PDL slice syntax, but haven't done so yet. I am 100% sure that a pull-request with a PDL::Slices::einops function (with suitable tests, even if incomplete) would be gratefully received :-D

        EDIT The future-design of PDL is being explored on this GitHub issue, including a mention of einops, and anyone with ideas would be extremely welcome to contribute!

        got it thanks. it's just I was not able to find in the doc how to do something IMO trivial.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-08-19 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?