#!/usr/bin/perl use strict; use Image::Magick; my $file_name = shift || usage(); my $pixel_size = shift || 1; my $image = Image::Magick->new; $image->Read($file_name); my $width = $image->Get('width'); my $height = $image->Get('height'); # # Create the image table # my $table = qq||; for (my $i =0; $i<$height; $i++) { $table .= ""; for (my $j =0; $j<$width; $j++) { # convert the color of the pixel at location $i,$j to hex my $c = get_hex_color($image,$j,$i); if ($c eq '#ffffff') { # we can save a little space by not using the white background $table .= qq||; } else { $table .= qq||; } } $table .= "\n"; } $table .= "
"; # # Print the table along with the style # print qq|
$table
|; exit(0); sub get_hex_color { my $image = shift; my $x = shift; my $y = shift; my $c = ''; my @p = $image->GetPixel('x'=>$x,'y'=>$y); foreach my $p (@p) { $c = $c . sprintf("%.2x",int($p*255)); } return "#$c" } sub usage { print < image.html Example: $0 my_image.png 2 > my_image.html EOF ; exit(1); }