Error reading PNG image data



Hello everyone,
I created an ImageJ macro for enhancing the histrogram of all “raw” images in a directory and saving the enhanced images into a second directory (e.g. “raw_enhanced”). For this, each file from the file list will be individually opened, enhanced, saved and closed. In contrast to loading all images as image sequence at once to a stack, this should prevent any memory shortage. (Resource monitor verfies this assumption.)
However after successfully enhancing 1450 of 6776 images (each 2800 x 2000 @ 8bit), the following error was thrown:

This is the macro:

raw_path = getDirectory("Choose a Directory ");
raw_directory = File.getName(raw_path);
enhanced_directory = raw_directory + "_enhanced";
parent_directory = File.getParent(raw_path);
enhanced_path = parent_directory + File.separator + enhanced_directory;


setBatchMode(true);	// Enables stack mode to speed up function.
tick = getTime();
items = enhanceFiles(raw_path, enhanced_path, "PNG", 0.6)
tock = getTime();
elapsed_time = (tock-tick)/1000; // Unit: s
print("Enhanced ", items, "images in ", elapsed_time, "s. ", items/elapsed_time, " images per second.");


function enhanceFiles(raw_path, enhanced_path, extension, saturation_percentage)
	file_list = getFileList(raw_path);
	i_max = file_list.length;
	for (i=0; i<i_max; i++)
		timeStamp1 = getTime();
		present_file_name = file_list[i];
		open(raw_path + File.separator + present_file_name);
		run("Enhance Contrast...", "saturated=&saturation_percentage normalize");
		saveAs(extension, enhanced_path + File.separator + present_file_name);
		timeStamp2 = getTime();
		elapsedTime = (timeStamp2 - timeStamp1)/ 1000; //s
		print("\\Update:\n" + "Enhanced image " + i+1 + " of " + i_max + " in " + elapsedTime + " s. (" + 1/elapsedTime + " images/s)");
	return i;

What could be the reason for this? A bug of ImageIO or a bug of my macro?

The system load was always uncritical.
Pre error:

Post error:

I used Fiji with ImageJ 1.52f .

Best regards,


Good day Max,

I didi’t look at your code in detail, but “return;” at the end of your main block isn’t a valid call. Use “exit();” instead.

Are you sure the saved images have a correct file suffix?

Furthermore, you may run the macro on a smaller number of images without “setBatchMode(true);” and check if the images are closed on a regular basis.

Have success



Hello Herbie,

Thank you your quick reply and your suggestions!
There are only image files with the extension “.png” in the source directory. I also tried it with a smaller test directory, where the first image was the 1451th image of the original directory. This worked properly. With this, I made sure that the reason for the bug has nothing to do with the naming of the files.
Finally, I ran the macro without using the batch mode. All images were closed immediately after each iteration. But the error appeared again at the 1451th image .



Well Max,

after having studied your macro code again, I can’t see anything weird and I can’t run the macro on so many images.

I guess the problem may be with Fiji or BioFormats.

Sorry to be of no real help.