back

Kint.pl

This was a variation of tink.pl and instead of creating coloured text, it created 'greyscale' html text from an image. Like kint.pl it takes the rgb value, only this time it converts it to a greyscale value and then modulo divides it to get it my ascii greyscale palette.

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

usage: perl kint.pl image fontsize linesize [background-colour] [forground-colour]
(fontsize and linheight are both given in pixels, colours are given in rrggbb format, output will need to be redirected to a file)
#!/usr/bin/perl

#	kint.pl - create 'greyscale' ascii images from image files.  reads all the formats that the GD module does
#	(c) d.shaw may/june 2008.
#
#	usage is - 
#				perl kint.pl image fontsize linesize [background-colour] [forground-colour]
#
#	last two are optional and default to white and black respectively
#
#	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];
$bgcolour = $ARGV[3];
$fgcolour = $ARGV[4];
$debug = $ARGV[5];

if (!$bgcolour ) { $bgcolour = "ffffff";}
if (!$fgcolour ) { $fgcolour = "000000";}

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

$greylength = length $greydata;

$segment = int (255 / $greylength);

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

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

if ($debug ) {print "width = $width" . "px" . " height = $height" . "px greydata = $greydata greylenth = $greylength segment = $segment\n";}

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

for ( $y = 0 ; $y < $height ; $y++ ) {
	for ( $x = 0; $x < $width ; $x++ ) {
		( $r ,$g , $b ) = $imagedata->rgb( $imagedata->getPixel( $x , $y ) );

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

		$greyref = int ($greycolour / $segment);
		if ($greyref > $greylength) { $greyref = $greylength;}
		$greyref--;
		$greyout = substr ( $greydata, $greyref , 1);
		if ($debug) {
			print "$greycolour : $greyref : \"$greyout\"\n";
		}
		else {
			print $greyout;
		}
		
	}
	print "\n";
}

print q(</pre></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