Java with Imagej does not convert right from tiff 16bit to jpg

Tags: #<Tag:0x00007fb87b9132b0> #<Tag:0x00007fb87b9130d0> #<Tag:0x00007fb87b912f18> #<Tag:0x00007fb87b912db0>


The program I created,inserting the path of the image.tiff creates in the same folder an new image.jpg. But when I run it, the conversion I get is this:
I obviously expect the second image (the generated one), to be the same as the original one.

Fragment of my code, which as you see is essential:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Start it");
        ImagePlus imp=IJ.openImage("C:\\test\\myImg.tiff"); 
        FileSaver fs= new FileSaver(imp);

I have not received this issue with other general tiff.

My tiff is: [dimension 1246*1650] [width 1246] [height 1650] [h resolution 1272 dpi][v resolution 1272 dpi] [bit depth 16] [uncompressed] [resolution unit 2]


Hi @FrancescoLiuzzi,

I was able to reproduce something like the behavior you’re seeing with a different 16-bit image - the reason this happened for me was that the FileSaver writes the output as an 8-bit image, so some loss of precision happens (unless your 16bit image only contains values in [0,255]). So in that sense, the output jpeg must be different than the original image. You might consider a format besides jpeg if it’s essential that you don’t lose any precision.

However, it is possible to do a worse or better job of faithfully representing the original values in the jpg.
ImageJ will re-bin the 16-bit values to 8-bit values differently, depending on how the brightness and contrast is set.

On the left is a 16 bit image. I resaved it twice with 2 different setting of brightness/contrast, the results of which are in the middle and on the right. (They’re both scaled to [0,255], so the differences you see result in different pixel values). The point is, one has to decide what intensity ranges are important when reducing precision.

Hope that helps,


Thank you for the advice, I should implement everything in java, do you have any snippet of useful code to translate your advice?
I do not have the need to maintain a rigorous copy, but something that can be used as thumbnail…


Is it very important that you save the result as a jpg?

If not, then I’d recommend you save as a tif and not have to deal with the precision issues above.

If you do absolutely need a jpg, something like:, "Enhance Contrast", "saturated=0.35");

may help if you’re looking for something automatic - i.e. you don’t a priori know your images’ dynamic range and don’t want to manually determine the contrast.



I call the method…) and after fileSaver.saveAsJpeg(…) this is the result:

The need to change the format is because I need to display the image in my java program (javaFx does not support tiff)

Also when I try to open the tiff file with imageJ I get the same graphic problem.
I upload my sample here


Okay, I get the impression that you want that bright stuff near the top to appear darker - is that correct?

The snippet I gave you above automatically tries to set the brightness and contrast of the image so that the dark stuff is black and the bright stuff is white - that’s why the bright stuff got even brighter. If you want the bright stuff to look darker, you’ll need the “top end” of this range to be at a big value. Please follow up with a question if that’s not clear.

Your image has intensities between about 80 and 185:

I got that window with Image>Adjust>Brightness/Contrast
With brightness/contrast set like this, pixels with intensity below 80 will look black, and pixels above 184 will be white.

Try this:

int bigNumber = 800;
IJ.setMinAndMax(imp, 0, bigNumber );

and you should get something like the image below:

and play with bigNumber until you get something that you like. The bigger you make it, the darker your image will be.

Hope that helps,


But is it normal that the image is as divided into two?

Is it like being split into a read and unread part of the original tiff…


That “split” is in the original image - ImageJ is not putting it there.
(Just to make sure, I even opened it in a different program).

Where did the image come from? Is it possible the imaging system “messed up” somehow?


yeah i totaly think the same!! i have now a lot of information for work with the img, i have olso to verify the start quality of the img.
p.s. I was using an old program that genereted this imgs