G'day bliako,
"Ideally, I would like a method which just takes in an array of RGB values and creates the image (fastly)."
The following Tk code does this.
#!/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;
Notes:
-
The GUI renders the image and shows the time taken.
-
The GUI image is exact size.
I used Gimp to look at the disk copy and zoomed in to see details.
-
The time is measured from just before the unpopulated 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.
-
I took some minor liberties with the array of RGB values
— @raw_rgbs 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.
-
If they're both the same size, nothing further needs doing.
-
If the array is smaller, you may want to pad the image with transparent pixels
or, perhaps, use some default background colour.
-
If the array is larger you can:
truncate the array;
simply not use the excess elements (in my code, I didn't put() 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).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.