back

Knit.pl

Another variation of kint.pl , this time it used the 'greyscale' characters but colours them in too, output is html.

Click here for an example html document (543kb) of an 144 x 185 pixel image of my friend Dave with fontsize of 6 and a lineheight of 4.

usage: perl knit.pl image fontsize linesize
(fontsize and linsize (aka lineheight) are both given in pixels, output will need to be redirected to a file)
#!/usr/bin/perl

#	knit.pl - create coloured 'greyscale' ascii images from image files.  reads all the formats that the GD module does
#	(c) d.shaw may/june 2008.
#
#	usage is - 
#				perl knit.pl image fontsize linesize
#
#	there's no error checking done either on anything, so it won't throw errors, it'll just fail...

use GD;

$imagefilename = $ARGV[0];
$fontsize = $ARGV[1];
$linesize = $ARGV[2];

$greydata = q(.,-:;*&O8SX@);

$greylength = length $greydata;

$segment = int (255 / $greylength);

$imagedata = GD::Image->new($imagefilename);

($width , $height) = $imagedata->getBounds();

print q(<html><body bgcolor=#000000 style="font-size: ) . "$fontsize" . q(px; line-height: ) . "$linesize" . q(px; font-family: courier new;">);

for ( $y = 0 ; $y < $height ; $y++ ) {
	for ( $x = 0; $x < $width ; $x++ ) {
		( $r ,$g , $b ) = $imagedata->rgb( $imagedata->getPixel( $x , $y ) );
		
		if ( ($r == $or) && ($g = $og) && ($b == $ob) ) { $samecolour = 1; }
		
		$mr = sprintf ("%02x" , $r);
		$mg = sprintf ("%02x" , $g);
		$mb = sprintf ("%02x" , $b);
		
		if ( $samecolour != 1) { print q(</font><font color=#)."$mr"."$mg"."$mb".q(>); }


		$greycolour = int (($r + $g + $g) / 3);

		$greyref = int ($greycolour / $segment);
		if ($greyref > $greylength) { $greyref = $greylength;}
		$greyref--;
		$greyout = substr ( $greydata, $greyref , 1);
		$greyout =~ s/ /\ \;/;
		print $greyout;
		
		$or = $r;
		$og = $g;
		$ob = $b;
		$samecolour = 0;
	}
	print q(<br>) . "\n";
}

print q(</font></body></html>);

All this code is © 2006 - 2015, except for the noted pieces which are © to their authors as noted. Feel free to use anything you find here, I'm not that precious about it. Take it, improve it, make lots of cash with it, it's a gift.

back