This works for me. I have used Image::PNG::Libpng. Somehow last night, in the dark, it looked scary but in the light of day it turns out to be quite adorable. And fast too.

use Image::PNG::Libpng ':all'; use Image::PNG::Const ':all'; my $W = 500; my $H = 500; my $compression_level = 9; ################################# my %ihdr = ( width => $W, height => $H, bit_depth => 8, # one of: PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLO +R_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, PNG_COLOR_TYPE_RGB_ALPHA. color_type => PNG_COLOR_TYPE_RGB, ); my @rows; for my $y (0..$H-1){ # each row is a binary string packing $W (R,G,B) triplets (not + an array) # so we keep appending to this binary buffer (a string! but bi +nary string ok,right?) my $col_bin_str = ""; for my $x (0..$W-1){ my $R = int(rand()*0x100); my $G = int(rand()*0x100); my $B = int(rand()*0x100); # test compression with this "compressable" content # my ($R, $G, $B) = (0xFF) x 3; # pack an R,G,B tripplet into 3 bytes, each an unsingned char # that would be different for different scheme (RGBA) +etc. $col_bin_str .= pack "CCC", $R, $G, $B; } push @rows, $col_bin_str; } my $png = create_writer('file.png'); $png->set_compression_level($compression_level); $png->set_IHDR(\%ihdr); $png->set_rows(\@rows); $png->write_png();

bw, bliako


In reply to Re: How to create and save an image from RGB values by bliako
in thread How to create and save an image from RGB values by bliako

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, details, 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, summary, 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.