back

Kniti.pl

This does the same as knit.pl, but it's output is a jpeg image.

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

#	kniti.pl - 	create coloured '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 kniti.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
#	
#	i can use the function GD::Image->trueColor(1) to make it true (24-bit) colour

use GD;

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

$imagefilename = $ARGV[0];

$greyout = "@";

$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;

GD::Image->trueColor(1);
$out_im = new GD::Image( $out_width , $out_height );

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;
		
		$out_im->char( $out_font , $out_x , $out_y , $greyout , $out_im->colorAllocate($r,$g,$b) );
	}
}

binmode STDOUT;
print $out_im->jpeg(100);			#	jpges do full colour

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