#!/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 .= ""; my $current_color = get_hex_color($image,0,$i); my $colspan = "0"; 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 $current_color) { $colspan++; } else { # write previous column $table .= write_cell($colspan,$current_color); # new color $colspan = 1; $current_color = $c; } } # write the final cell for this row $table .= write_cell($colspan,$current_color); $table .= "\n"; } $table .= "
"; # # Print the table along with the style # my $table_width = $pixel_size * $width; my $table_height = $pixel_size * $height; print qq|
$table
|; exit(0); sub write_cell { my $colspan = shift; my $color = shift; my $cell = ""; my $span = ""; if ($colspan>1) { $span = qq| colspan="$colspan"|; } if ($color eq '#ffffff') { # we can save a little space by using the white background $cell = qq||; } else { $cell = qq||; } return $cell; } 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); }