Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Convert Image to Text

by fockjef (Initiate)
on Jun 02, 2003 at 16:56 UTC ( #262428=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info fockjef@iit.edu
Description: This is an old project that i just rewrote in perl. It reads any type of image that ImageMagick supports, converts it to black&white, and then converts it into text. This requires the ImageMagick package. Also, it really helps to do some editing to the pictures to improve the results. Some examples of the output can be found at http://www.iit.edu/~fockjef/junk/ascii Peace jef
#!perl

use Image::Magick;    

initConstants();

$filename  = $ARGV[0];
$startChar = 32;
$endChar   = 127;

# Initialize Image Object
$image = Image::Magick->new;    

# Read in the Image
$image->Read($filename);

# Get the Width and Height
$height = $image->Get('height');
$width  = $image->Get('width' );

# Calculate the Number of Characters Required
$numRows = int($height/16); if($height%16){$numRows++;}
$numCols = int($width /8 ); if($width %8 ){$numCols++;}

# Convert the Image to B&W
if( $image->Get('colors') > 2 ){$image->Quantize(colors => 2);}

# Convert to the Raw MONO Format
$image->Set(magick=> 'MONO');

# Get an Array of the Bytes in the Image
@blob = split( //, $image->ImageToBlob());

# Iterate Through the Image Data One Character Block at a Time
for( $ypos = 0; $ypos < $numRows; $ypos++ )
{
    for( $xpos = 0; $xpos < $numCols; $xpos ++ )
    {
        # Get the Actual Image Data for the Current Character Block
        for( $i = 0; $i < 16; $i++ )
        {    
            $temp = ($ypos*16+$i)*$numCols+$xpos;
            if( $temp < scalar(@blob) )
            {
                $byte = ord($blob[$temp]);
            }
            else
            {
                $byte = 0;
            }
            $bitmap[$i] = $binary[$byte%16]*16+$binary[int($byte/16)];
        }
        
        # Find the Best Match for the Current Character Block
        $bestScore = 0;
        $bestChar = $startChar;
        for( $d = $startChar; $d <= $endChar; $d++ )
        {
            $score = 128;
            for( $e = 0; $e < 16; $e++ )
            {
                $temp = $bitmap[$e] ^ $charMap[$d][$e];
                while( $temp > 0 )
                {
                    $score -= $temp%2;
                    $temp = int($temp/2);
                }
            }
            if( $score > $bestScore )
            {
                $bestScore = $score;
                $bestChar = $d;
                if( $score == 128 )
                {
                    $d = $endChar;
                }
            }
        }
        print chr($bestChar);
    }
    print "\n";
}

sub initConstants
{
    @binary = ( 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 )
+;
    
    @charMap = (
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,126,129,165,129,129,165,153,129,129,126,0,0,0,0],
    [0,0,126,255,219,255,255,219,231,255,255,126,0,0,0,0],
    [0,0,0,0,108,254,254,254,254,124,56,16,0,0,0,0],
    [0,0,0,0,16,56,124,254,124,56,16,0,0,0,0,0],
    [0,0,0,24,60,60,231,231,231,24,24,60,0,0,0,0],
    [0,0,0,24,60,126,255,255,126,24,24,60,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [255,255,255,255,255,255,231,195,195,231,255,255,255,255,255,255],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,127,99,127,99,99,99,99,103,231,230,192,0,0,0],
    [0,0,0,24,24,219,60,231,60,219,24,24,0,0,0,0],
    [0,128,192,224,240,248,254,248,240,224,192,128,0,0,0,0],
    [0,2,6,14,30,62,254,62,30,14,6,2,0,0,0,0],
    [0,0,24,60,126,24,24,24,126,60,24,0,0,0,0,0],
    [0,0,102,102,102,102,102,102,102,0,102,102,0,0,0,0],
    [0,0,127,219,219,219,123,27,27,27,27,27,0,0,0,0],
    [0,124,198,96,56,108,198,198,108,56,12,198,124,0,0,0],
    [0,0,0,0,0,0,0,0,254,254,254,254,0,0,0,0],
    [0,0,24,60,126,24,24,24,126,60,24,126,0,0,0,0],
    [0,0,24,60,126,24,24,24,24,24,24,24,0,0,0,0],
    [0,0,24,24,24,24,24,24,24,126,60,24,0,0,0,0],
    [0,0,0,0,0,24,12,254,12,24,0,0,0,0,0,0],
    [0,0,0,0,0,48,96,254,96,48,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,24,60,60,60,24,24,24,0,24,24,0,0,0,0],
    [0,102,102,102,36,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,108,108,254,108,108,108,254,108,108,0,0,0,0],
    [24,24,124,198,194,192,124,6,6,134,198,124,24,24,0,0],
    [0,0,0,0,194,198,12,24,48,96,198,134,0,0,0,0],
    [0,0,56,108,108,56,118,220,204,204,204,118,0,0,0,0],
    [0,48,48,48,96,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,12,24,48,48,48,48,48,48,24,12,0,0,0,0],
    [0,0,48,24,12,12,12,12,12,12,24,48,0,0,0,0],
    [0,0,0,0,0,102,60,255,60,102,0,0,0,0,0,0],
    [0,0,0,0,0,24,24,126,24,24,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,24,24,24,48,0,0,0],
    [0,0,0,0,0,0,0,254,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,24,24,0,0,0,0],
    [0,0,0,0,2,6,12,24,48,96,192,128,0,0,0,0],
    [0,0,56,108,198,198,214,214,198,198,108,56,0,0,0,0],
    [0,0,24,56,120,24,24,24,24,24,24,126,0,0,0,0],
    [0,0,124,198,6,12,24,48,96,192,198,254,0,0,0,0],
    [0,0,124,198,6,6,60,6,6,6,198,124,0,0,0,0],
    [0,0,12,28,60,108,204,254,12,12,12,30,0,0,0,0],
    [0,0,254,192,192,192,252,6,6,6,198,124,0,0,0,0],
    [0,0,56,96,192,192,252,198,198,198,198,124,0,0,0,0],
    [0,0,254,198,6,6,12,24,48,48,48,48,0,0,0,0],
    [0,0,124,198,198,198,124,198,198,198,198,124,0,0,0,0],
    [0,0,124,198,198,198,126,6,6,6,12,120,0,0,0,0],
    [0,0,0,0,24,24,0,0,0,24,24,0,0,0,0,0],
    [0,0,0,0,24,24,0,0,0,24,24,48,0,0,0,0],
    [0,0,0,6,12,24,48,96,48,24,12,6,0,0,0,0],
    [0,0,0,0,0,126,0,0,126,0,0,0,0,0,0,0],
    [0,0,0,96,48,24,12,6,12,24,48,96,0,0,0,0],
    [0,0,124,198,198,12,24,24,24,0,24,24,0,0,0,0],
    [0,0,0,124,198,198,222,222,222,220,192,124,0,0,0,0],
    [0,0,16,56,108,198,198,254,198,198,198,198,0,0,0,0],
    [0,0,252,102,102,102,124,102,102,102,102,252,0,0,0,0],
    [0,0,60,102,194,192,192,192,192,194,102,60,0,0,0,0],
    [0,0,248,108,102,102,102,102,102,102,108,248,0,0,0,0],
    [0,0,254,102,98,104,120,104,96,98,102,254,0,0,0,0],
    [0,0,254,102,98,104,120,104,96,96,96,240,0,0,0,0],
    [0,0,60,102,194,192,192,222,198,198,102,58,0,0,0,0],
    [0,0,198,198,198,198,254,198,198,198,198,198,0,0,0,0],
    [0,0,60,24,24,24,24,24,24,24,24,60,0,0,0,0],
    [0,0,30,12,12,12,12,12,204,204,204,120,0,0,0,0],
    [0,0,230,102,102,108,120,120,108,102,102,230,0,0,0,0],
    [0,0,240,96,96,96,96,96,96,98,102,254,0,0,0,0],
    [0,0,198,238,254,254,214,198,198,198,198,198,0,0,0,0],
    [0,0,198,230,246,254,222,206,198,198,198,198,0,0,0,0],
    [0,0,124,198,198,198,198,198,198,198,198,124,0,0,0,0],
    [0,0,252,102,102,102,124,96,96,96,96,240,0,0,0,0],
    [0,0,124,198,198,198,198,198,198,214,222,124,12,14,0,0],
    [0,0,252,102,102,102,124,108,102,102,102,230,0,0,0,0],
    [0,0,124,198,198,96,56,12,6,198,198,124,0,0,0,0],
    [0,0,126,126,90,24,24,24,24,24,24,60,0,0,0,0],
    [0,0,198,198,198,198,198,198,198,198,198,124,0,0,0,0],
    [0,0,198,198,198,198,198,198,198,108,56,16,0,0,0,0],
    [0,0,198,198,198,198,214,214,214,254,238,108,0,0,0,0],
    [0,0,198,198,108,124,56,56,124,108,198,198,0,0,0,0],
    [0,0,102,102,102,102,60,24,24,24,24,60,0,0,0,0],
    [0,0,254,198,134,12,24,48,96,194,198,254,0,0,0,0],
    [0,0,60,48,48,48,48,48,48,48,48,60,0,0,0,0],
    [0,0,0,128,192,224,112,56,28,14,6,2,0,0,0,0],
    [0,0,60,12,12,12,12,12,12,12,12,60,0,0,0,0],
    [16,56,108,198,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0],
    [48,48,24,0,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,0,224,96,96,120,108,102,102,102,102,124,0,0,0,0],
    [0,0,0,0,0,124,198,192,192,192,198,124,0,0,0,0],
    [0,0,28,12,12,60,108,204,204,204,204,118,0,0,0,0],
    [0,0,0,0,0,124,198,254,192,192,198,124,0,0,0,0],
    [0,0,56,108,100,96,240,96,96,96,96,240,0,0,0,0],
    [0,0,0,0,0,118,204,204,204,204,204,124,12,204,120,0],
    [0,0,224,96,96,108,118,102,102,102,102,230,0,0,0,0],
    [0,0,24,24,0,56,24,24,24,24,24,60,0,0,0,0],
    [0,0,6,6,0,14,6,6,6,6,6,6,102,102,60,0],
    [0,0,224,96,96,102,108,120,120,108,102,230,0,0,0,0],
    [0,0,56,24,24,24,24,24,24,24,24,60,0,0,0,0],
    [0,0,0,0,0,236,254,214,214,214,214,198,0,0,0,0],
    [0,0,0,0,0,220,102,102,102,102,102,102,0,0,0,0],
    [0,0,0,0,0,124,198,198,198,198,198,124,0,0,0,0],
    [0,0,0,0,0,220,102,102,102,102,102,124,96,96,240,0],
    [0,0,0,0,0,118,204,204,204,204,204,124,12,12,30,0],
    [0,0,0,0,0,220,118,102,96,96,96,240,0,0,0,0],
    [0,0,0,0,0,124,198,96,56,12,198,124,0,0,0,0],
    [0,0,16,48,48,252,48,48,48,48,54,28,0,0,0,0],
    [0,0,0,0,0,204,204,204,204,204,204,118,0,0,0,0],
    [0,0,0,0,0,102,102,102,102,102,60,24,0,0,0,0],
    [0,0,0,0,0,198,198,214,214,214,254,108,0,0,0,0],
    [0,0,0,0,0,198,108,56,56,56,108,198,0,0,0,0],
    [0,0,0,0,0,198,198,198,198,198,198,126,6,12,248,0],
    [0,0,0,0,0,254,204,24,48,96,198,254,0,0,0,0],
    [0,0,14,24,24,24,112,24,24,24,24,14,0,0,0,0],
    [0,0,24,24,24,24,0,24,24,24,24,24,0,0,0,0],
    [0,0,112,24,24,24,14,24,24,24,24,112,0,0,0,0],
    [0,0,118,220,0,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,16,56,108,198,198,198,254,0,0,0,0,0],
    [0,0,60,102,194,192,192,192,194,102,60,12,6,124,0,0],
    [0,0,204,0,0,204,204,204,204,204,204,118,0,0,0,0],
    [0,12,24,48,0,124,198,254,192,192,198,124,0,0,0,0],
    [0,16,56,108,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,0,204,0,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,96,48,24,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,56,108,56,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,0,0,0,60,102,96,96,102,60,12,6,60,0,0,0],
    [0,16,56,108,0,124,198,254,192,192,198,124,0,0,0,0],
    [0,0,198,0,0,124,198,254,192,192,198,124,0,0,0,0],
    [0,96,48,24,0,124,198,254,192,192,198,124,0,0,0,0],
    [0,0,102,0,0,56,24,24,24,24,24,60,0,0,0,0],
    [0,24,60,102,0,56,24,24,24,24,24,60,0,0,0,0],
    [0,96,48,24,0,56,24,24,24,24,24,60,0,0,0,0],
    [0,198,0,16,56,108,198,198,254,198,198,198,0,0,0,0],
    [56,108,56,0,56,108,198,198,254,198,198,198,0,0,0,0],
    [24,48,96,0,254,102,96,124,96,96,102,254,0,0,0,0],
    [0,0,0,0,0,204,118,54,126,216,216,110,0,0,0,0],
    [0,0,62,108,204,204,254,204,204,204,204,206,0,0,0,0],
    [0,16,56,108,0,124,198,198,198,198,198,124,0,0,0,0],
    [0,0,198,0,0,124,198,198,198,198,198,124,0,0,0,0],
    [0,96,48,24,0,124,198,198,198,198,198,124,0,0,0,0],
    [0,48,120,204,0,204,204,204,204,204,204,118,0,0,0,0],
    [0,96,48,24,0,204,204,204,204,204,204,118,0,0,0,0],
    [0,0,198,0,0,198,198,198,198,198,198,126,6,12,120,0],
    [0,198,0,124,198,198,198,198,198,198,198,124,0,0,0,0],
    [0,198,0,198,198,198,198,198,198,198,198,124,0,0,0,0],
    [0,24,24,60,102,96,96,96,102,60,24,24,0,0,0,0],
    [0,56,108,100,96,240,96,96,96,96,230,252,0,0,0,0],
    [0,0,102,102,60,24,126,24,126,24,24,24,0,0,0,0],
    [0,248,204,204,248,196,204,222,204,204,204,198,0,0,0,0],
    [0,14,27,24,24,24,126,24,24,24,24,24,216,112,0,0],
    [0,24,48,96,0,120,12,124,204,204,204,118,0,0,0,0],
    [0,12,24,48,0,56,24,24,24,24,24,60,0,0,0,0],
    [0,24,48,96,0,124,198,198,198,198,198,124,0,0,0,0],
    [0,24,48,96,0,204,204,204,204,204,204,118,0,0,0,0],
    [0,0,118,220,0,220,102,102,102,102,102,102,0,0,0,0],
    [118,220,0,198,230,246,254,222,206,198,198,198,0,0,0,0],
    [0,60,108,108,62,0,126,0,0,0,0,0,0,0,0,0],
    [0,56,108,108,56,0,124,0,0,0,0,0,0,0,0,0],
    [0,0,48,48,0,48,48,96,192,198,198,124,0,0,0,0],
    [0,0,0,0,0,0,254,192,192,192,192,0,0,0,0,0],
    [0,0,0,0,0,0,254,6,6,6,6,0,0,0,0,0],
    [0,192,192,194,198,204,24,48,96,220,134,12,24,62,0,0],
    [0,192,192,194,198,204,24,48,102,206,158,62,6,6,0,0],
    [0,0,24,24,0,24,24,24,60,60,60,24,0,0,0,0],
    [0,0,0,0,0,54,108,216,108,54,0,0,0,0,0,0],
    [0,0,0,0,0,216,108,54,108,216,0,0,0,0,0,0],
    [17,68,17,68,17,68,17,68,17,68,17,68,17,68,17,68],
    [85,170,85,170,85,170,85,170,85,170,85,170,85,170,85,170],
    [221,119,221,119,221,119,221,119,221,119,221,119,221,119,221,119],
    [24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,24,24,248,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,248,24,248,24,24,24,24,24,24,24,24],
    [54,54,54,54,54,54,54,246,54,54,54,54,54,54,54,54],
    [0,0,0,0,0,0,0,254,54,54,54,54,54,54,54,54],
    [0,0,0,0,0,248,24,248,24,24,24,24,24,24,24,24],
    [54,54,54,54,54,246,6,246,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54],
    [0,0,0,0,0,254,6,246,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,246,6,254,0,0,0,0,0,0,0,0],
    [54,54,54,54,54,54,54,254,0,0,0,0,0,0,0,0],
    [24,24,24,24,24,248,24,248,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,248,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,24,24,31,0,0,0,0,0,0,0,0],
    [24,24,24,24,24,24,24,255,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,255,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,24,24,31,24,24,24,24,24,24,24,24],
    [0,0,0,0,0,0,0,255,0,0,0,0,0,0,0,0],
    [24,24,24,24,24,24,24,255,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,31,24,31,24,24,24,24,24,24,24,24],
    [54,54,54,54,54,54,54,55,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,55,48,63,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,63,48,55,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,247,0,255,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,255,0,247,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,55,48,55,54,54,54,54,54,54,54,54],
    [0,0,0,0,0,255,0,255,0,0,0,0,0,0,0,0],
    [54,54,54,54,54,247,0,247,54,54,54,54,54,54,54,54],
    [24,24,24,24,24,255,0,255,0,0,0,0,0,0,0,0],
    [54,54,54,54,54,54,54,255,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,255,0,255,24,24,24,24,24,24,24,24],
    [0,0,0,0,0,0,0,255,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,54,54,63,0,0,0,0,0,0,0,0],
    [24,24,24,24,24,31,24,31,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,31,24,31,24,24,24,24,24,24,24,24],
    [0,0,0,0,0,0,0,63,54,54,54,54,54,54,54,54],
    [54,54,54,54,54,54,54,255,54,54,54,54,54,54,54,54],
    [24,24,24,24,24,255,24,255,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,24,24,248,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,31,24,24,24,24,24,24,24,24],
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
    [0,0,0,0,0,0,0,255,255,255,255,255,255,255,255,255],
    [240,240,240,240,240,240,240,240,240,240,240,240,240,240,240,240],
    [15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15],
    [255,255,255,255,255,255,255,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,118,220,216,216,216,220,118,0,0,0,0],
    [0,0,120,204,204,204,216,204,198,198,198,204,0,0,0,0],
    [0,0,254,198,198,192,192,192,192,192,192,192,0,0,0,0],
    [0,0,0,0,254,108,108,108,108,108,108,108,0,0,0,0],
    [0,0,0,254,198,96,48,24,48,96,198,254,0,0,0,0],
    [0,0,0,0,0,126,216,216,216,216,216,112,0,0,0,0],
    [0,0,0,0,102,102,102,102,102,124,96,96,192,0,0,0],
    [0,0,0,0,118,220,24,24,24,24,24,24,0,0,0,0],
    [0,0,0,126,24,60,102,102,102,60,24,126,0,0,0,0],
    [0,0,0,56,108,198,198,254,198,198,108,56,0,0,0,0],
    [0,0,56,108,198,198,198,108,108,108,108,238,0,0,0,0],
    [0,0,30,48,24,12,62,102,102,102,102,60,0,0,0,0],
    [0,0,0,0,0,126,219,219,219,126,0,0,0,0,0,0],
    [0,0,0,3,6,126,219,219,243,126,96,192,0,0,0,0],
    [0,0,28,48,96,96,124,96,96,96,48,28,0,0,0,0],
    [0,0,0,124,198,198,198,198,198,198,198,198,0,0,0,0],
    [0,0,0,0,254,0,0,254,0,0,254,0,0,0,0,0],
    [0,0,0,0,24,24,126,24,24,0,0,255,0,0,0,0],
    [0,0,0,48,24,12,6,12,24,48,0,126,0,0,0,0],
    [0,0,0,12,24,48,96,48,24,12,0,126,0,0,0,0],
    [0,0,14,27,27,24,24,24,24,24,24,24,24,24,24,24],
    [24,24,24,24,24,24,24,24,216,216,216,112,0,0,0,0],
    [0,0,0,0,24,24,0,126,0,24,24,0,0,0,0,0],
    [0,0,0,0,0,118,220,0,118,220,0,0,0,0,0,0],
    [0,56,108,108,56,0,0,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,24,24,0,0,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,24,0,0,0,0,0,0,0],
    [0,15,12,12,12,12,12,236,108,108,60,28,0,0,0,0],
    [0,216,108,108,108,108,108,0,0,0,0,0,0,0,0,0],
    [0,112,216,48,96,200,248,0,0,0,0,0,0,0,0,0],
    [0,0,0,0,124,124,124,124,124,124,124,0,0,0,0,0],
    [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    );
}
Replies are listed 'Best First'.
Re: Convert Image to Text (program repair shop)
by Aristotle (Chancellor) on Jun 02, 2003 at 23:47 UTC
    That looked so much like C I thought I'd rewrite it in Perl. *g*
    #!/usr/bin/perl -w use strict; use POSIX qw(ceil); use List::Util qw(reduce sum); use Image::Magick; use constant STARTCHAR => 32; use constant ENDCHAR => 127; use constant CHARLIST => STARTCHAR .. ENDCHAR; my @charmap = map { chomp; [split] } <DATA>; my @binary = (0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15); my ($rows, $cols, @blob) = do { my $image = Image::Magick->new; $image->Read($ARGV[0]); $image->Quantize(colors => 2) if $image->Get('colors') > 2; $image->Set(magick => 'MONO'); ceil($image->Get('height') / 16), ceil($image->Get('width') / 8), split //, $image->ImageToBlob(); }; for my $y (0 .. $rows - 1) { for my $x (0 .. $cols - 1) { my @score = map { my $char = $_; 128 - sum map { my $byte = ord($blob[ ($y * 16 + $_) * $cols + $x ] || + chr 0); my $bitmap = $binary[ $byte / 16 ] + $binary[ $byte % +16 ] * 16; unpack("B*", chr($bitmap^$charmap[$char][$_])) =~ tr/1 +/1/ } 0 .. 15; } CHARLIST; print chr(reduce { $score[$a - STARTCHAR] >= $score[$b - STARTCHAR] ? $a : $b } CHARLIST); } print "\n"; }
    Initialization data follows:

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://262428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-09-30 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (155 votes). Check out past polls.

    Notices?