Continue in loop when open(image) fails

I run a loop where inages are opened and processed one by one. Some DICOM images are damaged and File opener reports that either width and height is 0 or file length doesn’t match with image size. This makes ImageJ stop in the loop.

Can I avoid this hinder in the looping and just move on to the next image in line?

Hi Mikael,

your question is hard to answer, if you don’t give us any details on the implementation (Macro|Script|Plugin, minimal code example, …).

In Jython I use the following code for my batch opener:

def batch_open_images(some parameters):
    # some code that is not relevant
    images = []
    for img_path in path_to_images:
            imp = IJ.openImage(img_path)
            if imp:
        except Exception, err:
    return images

The if imp is most important, as ImageJ does not throw an exception on all images it can’t open, but returns none instead of an ImagePlus object.

Supplement: I had a look at the relevant source code of ImageJ and found no Exception that is thrown. Just checking if the return value of IJ.openImage() is None seams to be sufficient.


Thanks for the answer. I run a macro in ImageJ.

for (i = 0; i < ImageList.length; i++) open(ImageList[i])

I now tried to first open each image by looking at the first bytes in the file using openAsRawString(ImageList[i],100). The problematic images can be identified with this, and then I pass only the OK images to the open function.