Extract Dilbert image from MacKay file

This extracts the figure of Dilbert from figure6.eps

#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use Image::PNG::Libpng ':all';
use Image::PNG::Const ':all';
my $file = 'figure6.eps';
open my $in, "<", $file or die $!;
my @rows;
my $in_image;
my $image_no = 1;
while (<$in>) {
    if (/^image$/) {
        $in_image = 1;
        next;
    }
    if ($in_image) {
        my $line = $_;
        $line =~ s/\n$//;
        if (! length $line) {
            $in_image = 0;
            write_image (\@rows, $image_no);
            $image_no++;
            @rows = ();
            next;
        }
        my $bytes = pack "H*", $line;
        push @rows, $bytes;
    }
}
close $in or die $!;
exit;

sub write_image
{
    my ($rows, $image_no) = @_;
    open my $pngfile, ">:raw", "out$image_no.png" or die $!;
    my $png = create_write_struct ();
    set_IHDR ($png, {
        width => 200,
        height => 200,
        bit_depth => 1,
        color_type => PNG_COLOR_TYPE_GRAY,
    });
    init_io ($png, $pngfile);
    set_rows ($png, $rows);
    write_png ($png);
    close $pngfile or die $!;
}

(download)


Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer