5

According to the PNG ref page, it is possible to generate PNG with 8 or 16 BitDepth. I cannot find a way to impose 16 for a graphics generated by Plot. Here is the example:

gr = Plot[Cos[x], {x, -4, 4}, Filling -> Bottom];
png=Export["test.png", gr, "BitDepth" -> 16];
Import[png, "BitDepth"]

(* out *) 
8

Thanks

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
bobknight
  • 2,037
  • 1
  • 13
  • 15
  • Related: (7780327), (11421), (14117), (17638), (34141), (52099). Be aware that at last examination Mathematica will not render graphics at greater than 8 bit color depth, therefore generating a 16 bit PNG is only wasting space. Sorry to be the bearer of bad news if I am correct. – Mr.Wizard Dec 03 '14 at 18:03
  • @Mr.Wizard But assuming that the above Plot example, was really only an example, it probably doesn't matter for the use case of the OP whether images are only displayed with 8 bit. In image processing it is often important that you can transport 16bit information in the image even if it is not displayed. One application is a component label image. – halirutan Dec 03 '14 at 18:26
  • @halirutan "I cannot find a way to impose 16 for a graphics generated by Plot." And last I checked Mathematica will not render (Plot) Graphics in 16 bit. I don't think this is an imagine processing question. – Mr.Wizard Dec 03 '14 at 18:38
  • thanks Mr.Wizard you are right, this is not an image processing issues, I was just looking at a way to export PNG with bit depth greater than 8, from a webMathematica application. However, the solution provided by halirutan could be acceptable, once I had a performance test. – bobknight Dec 08 '14 at 09:30

1 Answers1

4

I guess the rasterization into a 16 bit image is not done even though you specified the option. Does it work for you, if you use this:

gr = Plot[Cos[x], {x, -4, 4}, Filling -> Bottom];
png = Export["test.png", Image[gr, "Bit16"]];
Import[png, "BitDepth"]
halirutan
  • 112,764
  • 7
  • 263
  • 474
  • +1 I can confirm this works. Note that the combination of "Bit16" with a graphics argument isn't documented so this isn't quite obvious. By the way the options "Real32" and "Real" lead to 16 bit output as well. – george2079 Dec 04 '14 at 18:27
  • ... playing with some more.. you need to be careful, in some cases you get a nominally 16bit file (reported by BitDepth) but if you look at the ImageData you see only 256 unique values. for some reason you need to specify ImageSize ie Export["test16.png", Image[gr, "Bit16"], ImageSize -> 1000] to get true 16 bit output. – george2079 Dec 04 '14 at 19:59
  • That's fine, thanks. It works, even if I would have preferred a solution built in Export itself. Indeed, what I really need is a quite frequent export of plots from a webMathematica application and I would have PNG with bit depth greater than 8. So, regarding the performance, Export combined with Image is not the optimum. However, what you suggest seems to be the only way to do that. Thanks again for the answer. – bobknight Dec 08 '14 at 09:11
  • Are you sure there are some values in between actual 256 values in that 16 bit PNG? You can check that with cllor picker in GIMP, it supports true 16 bit values, Photoshop does not. – Валерий Заподовников Oct 07 '22 at 21:36