devmage has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise ones I seek your knowlege... Below is some legacy code I have for reading captions. Problem is some times it ends up reading in part of the image if there are no captions. It seems to only do this on certain types of jpegs. I've tried many things to detect the binary data. Can you help? I have tried JPEG::JFIF the only module I have found that can do it but it does not read captions from enough formats of jpegs for my needs.
binmode PHOTO_IN; #Set filemode to bin for good measure $/= "\x42\x49\x4D\x03"; #Input Record Delimeter. $cnt=0; #We want 1st of 2 records. while (<PHOTO_IN>) { #Read Photo... if ("$cnt"<1) { # Have Photo Header... $photo_header="$_"; # Save Photo Header... ++$cnt; # Bump count to skip photo. } } $photo_header =~ s/\x1C\x02/XYZZY/g; # Replace hex markers with text. $photo_header =~ s/[\n\r]/\x20/g; # Replace newlines with space. @fields=split(/XYZ/,$photo_header); # Set array using text marker. $n=2; # Start at 2 to skip junk. ITEM: while ( "$fields[$n]" ne "" ) {# Process all array elements. $fields[$n] =~ s/ZY\x78../Caption : /; # Set Caption Text Marker. if ($fields[$n] =~ /^Caption/) { # Save Caption Text for database. $caption=""; $caption=substr($fields[$n],10); #Apply the filters to the caption $caption =~ s/(\s*)$//g; $caption =~ s/'/''/g; $caption =~ s/\n//g; $caption =~ tr/a-zA-Z0-9\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\ +]\\\:\"\;\'\<\>\?\,\.\/ \t//cd; $caption =~ s/8BIM.*$//g; } $n++; }#endofwhile

Replies are listed 'Best First'.
Re: Grabbing Jpeg Captions
by CombatSquirrel (Hermit) on Sep 30, 2003 at 16:15 UTC
    I don't have a complete solution for you, but if you look at the following chunk:
    $cnt=0; #We want 1st of 2 records. while (<PHOTO_IN>) { #Read Photo... if ("$cnt"<1) { # Have Photo Header... $photo_header="$_"; # Save Photo Header... ++$cnt; # Bump count to skip photo. } }
    you will realize that all it really does is store the first "line" of the file into $photo_header. Just replace this block with
    $photo_header = <PHOTO_IN>;
    and you'll save a lot of time by not reading the rest of the file.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
      I am by no means an "Expert" perl coder. I've been using it for a while for various things though and consider my self competent. I wasn't horribly familiar with what this is doing. I do see what you mean but judging by what it actually does its another story. I've spent alot of time just looking at Jpegs in a hex editor. The caption information is stored either near the top or near the bottom. But not exactly in the first few lines. The full function I'm using is below. If you are interested in seeing it in action. I recently added a size check on the caption which helps but isn't really a fix. It is definetly getting more than the first few lines. This code does successfully pull the captions of files who have the captions at the bottom of the jpeg.
      #Function to pull the caption data from a jpg image. #Expects the filename to be passed to it sub get_photo_caption { $jpg_filename = shift; #Grab the filename passed open(PHOTO_IN,"$jpg_filename"); #Open the file binmode PHOTO_IN; #Set filemode to bin for good meas +ure $/= "\x42\x49\x4D\x03"; #Input Record Delimeter. Works for + Photoshop 5.5 - 7.0 $cnt=0; #We want 1st of 2 records. while (<PHOTO_IN>) { #Read Photo... if($_ =~ /\x4a\x46\x49\x46/) { #We are looking for JFIF in the he +ader to avoid miss reading captions $JFIF = 1; } if ("$cnt"<1) { # Have Photo Header... $photo_header="$_"; # Save Photo Header... ++$cnt; # Bump count to skip photo. } } $photo_header =~ s/\x1C\x02/XYZZY/g; # Replace hex markers with tex +t. $photo_header =~ s/[\n\r]/\x20/g; # Replace newlines with space. @fields=split(/XYZ/,$photo_header); # Set array using text marker. $n=2; # Start at 2 to skip j +unk. ITEM: while ( "$fields[$n]" ne "" ) { # Process all array el +ements. $fields[$n] =~ s/ZY\x78../Caption : /; # Set Caption Text Marker +. if ($fields[$n] =~ /^Caption/) { # Save Caption Text for databa +se. $caption=""; $caption=substr($fields[$n],10); #Apply the filters to the caption $caption =~ s/(\s*)$//g; $caption =~ s/'/''/g; $caption =~ s/\n//g; $caption =~ tr/a-zA-Z0-9\`\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\ +}\|\[\]\\\:\"\;\'\<\>\?\,\.\/ \t//cd; $caption =~ s/8BIM.*$//g; } $n++; }#endofwhile #If JFIF wasn't equal to 1 then we probably got bogus caption inform +ation so clear out the caption variable if($JFIF != 1) { $caption = ''; } #Photoshop only allows for 2000 character captions, if its bigger th +an that we probably got garbage. $captionsize = length($caption); if($captionsize > 2000) { $caption = ''; } chomp($caption); #For good measure remove any line feeds or CR that + may be on the end of the string close(PHOTO_IN); #Close our file $JFIF = 0; #Reset the JFIF value for the next time around. }#endofsub
Re: Grabbing Jpeg Captions
by MidLifeXis (Monsignor) on Sep 30, 2003 at 15:51 UTC
      I remember seeing Image::Info in my searches but it didn't look like it read captions. I downloaded and tried it anyways to see and it doesn't. I thought perhaps "Comments" could some how translate into being captions but no such luck. Thank you for the sugestion though.

        Since I don't know what a JPEG caption is... :)

        I assume that you did a print Data::Dumper->Dumper(image_info(foo)...) and it isn't hidden in there somewhere.

        What is a JPEG caption? Perhaps a patch to the Image::Info code would be appropriate.

Re: Grabbing Jpeg Captions
by menolly (Hermit) on Sep 30, 2003 at 22:52 UTC
    What about Image::Magick? Overkill, maybe, and I don't know if it'll work any better than the other modules mentioned, but might be worth a try.
      Definetly probably over kill. This is running on a windoze machine as well so that would present more of a challenge with Image::Magick but I know its possible. I looked at it briefly but didn't see any mechanism in it for pulling captions.