#!/usr/bin/perl -w # Resize and convert image to text. use strict; die "$0 \n" unless $#ARGV == 2; my ($input, $width, $height) = @ARGV; # Convert to 2 color PGM. my $cmd = "anytopnm $input | " . "pamdepth 255 | " . "pnmscale -xsize=$width -ysize=$height | " . "pnmquant 2 | " . "ppmtopgm"; open my $pipe, "$cmd|" or die $!; # Convert PPM to text, assuming first pixel is black and everything # else is white. my ($header, $header0, $blackpixel); $header0 = "P5\012$width $height\012255\012"; $blackpixel = undef; sysread($pipe, $header, length($header0)) or die $!; die "Bad header" if $header ne $header0; for(my $y = 0; $y < $height; $y++) { my $scanline; sysread($pipe, $scanline, $width) or die $!; defined($blackpixel) or $blackpixel = ord($scanline); print join '', map {($_ == $blackpixel) ? ' ' : 'X'} unpack 'C*', $scanline; print "\n"; } close $pipe; exit 0;