MATLAB Answers


Problem with PNG Image (grayscale): 12 bits from 16 bits

Asked by Alejo
on 27 Jun 2012


I´m working in a project to get images from DDSM ( I´ve got the step to convert (LJPEG to LJPEG.1) and now I´m with the step to convert LJPEG.1 to PNG, but the problem is the PNG image must be of 12 bits (because the original image of DDSM has 12 bits).

I know it´s not possible to obtain directly a 12 bit image from PNG, so I´ve used 2 ways:

  1. Divide the matrix by 16. e.g. I4=I3/16;
  2. Shift the image 4 positions. e.g. I5=bitshift (I3,-4);

With this two options, I get the same result, a matrix with max(I5(:)) = 4095, so the process is OK and I could see it in a figure, but the problem comes when I want to save in a png file, because the image must be saved in 16-bit and get me a dark image with a big size (four times the 8 bit png image). I also checked the value of bits: 13, 14, 15 and 16 and their values are 0.

I´ve used the PNG option of 'SignificantBits' with 12, but I obtained the same result.

Any ideas to solve the problem? I´ve found many articles about it but any of them gives a solution.

Thanks & Regards



Image Analyst
on 27 Jun 2012

I believe Ashish Uthama is the "image format guy" at the Mathworks. Try calling him if he doesn't answer here. I think he might hang out at the newsgroup more than there so try there also. Steve Eddins also has been willing to answer format questions on occasion in the past.

Cheng Zhao
on 9 Nov 2012

This may be of great interest to you! Someone has already done it.



No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 27 Jun 2012

Do not downscale the data: it must be full-range for the bitdepth (16). Use 'SignificantBits' of 12.

Grayscale, 16 bits per channel, 12 significant bits, is the way that PNG represents 12-bit grayscale images. Decoders are allowed to ignore the 12 bit specification

See sBIT Significant bits in


on 28 Jun 2012

I´ve seen this part, but I´m not able to obtain good results with uint16 in the output and I don´t know why. Anyway, I´ve tested with other values: ubit12, ubit16 with bad results.


The other problem ocurrs when I obtain the image with 12 bits and I want to save it as 16-bit png the max value is 4096 and I don´t know how to indicate that this (4095) is the max value and not 65536 (16 bits), for this reason my result is a black image.

I can see (with imshow()) the matrix with max value= 4095 and it´s OK, but I´m not able to save as 12 bit png image.

on 5 Jul 2012


Finally, I´ve got an output image of uint16 and its maximum value is 4095, so that´s right, but now my problem is when I tried to save this image with imwrite.

If I use 'imshow', it works fine, but with imwrite: imwrite(A,'myimage.png', 'BitDepth', 16,'SignificantBits',12);

I obtain a black image with wrong gray levels, I´ve tried to use the command 'imadjust', but it doesn´t work properly, because the result is a "white" image with with wrong gray levels.

Do you know how I can change the gray leves (with max: 4095 (12 bits), not 65536 (16 bits)) with imwrite or another way to get it?


on 12 Jan 2013

I have the same problem here trying to read DDSM database images, can you provide me with your code solution ?

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today