[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...extract the date & time from a JPEG? I noticed...

<< Back to: JPEG image compression FAQ, part 1/2

Question by holgerjakobs
Submitted on 8/17/2003
Related FAQ: JPEG image compression FAQ, part 1/2
Rating: Rate this question: Vote
How can I extract the date & time from a JPEG? I noticed that my Casio and Canon cameras put is into the files. How come the file format descriptions don't mention this information, which can come in handy.


Answer by bene
Submitted on 12/1/2003
Rating:  Rate this answer: Vote
Date and time from your camera have nothing to do with the jpeg format and everything to do with the settings of your camera. Before taking any more photos, check out your manual or the camera's menu and remove the option for date stamp. The only way to remove the date etc from your current images is to use image-editing software, usually the clone/rubber stamp tool will do the job

 

Answer by Patrick
Submitted on 12/2/2003
Rating:  Rate this answer: Vote
The date and time stamp in camera pictures is not specified in the JFIF format (and even least in the JPEG compression specification), but in the Exif 2.1 standard. Exif specifies the standard file format for camera pictures, which can either be baseline JPEG or noncompressed TIFF. In either case, Exif adds some additional information to the file, such as the time, in a TIFF tag. To synthetize the Exif standard, there is inside the JPEG file a TIFF image, itself containing a JPEG image (the thumbnail) !

 

Answer by Maarschalk
Submitted on 6/13/2004
Rating:  Rate this answer: Vote
In addition to Patricks answer:
I found it difficult to find or write libraries to extract Exif tags. Since I'm merely interested in the date-time I decided to search the first 1 kB of my JPEGs for a recent or near-future year number. The date-times are always in yyyy:MM:dd HH:mm:ss format. This simple method proved effective.

The ugly little VBscript below renames all Jpeg in the directory where the script resides, giving them a sortable name, preserving most of the original name. I hope the forum preserves tabs & newlines (and doesn't wrap too soon)..!


' This script renames JPEG files to its actual creation time.
' using metadata found in the file
' making it possible to sort together with otherones pictures


Set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.GetFolder(".")

nrFiles = 0
for each fl in f.files
        if timestampname(fl) then nrFiles = nrFiles +1
next


MsgBox("Renamed " & nrFiles & " jpg's)

function timestampname (f1)
    timestampname = false
    ext = "." & lcase(fso.getExtensionName(f1.path))
    if ext=".jpg" then
        nam=fso.getFileName(f1.path)
        nam=left(nam,len(nam)-len(ext))
        while asc(left(nam,1)) < asc("A") or left(nam,1)="_"
            nam=mid(nam,2)
        wend
        set file = fso.OpenTextFile(f1.path,1)
        jpg=file.Read(1000)
        file.close
        s=""
        jr = 0
        for jr = 1997 to 2005
            ofst = instr(jpg,jr&"")
            if ofst > 0 then exit for
        next
        if ofst = 0 then
                if instr(jpg,"PC100") then ofst = 169
                if instr(jpg,"FUJIFILM") then ofst = 241
                if instr(jpg,"KODAK") then ofst = 449
                if instr(jpg,"CYBERSHOT") then ofst = 211
            if instr(jpg,"MINOLTA") then ofst = 277
        end if
        if ofst = 0 then exit function
        s = mid(jpg,ofst,19)

        sn = ""
        sn = sn & mid(s,1,4) & "-" ' year
        sn = sn & mid(s,6,2) & "-" ' month
        sn = sn & mid(s,9,2) & "  " ' day
        sn = sn & mid(s,12,2)& ":" ' hour
        sn = sn & mid(s,15,2)& ":" ' minute
        sn = sn & mid(s,18,2)& "" ' second
'       msgbox(sn)
            
        dt=0
        on error resume next
        dt = CDate(sn)
         if dt=0 then exit function

        if instr(jpg,"MINOLTA") then
            dt = DateAdd("D",0,DateAdd("H",-1,DateAdd("N",-5,dt))) ' correction for this camera: adds Days, Hours and Minutes
            s = year(dt) & "-" & nl(month(dt)) & "-" & nl(day(dt))  & " " &  nl(Hour(dt))  & "." & nl(Minute(dt))  & "." &  nl(Second(dt))
        end if
        if s <> "" then
            sn = ""
            sn = sn & mid(s,1,4) & "-" ' year
            sn = sn & mid(s,6,2) & "-" ' month
            sn = sn & mid(s,9,2) & "_" ' day
            sn = sn & mid(s,12,2)& "." ' hour
            sn = sn & mid(s,15,2)& "." ' minute
            sn = sn & mid(s,18,2)& "_" ' second
            nam  = sn & nam & ".jpg"
            if nam <> fso.getFileName(f1.path) then
                fso.Movefile f1.path, nam  ' effective rename
                'fso.DeleteFile f1.path
            timestampname = true
            end if
        end if
    end if
end function

function nl(nnn)
        nl = right("00" & nnn,2)
end function

 

Answer by dmc
Submitted on 9/28/2005
Rating: Not yet rated Rate this answer: Vote
thats a nice script works great!

but is an error inside line 15 see:
MsgBox("Renamed " & nrFiles & " jpg's)

the last " are missed :) so is right:
MsgBox("Renamed " & nrFiles & " jpg's")

greetings

 

Answer by Lightwave
Submitted on 4/2/2007
Rating: Not yet rated Rate this answer: Vote
GOOD WORK!

Very very useful. I've just given it some extra tweak, to make it more accurate and capable of drilling into subdirectories.

' This script renames JPEG files to its actual creation time.
' using metadata found in the file
' making it possible to sort together with otherones pictures

path=Wscript.ScriptFullName
path=left(path,InStrRev(path,""))

Set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.GetFolder(path)

answer=msgbox("Renaming JPG files under the tree of " & path, vbOKCancel, "jpgRenamer")

if answer = vbOk then
    nrFiles = 0
    call renameInFolder(f)
    MsgBox("Renamed " & nrFiles & " jpg's")
else
    MsgBox "Cancelled", vbOKOnly, "jpgRenamer"
end if

sub renameInFolder (f)
    for each fl in f.files
        if timestampname(fl) then nrFiles = nrFiles +1
    next
    For Each oFSSubFolder In f.SubFolders
       renameInFolder (oFSSubFolder)
    Next
end sub

function timestampname (f1)
    timestampname = false
    ext = "." & lcase(fso.getExtensionName(f1.path))
    if ext=".jpg" then
        nam=fso.getFileName(f1.path)
        nam=left(nam,len(nam)-len(ext))
        while asc(left(nam,1)) < asc("A") or left(nam,1)="_"
            nam=mid(nam,2)
        wend
        set file = fso.OpenTextFile(f1.path,1)
        jpg=file.Read(1000)
        file.close
        s=""
        jr = 0
        for jr = 1997 to 2099
            ofst = instr(jpg,jr&":")

            if ofst = 0 or mid(jpg,ofst+4,1) <> ":" or mid(jpg,ofst+7,1) <> ":" or mid(jpg,ofst+10,1) <> " " or mid(jpg,ofst+13,1) <> ":" or mid(jpg,ofst+16,1) <> ":" then
           ofst = 0
            end if
            if ofst > 0 then exit for
        next
        if ofst = 0 then
                if instr(jpg,"PC100") then ofst = 169
                if instr(jpg,"FUJIFILM") then ofst = 241
                if instr(jpg,"KODAK") then ofst = 449
                if instr(jpg,"CYBERSHOT") then ofst = 211
            if instr(jpg,"MINOLTA") then ofst = 277
        end if
        if ofst = 0 then exit function
        s = mid(jpg,ofst,19)

   sn = ""
        sn = sn & mid(s,1,4) & "-" ' year
        sn = sn & mid(s,6,2) & "-" ' month
        sn = sn & mid(s,9,2) & "  " ' day
        sn = sn & mid(s,12,2)& ":" ' hour
        sn = sn & mid(s,15,2)& ":" ' minute
        sn = sn & mid(s,18,2)& "" ' second
            
        dt=0
        on error resume next
        dt = CDate(sn)
         if dt=0 then exit function

        if instr(jpg,"MINOLTA") then
            dt = DateAdd("D",0,DateAdd("H",-1,DateAdd("N",-5,dt))) ' correction for this camera: adds Days, Hours and Minutes
            s = year(dt) & "-" & nl(month(dt)) & "-" & nl(day(dt))  & " " &  nl(Hour(dt))  & "." & nl(Minute(dt))  & "." &  nl(Second(dt))
        end if
        if s <> "" then
            sn = ""
            sn = sn & mid(s,1,4) ' & "-" ' year
            sn = sn & mid(s,6,2) ' & "-" ' month
            sn = sn & mid(s,9,2) & "_" ' day
            sn = sn & mid(s,12,2)' & "." ' hour
            sn = sn & mid(s,15,2)' & "." ' minute
            sn = sn & mid(s,18,2) & "_" ' second
            nam  = sn & nam & ".jpg"
            if nam <> fso.getFileName(f1.path) then
                fso.Movefile f1.path, left(f1.path,InStrRev(f1.path,"")) & nam  
                timestampname = true
            end if
        end if
    end if
end function

function nl(nnn)
        nl = right("00" & nnn,2)
end function

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: JPEG image compression FAQ, part 1/2


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.