How Can I Display the Vertical and Horizontal Sizes of a .JPG File?
Hey, Scripting Guy! How can I display the vertical and horizontal resolutions of a .JPG file?
Hey, ST. This is a very commonly-asked question, and for the longest time we had no answer for it: nothing built into the operating system seemed capable of returning this information, and even Dsofile, a COM object released by the Microsoft Office team specifically to return the values of extended properties like this, can’t get us picture sizes. The whole thing seemed hopeless.
But just because things looked bleak does that mean we gave up? Well, to tell you the truth, yes. But then we pieced together a Fun Zone article on using scripts to manage music files and playlists, and purely by accident we stumbled upon a workaround: as long as you import all your .JPG files into Windows Media Player you can use scripts to return information about the file, such as a picture’s vertical and horizontal size.
Now keep in mind that, by default, Windows Media Player doesn’t automatically keep track of photo items. To add .JPG files to your media collection from inside Windows Media Player you’ll need to click File and then select one of the options under Add to Library. Before you can do that, however, you must first click Tools, click Options, and then, on the Player tab, select Enable picture support for devices. If you don’t enable picture support, Windows Media Player will tell you that .JPG is an unsupported file extension. Enable picture support, then start adding pictures to the media collection.
Note. This doesn’t move your files or convert your .JPGs to some sort of proprietary Windows Media Player format; in fact, it doesn’t do anything to your files other than make them known to the Windows Media Player library.
After your .JPG files are known to Windows Media Player than you can use a script like this to retrieve the vertical and horizontal sizes of each picture:
Set objPlayer = CreateObject(“WMPlayer.OCX” )
Set colMediaCollection = objPlayer.mediaCollection
Set objPhotos = colMediaCollection.getByAttribute(“MediaType”, “photo”)
For i = 0 to objPhotos.Count – 1
Set objPhoto = objPhotos.item(i)
Wscript.Echo “Name: ” & objPhoto.Name
Wscript.Echo “Height: ” & objPhoto.getItemInfo(“WM/VideoHeight”)
Wscript.Echo “Width: ” & objPhoto.getItemInfo(“WM/VideoWidth”)
The script begins by creating an instance of the WMPlayer.OCX object, and then binds to the Windows Media Player media collection. The script then uses the getByAttribute method to return a collection of all the photo items in the media collection (in the world of Windows Media Player 10, photo items are simply .JPG files).
Our photo items come back in the form of an array; to iterate each item in that array we create a For Next loop that runs from 0 (the first item in an array is always numbered 0) to the number of items in the array minus 1 (objPhotos.Count – 1). Inside that For Next loop we bind to each .JPG file using this line of code:
Set objPhoto = objPhotos.item(i)
We then echo the values of the Name, WM/VideoHeight, and WM/VideoWidth attributes. As you probably already guessed, the latter two attributes give us the height and width of each picture.
Incidentally, you can do more with .JPG files than simply retrieve their height and width. For a complete list of photo item attributes check out the Windows Media Player SDK on MSDN.