Cropped and resized images are not saved

I’m trying to resize and crop an image using ImageJ. Here’s the code:

ImagePlus ip1 = IJ.openImage("_Pic.jpg");
ImagePlus ip2 = IJ.openImage("_Pic.jpg");

ImageProcessor imgP1 = ip1.getProcessor();
ImageProcessor imgP2 = ip2.getProcessor();

FileSaver fs1 = new FileSaver(ip1);
FileSaver fs2 = new FileSaver(ip2);

/* Trying to resize */
imgP1.resize(100); // also tried with width and height
fs1.saveAsJpeg("Resized.jpg");

/* Trying to crop */
imgP2.setRoi(100, 100, 200, 200);
imgP2.crop();
fs2.saveAsJpeg("Cropped.jpg");

Unfortunately, the newly created files are identical as the original one.

So far I’ve found out how to blur, smooth, invert, translate, rotate… but these two are giving me hard time. Anybody has an idea?

Thanks.

Hi @vtomic85,

welcome to the forum! :slight_smile:

If you take a look at the Javadocs for ImageProcessor you’ll see that resize() as well as crop() return new ImageProcessor instances and do not operate on this. That’s why you’ll have to use the ImagePlus.setProcessor(ImageProcessor) method to add the returned ImageProcessors to ip1 and ip2.

Best,
Stefan

3 Likes

Thanks a lot, @stelfrich, this helped :slight_smile:

@vtomic85 when cross-posting here and on stackoverflow.com, please include links so that others will be able to find the answers :slight_smile:

1 Like

I’m need to resize images, but I don’t quite understand how ImagePlus and ImageProcessor gets used. Can you please post a code example for your answer?