Mathematica Asked by M.R. on January 6, 2021
I have a directory of images that might not have downloaded, and need to delete the duds.
This is too slow: Import /* ImageQ /@ fileNames
In bash, I use the file
command to inspect the metadata of an image file’s header, but ImageQ
only works on in-memory images not paths or File
objects.
Is there some way to do this efficiently, without importing them? There’s a function for this I forgot.
Update:
FileFormat
has a problem – it checks all known filetypes! e.g. you can’t just check if the file is one of a list {"JPEG","PNG","TIFF"}
.
Maybe this helps you already:
Lets take the filenames as a provided list as you suggest.
fileNames = {} (*your list here*)
First, lets time the code you provided:
AbsoluteTiming[Import /* ImageQ /@ fileNames]
{0.204095, {True, True, True, True}}
Speed up by a factor of 2 gives this code:
AbsoluteTiming[MemberQ[Import[#, "Elements"], "Image"] & /@ fileNames]
{0.0822613, {True, True, True, True}}
Speed up by a factor of 20 seems to give this:
Extensions = {‰PNG, ‰JPG} (*or other extensions*)
AbsoluteTiming[MemberQ[Extensions, First@ReadList[#, Expression, 1]] & /@ fileNames]
{0.00788099, {True, True, True, True}}
Note that the last code also works if you delete the .png extension from the file name. So "image.png" and "image" give both True
.
Answered by Mr Puh on January 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP