How Can I Tell Whether a .GIF File is a Regular Picture File or an Animated Picture File?
Hey, Scripting Guy! I have a silly question for you: how can I tell whether a .GIF file is a regular old picture file or an animated picture file?
Hey, WW. To begin with, there’s no such thing as a silly question. We’ve always believed that – well, come to think of it, last summer a neighbor’s smoldering firework caught one of the Scripting Guys’ garages on fire. As he stood there in the middle of the night, talking to the fire chief and watching the fire fighters put out the fire, another neighbor rushed up to him and said, “Oh, my gosh: did you know that your house is on fire!?!”
So, in retrospect, maybe there is such a thing as a silly question. We stand corrected.
To tell you the truth, WW, we didn’t find your question silly. However, we did assume that there was no possible way to retrieve that information using a script. After all, we can’t manage our wireless settings using a script; what do you suppose the odds are that we could use a script to determine whether or not a .GIF file was animated?
As it turns out, those odds are surprisingly good:
Set objImage = WScript.CreateObject(“WIA.ImageFile”) objImage.LoadFile(“c:\scripts\dr_scripto_anim.gif”)
If objImage.IsAnimated Then Wscript.Echo “This is an animated image.” Else Wscript.Echo “This is not an animated image.” End if
The trick here is to use the Windows Image Acquisition (WIA) 2.0 library. Now, it’s possible that you don’t have WIA installed on your machine; if that’s the case you can download everything you need from here. Note that, for some reason, you need to download the entire WIA SDK (which is just a 520KB .zip file). Download the SDK and extract all the files to a folder of your choice, then use Regsvr32.exe to register the Wiaaut DLL file:
Note. How would you know that you don’t have WIA 2.0 installed on your computer? One easy way to tell is to try running the script. If it fails with a “can’t create object” error then you don’t have WIA 2.0 installed.
Once you have WIA installed the rest is easy. We start out by creating an instance of the WIA.ImageFile object, then use the LoadFile method to bind to the file we want to check. (In this case, that’s C:\Scripts\Dr_Scripto_Anim.gif.) We then check the value of the IsAnimated property. If IsAnimated is True then we’re dealing with an animated picture file. If IsAnimated is False then our picture file is not animated.
Like we said, we had no idea you could even do this, let alone know that it would be this easy.
To be honest, we haven’t played around with the WIA scripting library very much. It looks interesting, though: after all, if you have the right equipment, you can use scripts to control such things as scanners and Web cameras. If that piques your interest, take a look at the Windows Image Acquisition SDK on MSDN.
Of course, once we got started checking to see if our picture files were animated, well, it was pretty hard to stop. With that in mind we thought we’d toss in a bonus script, one that checks all the files in a folder and reports back any that happen to be animated picture files. We won’t discuss this script in any great detail, but we will tell you what it does:
It uses the FileSystemObject to bind to the folder C:\Scripts (using the GetFolder method).
It creates an object reference named colFiles that contains all the files found in C:\Scripts.
For each file in the folder it tries loading the file using the LoadFile method. As you might expect, LoadFile can only load image files; it can’t load, say, Word documents or executable files. Therefore each time the script calls LoadFile it checks the value of the VBScript Err object. If Err is equal to 0 that means the file loaded successfully; if Err is anything but 0 that means the file could be loaded.
For each file successfully loaded the script checks the value of the IsAnimated property; it then echoes back the file name if IsAnimated is True. If a file cannot be loaded the script simply skips this step altogether.
The script resets the Err object back to 0, then loops around and repeats the process with the next file in the collection.
Here’s what the code looks like:
On Error Resume Next
Set objImage = WScript.CreateObject(“WIA.ImageFile”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFolder = objFSO.GetFolder(“C:\Scripts”)
Set colFiles = objFolder.Files
For Each objFile in colFiles strFile = “C:\Scripts\” & objFile.Name objImage.LoadFile(strFile) If Err = 0 Then If objImage.IsAnimated Then Wscript.Echo objFile.Name End If End If Err.Clear Next
And, in case you’re wondering, yes, this Scripting Guy was aware that his house was on fire. Painfully aware, as they say.