My question is very similar to this, but I have thousands of images on disk and I want to fast read their width and height in pixels without loading each file in memory.
On my Linux machine, I could do something like this for each file:
path_to_file <- 'img/13600061.jpg'
system(sprintf("file %s", path_to_file), intern = TRUE)
But the output of file
can differ for jpg, jpeg and png files and then I need to catch the pixel info differently depending on the file. I was wondering if there is a general fast solution out there already.
CodePudding user response:
I think exiftool
fits the bill nicely here. It runs on all platforms, is very controllable and crucially, it can recurse on its own so it doesn't incur the overhead of being started once per file.
As a rough first attempt, you'd want something like this if processing PNGs and JPEGs and recursing down starting at current directory, i.e. .
exiftool -csv -ImageHeight -ImageWidth -r -ext jpg -ext jpeg -ext png .
Sample Output
black.png,80,80
blue.png,80,80
c.jpg,1,1
deskew/skew40.png,800,800
deskew/gradient.png,800,800
You may want to add -q
to exclude the summary if you are parsing the output.
As a rough guide, the above command runs in 9 seconds on a directory containing 10,000 images on my Mac.