5
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Dec 3, 2014 at 18:03
  • $\begingroup$ @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. $\endgroup$
    – halirutan
    Dec 3, 2014 at 18:26
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Dec 3, 2014 at 18:38
  • $\begingroup$ 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. $\endgroup$
    – bobknight
    Dec 8, 2014 at 9:30

1 Answer 1

4
$\begingroup$

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"]
$\endgroup$
3
  • $\begingroup$ +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. $\endgroup$
    – george2079
    Dec 4, 2014 at 18:27
  • $\begingroup$ ... 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. $\endgroup$
    – george2079
    Dec 4, 2014 at 19:59
  • $\begingroup$ 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. $\endgroup$
    – bobknight
    Dec 8, 2014 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.