in reply to Grabbing Jpeg Captions

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.

Replies are listed 'Best First'.
Re: Re: Grabbing Jpeg Captions
by devmage (Initiate) on Sep 30, 2003 at 17:25 UTC
    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