back

Kinti.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. This version outputs a gif as the file sizes are much smaller and it's only 2 colour black and white..

usage: perl kinti.pl image
(output will need to be redirected to a file)
#!/usr/bin/perl

#	kinti.pl - 	create 'greyscale' ascii images from image files.  reads all the formats that the GD module does
#				this version will output a jpeg image though
#	(c) d.shaw may/june 2008.
#
#	usage is - 
#				perl kinti.pl image 
#
#	there's no error checking done either on anything, so it won't throw errors, it'll just fail...
#
# 	gdTinyFont is 6 x 9 pixels
#	


use GD;

$font_x_size = 5;		#	makes a better image at 6x6 though
$font_y_size = 5;
$out_font = gdSmallFont;

$imagefilename = $ARGV[0];

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

$greylength = length $greydata;

$greylength++;

$segment = int (256 / $greylength);

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

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

# setup the output image

$out_width = $width * $font_x_size;
$out_height = $height * $font_y_size;
$out_x = 0;
$out_y = 0;

$out_im = new GD::Image( $out_width , $out_height );

$white = $out_im->colorAllocate(255,255,255);
$black = $out_im->colorAllocate(0,0,0);

for ( $y = 0 ; $y < $height ; $y++ ) {
	$out_y = $y * $font_y_size;
	for ( $x = 0; $x < $width ; $x++ ) {
		( $r ,$g , $b ) = $imagedata->rgb( $imagedata->getPixel( $x , $y ) );
		
		
		$out_x = $x * $font_x_size;
		
		$greycolour = int (($r + $g + $b) / 3);

		$greyref = int ($greycolour / $segment);
		if ($greyref > $greylength) { $greyref = $greylength;}
		$greyref--;
		$greyout = substr ( $greydata, $greyref , 1);
		
		$out_im->char( $out_font , $out_x , $out_y , $greyout , $black);
	}
}

binmode STDOUT;
print $out_im->gif;				#	gifs are a lot smaller and it's only 2 colour b&w

exit (0);

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