#28 Image Information

Digital cameras store a lot of information about a photograph in a hidden encoding in the image. Perl can make this information visible.

The Code

  1 #!/usr/bin/perl
  2 use strict;
  3 use warnings;
  4
  5 my %good = (
  6     'ColorSpace' => 1,
  7     'ComponentsConfiguration' => 1,
  8     'DateTime' => 1,
  9     'DateTimeDigitized' => 1,
 10     'DateTimeOriginal' => 1,
 11     'ExifImageLength' => 1,
 12     'ExifImageWidth' => 1,
 13     'ExifVersion' => 1,
 14     'FileSource' => 1,
 15     'Flash' => 1,
 16     'FlashPixVersion' => 1,
 17     'ISOSpeedRatings' => 1,
 18     'ImageDescription' => 1,
 19     'InteroperabilityIndex' => 1,
 20     'InteroperabilityVersion' => 1,
 21     'JPEG_Type' => 1,
 22     'LightSource' => 1,
 23     'Make' => 1,
 24     'MeteringMode' => 1,
 25     'Model' => 1,
 26     'Orientation' => 1,
 27     'SamplesPerPixel' => 1,
 28     'Software' => 1,
 29     'YCbCrPositioning' => 1,
 30     'color_type' => 1,
 31     'file_ext' => 1,
 32     'file_media_type' => 1,
 33     'height' => 1,
 34     'resolution' => 1,
 35     'width' => 1
 36 );
 37
 38 use Image::Info qw(image_info);
 39
 40
 41 foreach my $cur_file (@ARGV) {
 42     my $info = image_info($cur_file);
 43
 44     print "$cur_file ----------------------------------
";
 45     foreach my $key (sort keys %$info) {
 46         if ($good{$key}) {
 47             print "    $key -> $info->{$key}
";
 48         }
 49     }
 50 }

Running the Script

To run the script, just type the names of the files you're interested in on the command line.

The Results

The result is a lot of information from the photograph.

p2230148.jpg ----------------------------------
    ColorSpace -> 1
    ComponentsConfiguration -> YCbCr
    DateTime -> 2001:02:23 18:07:45
    DateTimeDigitized -> 2001:02:23 18:07:45
    DateTimeOriginal -> 2001:02:23 18:07:45
    ExifImageLength -> 960
    ExifImageWidth -> 1280
    ExifVersion -> 0210
    FileSource -> (DSC) Digital Still Camera
    Flash -> Flash fired
    FlashPixVersion -> 0100
    ISOSpeedRatings -> 125
    ImageDescription -> OLYMPUS DIGITAL CAMERA
    InteroperabilityIndex -> R98
    InteroperabilityVersion -> 0100
    JPEG_Type -> Baseline
    LightSource -> unknown
    Make -> OLYMPUS OPTICAL CO.,LTD
    MeteringMode -> Pattern
    Model -> C960Z,D460Z
    Orientation -> top_left
    SamplesPerPixel -> 3
    Software -> v874u-74
    YCbCrPositioning -> 2
    color_type -> YCbCr
    file_ext -> jpg
    file_media_type -> image/jpeg
    height -> 960
    resolution -> 72 dpi
    width -> 1280

How It Works

JPEG and some other image file formats store information inside the files. Because JPEG was designed for digital cameras, a lot of this information has to do with the camera and how the photograph was taken. The Perl module Image::Info knows all about the JPEG standard for embedded information and how to extract that information.

So to get the data you want, all you do is call the Image::Info function image_info and print the results (sort of):

 41 foreach my $cur_file (@ARGV) {
 42     my $info = image_info($cur_file);

You need to print the results, but there is a small problem. Not all the information is scalar. Sometimes references to arrays or hash references are returned. Also, some results are binary and don't print well.

So in this program, you limit the values you print to the "good" stuff, stuff you know will print nicely:

 45     foreach my $key (sort keys %$info) {
 46         if ($good{$key}) {
 47             print "    $key -> $info->{$key}
";
 48         }
 49     }
 50 }

Hacking the Script

A clever programmer could print everything. For example, the program can be hacked to detect whether or not the data is binary and transform it into something useful. You could also detect complex data values (arrays, hashes, arrays of hashes, etc.) and print them as well.

It all depends on what you want to get out of your camera. This script gets everything, but once you decide what's useful, it shouldn't be too hard to cut it down so only the good stuff is printed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset