# Expecting an image or graphics instead of -Image-

Here's a snippet of my notebook. When run with the frontend, the code runs fine with no problem at all. However, when I save the notebook as .m file and run it using "Mathkernel -noprompt -script notebook.m," the ImageSubtract command throws the error " Expecting an image or graphics instead of -Image-." Any help is appreciated.

Print["Processing images..."];
atomImageFile =
imagesDirectory <> imageTime <> atomImageSuffix <> imageFormat;
noAtomImageFile =
imagesDirectory <> imageTime <> noAtomImageSuffix <> imageFormat;
backgroundImageFile =
imagesDirectory <> backgroundImageFileName <> imageFormat;
backgroundImage = Image[Import[backgroundImageFile, "PNG"]];
atomImage = Image[Import[atomImageFile, "PNG"]];
noAtomImage = Image[Import[noAtomImageFile, "PNG"]];
ImageSubtract[atomImage, backgroundImage];

Print["Done."];

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
Nov 28 '14 at 17:52
• Not able to try it now, but please try assigning xx = ImageSubtract[...];.Thanks Nov 28 '14 at 18:07
• Still giving me the same error @belisarius Nov 30 '14 at 0:55
• What are FullForm[atomImage] and FullForm[noAtomImage]? Oct 1 '15 at 15:19

The solution to this problem is to convert all images to ImageData for calculation. When the calculation is finished, Export the result image to a file. Here's the code that works.

atomImageFile =
imagesDirectory <> imageTime <> atomImageSuffix <> imageFormat;
noAtomImageFile =
imagesDirectory <> imageTime <> noAtomImageSuffix <> imageFormat;
backgroundImageFile =
imagesDirectory <> backgroundImageFileName <> imageFormat;
backgroundImage =
ImageData[
ColorConvert[Image[Import[backgroundImageFile, "PNG"]],
"Grayscale"]];
atomImage =
ImageData[
ColorConvert[Image[Import[atomImageFile, "PNG"]], "Grayscale"]];
subtractImage = atomImage - backgroundImage;
subtractImage = Image[subtractImage];
Export[atomImageFile, subtractImage, "TIFF"];