Reading JPEG comments

For a possibly more up to date version of this code see the tcllib jpeg module


 proc read_jpg_comments {file} {
 #: returns a list containing all the comments found in ''file''
    set fh [open $file r]
    fconfigure $fh -encoding binary -translation binary -eofchar {}
    if {[read $fh 2] != "\xFF\xD8"} { close $fh; return }
    set comments {}

    while {[read $fh 1] == "\xFF"} {
        binary scan [read $fh 3] aS type len
        set pos [tell $fh]
        if {$type == "\xFE"} {
            lappend comments [read $fh [expr $len - 2]]
        }
        seek $fh [expr {$pos + $len - 2}] start
    }
    return $comments
 }

 # Test:
  catch { console show }
  puts [ read_jpg_comments "pic.jpg" ]

--AF 19-12-03

TV Nice, works. Would you happen to know how to read other information, for instance I can use a camera which nicely records picture data and time and some cam settings, which explorer nicely pops up. It would be nice to be able to read that, too.

AF For that I think you need to see EXIF (deprecated) or the tcllib jpeg module.


See also