Test if a file is an image

I was wondering if there is a function to check if a filepath corresponds to an image file that can be opened by ImageJ/Fiji.

The idea is to use such function when walking through a directory of file. Which mostly contains images but possibly some extra non-image files.

Checking the file extension is not really a solution unless there is an updated list of image file extensions that can be opened by ImageJ/Fiji or Bioformat.

Otherwise I will fall back on a try open/except pass.

There’s the Linux file command. I am not a Java programmer but StackOverflow has suggestions for equivalent functions.

1 Like

I guess you could use io.scif.services.FormatService from #scifio as illustrated here:

#@ File input
#@ FormatService fs

def format
try {
	format = fs.getFormat(input.getAbsolutePath())
}
catch (e) {
	// ignore FormatException
}
message = "$input "
message += format == null ? "cannot be opened." : "is a known format."
println message
1 Like

Thank you both for the answers !

Indeed one possibility is to use Files.probeContentType, which returns a string like image\tiff, or text\plain.
Here is the jython code

#@ File InFile
from java.nio.file import Files

FileType = Files.probeContentType(InFile.toPath())
print FileType

And this work too, it throws an error if it is not a format openable by SCIFIO.
I tested a number of them, it will indeed return an error with most non-image format, except .java or .xml files ! But the probability is rather low to have java source code in an image folder so I will opt for this option.

I had trouble running the java code above (UnsupportedOperationException) but here is a Jython version that works fine.

#@ File InFile
#@ FormatService fs

try:
	format = fs.getFormat(InFile.toPath())

except:
	print "not compatible"

EDIT: With the latest round of updates, the getFormat method does not accept sring path anymore.
It should be wrapped into a FileLocation object as such

#@ File InFile
#@ FormatService fs
from org.scijava.io.location import FileLocation

format = fs.getFormat(FileLocation(InFile))

The code I posted above was #groovy code, sorry for omitting this information. The script runs as is from the script editor.

1 Like

This is because .java is a valid image format for SCIFIO. For example, run this Groovy script from the script editor (e.g. with the blobs image open):

#@ File (label="Enter .java file path to save", style="save", value="testJavaFormat.java") outputFile
#@ Dataset input
#@ DatasetIOService datasetIO

datasetIO.save(input, outputFile.getPath())

The output file will look similar to this:

//
// testJavaFormat.java
//

// Generated by SCIFIO v0.37.3
// Generated on Tue Mar 26 15:39:47 CET 2019

public class testJavaFormat {

  public byte[][] image0Plane0 = {
    {40, 32, 24, 24, 16, 24, 24, 32, 32, 32, 24, 24, 24, 24, 24, 32, 40, 80, 112, -104, -72, -48, -24, -24, -32, -40, -48, -48, -48, -48, -56, -56, -56, -72, -96, -128, 88, 72, 48, 40, 24, 24, 24, 32, 32, 40, 40, 48, 48, 48, 48, 72, 96, -120, -80, -72, -64, -72, -88, -80, -72, -64, -56, -56, -64, -72, -80, -72, -72, -64, -56, -64, -80, -112, 112, 88, 56, 40, 24, 24, 24, 32, 32, 32, 32, 40, 40, 48, 48, 48, 48, 48, 40, 40, 40, 56, 72, 120, -96, -64, -40, -24, -16, -24, -32, -32, -32, -24, -24, -16, -16, -16, -16, -16, -24, -24, -32, -40, -48, -72, -104, 104, 56, 48, 32, 40, 48, 64, 72, 64, 56, 56, 56, 56, 48, 48, 48, 48, 40, 40, 32, 32, 32, 56, 72, 120, -96, -64, -40, -24, -8, -16, -24, -24, -24, -24, -24, -24, -32, -40, -56, -64, -72, -88, -112, 112, 80, 56, 32, 40, 40, 48, 56, 56, 56, 56, 48, 48, 48, 40, 32, 32, 32, 32, 32, 40, 48, 56, 56, 64, 64, 64, 64, 56, 48, 48, 40, 40, 40, 48, 48, 56, 56, 56, 56, 64, 64, 56, 48, 48, 48, 48, 48, 56, 56, 64, 64, 64, 56, 56, 48, 48, 48, 48, 40, 40, 40, 40, 40, 32, 24, 24, 24, 24, 16, 16, 8, 16, 24, 48, 64, 104, -112, -80, -56, -40, -24, -16, -16, -16, -16, -16, -24, -40, -56, -56},
    {56, 40, 24, 24, 24, 32, 32, 32, 32, 32, 24, 24, 24, 32, 32, 48, 56, 96, -128, -96, -64, -40, -24, -24, -24, -32, -48, -48, -48, -56, -56, -64, -72, -96, -112, 112, 80, 64, 48, 40, 24, 24, 24, 32, 32, 40, 40, 48, 48, 48, 48, 72, 88, -128, -88, -72, -64, -72, -80, -80, -72, -72, -64, -72, -72, -72, -80, -64, -56, -48, -48, -56, -72, -112, 112, 88, 56, 48, 32, 32, 24, 32, 40, 40, 40, 48, 48, 48, 48, 48, 48, 40, 40, 40, 48, 72, 88, -120, -80, -56, -32, -24, -16, -24, -32, -32, -32, -32, -24, -24, -24, -24, -24, -24, -32, -32, -32, -40, -48, -72, -96, 120, 72, 56, 40, 40, 48, 56, 64, 64, 56, 56, 48, 48, 48, 48, 48, 48, 40, 40, 32, 40, 40, 72, 96, -120, -80, -56, -32, -16, -8, -8, -16, -16, -16, -16, -16, -24, -24, -40, -48, -64, -72, -88, -104, 120, 80, 56, 32, 32, 40, 48, 56, 56, 56, 48, 48, 48, 40, 40, 32, 32, 32, 40, 40, 48, 56, 56, 56, 64, 64, 56, 56, 48, 48, 40, 40, 40, 40, 48, 48, 56, 56, 64, 64, 64, 64, 56, 48, 48, 48, 48, 48, 48, 48, 56, 64, 64, 56, 56, 48, 48, 48, 40, 40, 40, 40, 40, 40, 40, 32, 32, 24, 24, 16, 16, 16, 32, 40, 64, 88, -128, -88, -64, -40, -24, -16, -8, -8, -8, -8, -16, -16, -24, -40, -40},
    {64, ...
1 Like

I just found a 3rd solution in the ImageJ1 API relying on the Opener class.
See the example in the NextImageOpener here

I guess it supports less formats than the Scifio service though.