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

In reply to Re: Re: Grabbing Jpeg Captions by devmage
in thread Grabbing Jpeg Captions by devmage

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.