note
kcott
<p>G'day [id://1165397|bliako],</p>
<blockquote>
<em>"Ideally, I would like a method which just takes in an array of RGB values and creates the image (fastly)."</em>
</blockquote>
<p>
The following [https://metacpan.org/pod/Tk|Tk] code does this.
</p>
<code>
#!/usr/bin/env perl
use strict;
use warnings;
use Tk;
use Tk::PNG;
use Time::HiRes 'time';
my $mw = MainWindow::->new();
my @common_raw_rgbs;
my $png_image = 'pm_11139959_image.png';
my ($W, $H) = (100, 100);
{
no warnings 'qw';
@common_raw_rgbs = qw{
#ff0000 #ffff00 #009900 #00ffff #0000ff #ff00ff
};
}
my @raw_rgbs = (@common_raw_rgbs) x int(1 + ($W * $H / (0+@common_raw_rgbs)));
my $t0 = time;
my $image = $mw->Photo(-format => 'png', -width => $W, -height => $H);
my $i = 0;
for my $y (0 .. $H - 1) {
for my $x (0 .. $W - 1) {
$image->put([$raw_rgbs[$i++]], -to => $x, $y, $x + 1, $y + 1);
}
}
$image->write($png_image);
my $t1 = time;
$mw->Label(-text => 'Image:')->pack();
$mw->Label(-image => $image)->pack();
$mw->Label(-text => 'Time: ' . sprintf '%.6f', $t1 - $t0)->pack();
MainLoop;
</code>
<p>Notes:</p>
<ul>
<li>
The GUI renders the image and shows the time taken.
<ul>
<li>
The GUI image is exact size.
I used Gimp to look at the disk copy and zoomed in to see details.
</li>
<li>
The time is measured from just before the unpopulated [https://metacpan.org/pod/Tk::Photo|Tk::Photo] is created,
to just after the final PNG is written to disk
— all tests gave this as a tad over 59ms; as always, YMMV.
</li>
</ul>
</li>
<li>
I took some minor liberties with the array of RGB values
— <c>@raw_rgbs</c> actually contains 10,002 elements; a 100x100 image only has 10,000 pixels —
because I couldn't tell how you were aligning image size with array length.
<ul>
<li>
If they're both the same size, nothing further needs doing.
</li>
<li>
If the array is smaller, you may want to pad the image with transparent pixels
or, perhaps, use some default background colour.
</li>
<li>
If the array is larger you can:
truncate the array;
simply not use the excess elements (in my code, I didn't <c>put()</c> the last two elements); or,
modify the image size at the outset (some decisions for you regarding how you'd want to go about this).
</li>
</ul>
</li>
</ul>
<p></p>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-861371">
<p>— Ken</p>
</div></div>
11139959
11139959