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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.