#!/usr/bin/perl # # Convert the single image specified upon the command-line # into a series of lines. # # mogrify -resize 400x300\! $input # convert $input -monochrome $output # perl export.pl $filename # use strict; use warnings; use Image::Magick; # Get the image-filename from the command-line my $arg = shift; die "Usage: $0 image.ext" unless ( defined($arg) && -e $arg ); # Create an image-helper. my $IMimage = Image::Magick->new; $IMimage->Read($arg); # Dimensions my $X = 400; my $Y = 300; # Process my $y = 0; while ( $y < $Y ) { my $x = 0; my $blackstart = undef; while ( $x < $X ) { my $pixel = $IMimage->Get( 'pixel[' . $x . ',' . $y . ']' ); # # In the past we used to look for "0,0,0,0" to mean "black" # and "65535,65535,65535,0" to mean white. # # However sometimes we see values that are "low but non-zero" # so we now count them as black if less than 50%. # my ( $r, $g, $b, $d ) = split( /,/, $pixel ); my $threshold = 65535 / 2; # # If less than the threshold, which is a bit arbitrary, then # we pretend the pixels are solid black. # if ( ( $r < $threshold ) && ( $g < $threshold ) && ( $b < $threshold ) ) { if ( !defined $blackstart ) { $blackstart = $x; } } else { # white - so the black has ended if ( defined $blackstart ) { # # NOTE: we're at the next pixel so the colour # finished at the previous coordinate. my $e = $x - 1; print "$y,$blackstart,$y,$e\n"; $blackstart = undef; } } $x += 1; } # Still in a run of black at the end of the row? if ( defined $blackstart ) { print "$y,$blackstart,$y,$X\n"; $blackstart = undef; } $y += 1; }