# Saving greater than 8 bit images

**URL:** <https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057>\
**Category:** New to Julia\
**Tags:** images\
**Created:** [September 25, 2017, 7:32am UTC](https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057 "2017-09-25T07:32:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dryden4482](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dryden4482](https://discourse.julialang.org/u/dryden4482)\
**Post date:** [September 25, 2017, 7:32am UTC](https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057/1 "2017-09-25T07:32:02Z")

</div>

I’m trying to save out Array{ColorTypes.Gray{Float16},2} as a tiff image. I’m just getting black images in the output file. The package being used is imagemagick. Thank you for your time.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [September 25, 2017, 8:24am UTC](https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057/2 "2017-09-25T08:24:58Z")

</div>

Please provide more context; you are using which save function?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 26, 2017, 9:55am UTC](https://discourse.julialang.org/t/saving-greater-than-8-bit-images/6057/3 "2017-09-26T09:55:48Z")

</div>

Not exactly sure that I know what you’re experiencing, but I suspect this is an interesting combination of limitations of the ImageMagick C library and a bug in FixedPointNumbers [fixed here](https://github.com/JuliaMath/FixedPointNumbers.jl/pull/95).

ImageMagick requires that floating-point values be between 0 and 1, so if your values aren’t in that range they will be clamped. Moreover, as far as I can tell (and please correct me if I’m wrong), ImageMagick quantizes your inputs and does not provide any mechanism for knowing the original pixel type, so that when you load the image again you can’t guarantee that the original pixel type is recreated. For these reasons, ImageMagick.jl defaults to `N0f8` precision for anything that’s not already “integer” (`Normed`) datatypes. If you want to control this yourself, consider `save(filename, n0f16.(img))` or `save(filename, img; mapi=x->N0f16(clamp01nan(x)))` so that you’re in charge of the precision yourself.

Now, it turns out there was also a bug in FixedPointNumbers. The problem arises because the computations here overflow:

```julia
julia> Float16(1.0)*0xffff # the 0xffff comes from quantizing to 16-bit integers
Inf16

julia> realmax(Float16)
Float16(6.55e4)

julia> Float16(0xffff)
Inf16

```

The fix was just to convert to Float32 before converting to `N0f16`.

If you don’t like the idea of the ImageMagick C library quantizing your inputs and want to exactly preserve the values you supply, a good format choice might be NRRD. Julia’s NRRD package will load them as plain `Float16`, but you can always do `b = colorview(Gray, load("test.nrrd"))` to get the `Gray` back. (And it may be fixable to load them as `Gray`, feel free to [open an issue](https://github.com/JuliaIO/NRRD.jl).)
