Use file for everyday
summaries. Use ExifTool when you need to know
everything.
The problem
I am once again puttering around with my site.
I have an image here.
 grubby scary basement](/_astro/basement-original.KaTU22Og_2hRD6J.webp)
Do I need to resize it? Should find out how big it is first.
$ exa basement-original.jpgPermissions Size User Date Modified Git Name.rw-rw-r-- 131k random 20 Apr 9:04 -- basement-original.jpgNo I don’t mean file size. I mean geometry. How many pixels wide, and how many high? exa is nifty though. You should try it out.
I don’t want to leave my shell session to do it either. Sure that’s just stubbornness on my part. I could get the necessary information from my desktop’s file browser, but I type quicker than I click.
file is everywhere
file is a standard utility, which means it’s available on pretty much
any Unix or Unix-like system you use. It works by matching a file’s
internal details to entries in a
magic database and reporting its
findings. magic can be extended, though the details are beyond me for
now.
$ file basement-original.jpgbasement-original.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, progressive, precision 8, 800x487, frames 3basement-original.jpg is a jpeg image. Yep, that checks out. Let’s
see — 800x487. That looks like a geometry value. 800 pixels wide and 487
pixels high, if I remember the order right.
I want more detail, but this is all I can get from file. That’s what
ExifTool is for.
ExifTool knows everything
ExifTool lets me read and edit metadata for images, music, PDF, Word files, videos — a dizzying assortment of files are supported.
You can install ExifTool with the downloads listed on its site or using your favorite package manager.
$ brew install exiftoolDefault usage returns every bit of information ExifTool thinks is relevant for the file type.
$ exiftool basement-original.jpgExifTool Version Number : 11.85File Name : basement-original.jpgDirectory : .File Size : 129 kBFile Modification Date/Time : 2020:04:20 09:04:28-07:00File Access Date/Time : 2020:04:21 10:08:57-07:00File Inode Change Date/Time : 2020:04:20 09:04:28-07:00File Permissions : rw-rw-r--File Type : JPEGFile Type Extension : jpgMIME Type : image/jpegJFIF Version : 1.01Resolution Unit : inchesX Resolution : 72Y Resolution : 72Image Width : 800Image Height : 487Encoding Process : Progressive DCT, Huffman codingBits Per Sample : 8Color Components : 3Y Cb Cr Sub Sampling : YCbCr4:4:4 (1 1)Image Size : 800x487Megapixels : 0.390800 pixels wide, 487 pixels high. I remembered correctly! While I’m here, let’s look at ways to fine-tune the output.
We could cut down on the noise by specifying the fields or tags we want to see.
$ exiftool -ImageWidth -ImageHeight basement-original.jpgImage Width : 800Image Height : 487We could use -S for more compact output focused less on tabular
layout.
$ exiftool -S -ImageWidth -ImageHeight basement-original.jpgImageWidth: 800ImageHeight: 487We could tell ExifTool to format its report for processing by CSV or JSON tools.
$ exiftool -j basement-original.jpg[{ "SourceFile": "basement-original.jpg", "ExifToolVersion": 11.85, "FileName": "basement-original.jpg", "Directory": ".", "FileSize": "129 kB", "FileModifyDate": "2020:04:20 09:04:28-07:00", "FileAccessDate": "2020:04:21 10:08:57-07:00", "FileInodeChangeDate": "2020:04:20 09:04:28-07:00", "FilePermissions": "rw-rw-r--", "FileType": "JPEG", "FileTypeExtension": "jpg", "MIMEType": "image/jpeg", "JFIFVersion": 1.01, "ResolutionUnit": "inches", "XResolution": 72, "YResolution": 72, "ImageWidth": 800, "ImageHeight": 487, "EncodingProcess": "Progressive DCT, Huffman coding", "BitsPerSample": 8, "ColorComponents": 3, "YCbCrSubSampling": "YCbCr4:4:4 (1 1)", "ImageSize": "800x487", "Megapixels": 0.390}]Hang on. I feel compelled to be a bit fancy.
Piping to xsv for aggregate information about site images.
$ exiftool -csv -r content \ | xsv search -s MIMEType 'image/.+' \ | xsv select ImageWidth,ImageHeight \ | xsv stats \ | xsv select field,min,max,mean \ | xsv table 682 directories scanned 419 image files readfield min max meanImageWidth 27 5120 1337.1172248803825ImageHeight 27 4032 1009.7368421052624Hm. I must have some icon files in there somewhere.
For more fun, point it at some music files. Heck, it will try to give useful information for text!
$ exiftool index.adocExifTool Version Number : 11.85File Name : index.adocDirectory : .File Size : 4.8 kBFile Modification Date/Time : 2020:04:21 12:58:13-07:00File Access Date/Time : 2020:04:21 12:58:14-07:00File Inode Change Date/Time : 2020:04:21 12:58:13-07:00File Permissions : rw-rw-r--File Type : TXTFile Type Extension : txtMIME Type : text/plainMIME Encoding : us-asciiNewlines : Unix LFLine Count : 189Word Count : 619Here’s the file output in comparison.
$ file index.adocindex.adoc: ASCII textI could probably write some magic configuration to get more
information. But installing ExifTool was easier.
Good enough?
file is universally available and gave me the details I needed today.
ExifTool gives me everything I needed and then some. I’ll most likely
keep it available on my systems.
And no, I’m not going to worry about resizing that image for now.
Got a comment? A question? More of a comment than a question?
Talk to me about this page on: Hacker's Town