# Get an image from a matrix of signed integers

**URL:** <https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701>\
**Category:** General Usage\
**Created:** [March 13, 2018, 11:03pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701 "2018-03-13T23:03:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 13, 2018, 11:03pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/1 "2018-03-13T23:03:17Z")

</div>

What’s the best way to get a PNG image out of a matrix of signed integers, with `Images.jl`?

I converted the signed integers to `Fixed` in the range `[-1, 1)` (e.g., `Int64` to `Fixed{Int64, 63}`), but when I try to create a PNG image (for example, when doing this in a Jupyter notebook) I get an error.

Single-line recipe to reproduce the issue:

```julia
julia> using Images

julia> reprmime("image/png", Gray.(reinterpret.(Fixed{Int8, 7}, rand(Int8, 5, 5))))
WARNING: Mapping to the storage type failed; perhaps your data had out-of-range values?
Try `map(clamp01nan, img)` to clamp values to a valid range.
Errors encountered while saving "Nullable{String}()".
All errors:
   InexactError()
   ErrorException("function save does not accept keyword arguments")
Fatal error:
ERROR: InexactError()
Stacktrace:
 [1] kwfunc(::Any) at ./boot.jl:237
 [2] eval(::Module, ::Any) at ./boot.jl:235
 [3] #save#21(::Array{Any,1}, ::Function, ::FileIO.Stream{FileIO.DataFormat{:PNG},IOContext{Base.AbstractIOBuffer{Array{UInt8,1}}}}, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}, ::Vararg{Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2},N} where N) at /home/mose/.julia/v0.6/FileIO/src/loadsave.jl:115
 [4] (::FileIO.#kw##save)(::Array{Any,1}, ::FileIO.#save, ::FileIO.Stream{FileIO.DataFormat{:PNG},IOContext{Base.AbstractIOBuffer{Array{UInt8,1}}}}, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}) at ./<missing>:0
 [5] #show#93(::Int64, ::Int64, ::Function, ::Function, ::IOContext{Base.AbstractIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("image/png")}, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}) at /home/mose/.julia/v0.6/Images/src/showmime.jl:35
 [6] verbose_show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/png")}, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}) at ./multimedia.jl:42
 [7] _binreprmime at ./multimedia.jl:71 [inlined]
 [8] reprmime(::MIME{Symbol("image/png")}, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}) at ./multimedia.jl:61
 [9] reprmime(::String, ::Array{ColorTypes.Gray{FixedPointNumbers.Fixed{Int8,7}},2}) at ./multimedia.jl:98

```

No problem with `Matrix{Gray{Normed{T,N}}}`, for unsigned integers, nor with `Matrix{Gray{AbstractFloat}}` for plain floating numbers (including negative values)

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [March 14, 2018, 12:30pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/2 "2018-03-14T12:30:15Z")

</div>

The problem is, the image values are meaningful for the range of \<0, max\_type\_value\> or \<0,1\> for floating point images. So you have to at least convert to UInt.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 12:46pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/3 "2018-03-14T12:46:26Z")

</div>

> [@phlavenk](#):
>
> The problem is, the image values are meaningful for the range of \<0, max\_type\_value\> or \<0,1\> for floating point images

Why does a `Matrix{Gray{AsbtractFloat}}` with values outside `[0,1]` work? 😕

I thought that `Fixed` type would have taken care of mapping the signed integers to numbers that can be digested by `Images.jl`, just like `Normed` for unsigned integers.

> [@phlavenk](#):
>
> So you have to at least convert to UInt.

How to map signed integers to unsigned integers? Yesterday I came up with a method to do this (just add `typemax(Int) + 1`), but doesn’t work with `Int64`.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 14, 2018, 1:38pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/4 "2018-03-14T13:38:49Z")

</div>

> [@giordano](#):
>
> What’s the best way to get a PNG image out of a matrix of signed integers, with Images.jl?

one way is:

```julia
using Images
img = rand(Int, 5, 5)
imadjustintensity(Float64.(img), extrema(img))

```

But it depends on what your minimum, maximum, and middle is.

---

<div class="post-metadata">

**Author:** ![shorty66](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shorty66/32/3254_2.png) [@shorty66](https://discourse.julialang.org/u/shorty66)\
**Post date:** [March 14, 2018, 1:43pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/5 "2018-03-14T13:43:03Z")

</div>

> [@giordano](#):
>
> How to map signed integers to unsigned integers? Yesterday I came up with a method to do this (just add typemax(Int) + 1), but doesn’t work with Int64.

By adding `typemax(Int)+1` to an `Int64` you would exceed the types `typemax`.  
Use `UInt(n+BigInt(typemax(Int)+1))` instead.

> [@yakir12](#):
>
> imadjustintensity(Float64.(img), extrema(img))

Why not `imadjustintensity(Float64.(img), (typemin(Int), typemax(Int)))` ?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 14, 2018, 1:54pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/6 "2018-03-14T13:54:25Z")

</div>

> [@shorty66](#):
>
> Why not imadjustintensity(Float64.(img), (typemin(Int), typemax(Int))) ?

Not “not”, as I said:

> [@yakir12](#):
>
> it depends on what your minimum, maximum, and middle is

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 2:33pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/7 "2018-03-14T14:33:55Z")

</div>

I’d like to avoid converting to float. Images can be quite large, so converting a `Int8` to a `Float64` is a waste of memory, in addition float numbers can convert precisely only numbers up to `maxintfloat`. This is for scientific purposes, I don’t want to leave any bit behind

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 2:36pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/8 "2018-03-14T14:36:25Z")

</div>

> [@shorty66](#):
>
> By adding typemax(Int)+1 to an Int64 you would exceed the types typemax.
> 
> Use UInt(n+BigInt(typemax(Int)+1)) instead.

I know I could use `BigInt` to overcome the limitation, but, as I said before, Images can be large and involving `BigInt` isn’t ideal.

Really, `Fixed` type looks just what I want, it transparently maps signed integer to another type of the same size compatible with `Color` types, I just don’t understand why I can’t create a PNG image out of a `Matrix{Gray{Fixed}}`.

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [March 14, 2018, 2:47pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/9 "2018-03-14T14:47:48Z")

</div>

```julia
julia> a = Int8[-123, 5]
julia> to_uint8(a::AbstractArray{T,N}) where {T<:Int8, N} = xor.(reinterpret.(UInt8, a, 0x80))

```

Now you can reinterpret to fixedpoint

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 2:51pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/10 "2018-03-14T14:51:27Z")

</div>

> [@phlavenk](#):
>
> ```julia
> to_uint8(a::AbstractArray{T,N}) where {T<:Int8, N} = xor.(reinterpret.(UInt8, a, 0x80))
> 
> ```

Three-argument `reinterpret` doesn’t exist. Probably should have been

```julia
to_uint8(a::AbstractArray{T,N}) where {T<:Int8, N} = xor.(reinterpret.(UInt8, a), 0x80)

```

?

---

<div class="post-metadata">

**Author:** ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)\
**Post date:** [March 14, 2018, 3:47pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/11 "2018-03-14T15:47:05Z")

</div>

Yes. A typo.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 14, 2018, 3:50pm UTC](https://discourse.julialang.org/t/get-an-image-from-a-matrix-of-signed-integers/9701/12 "2018-03-14T15:50:32Z")

</div>

Ok, thanks, your function is the mapping I was looking for. This doesn’t require higher-precision integers for the conversion.

But still, I’d like to know if there is a fundamental limitation in `Matrix{Gray{Fixed}}` or one just needs to write the code needed to convert the matrix to PNG.
