# How to resize a very large array or image without changing its number type

**URL:** <https://discourse.julialang.org/t/how-to-resize-a-very-large-array-or-image-without-changing-its-number-type/43936>\
**Category:** Signal and Image Processing\
**Tags:** question\
**Created:** [July 30, 2020, 12:48am UTC](https://discourse.julialang.org/t/how-to-resize-a-very-large-array-or-image-without-changing-its-number-type/43936 "2020-07-30T00:48:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rio](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@rio](https://discourse.julialang.org/u/rio)\
**Post date:** [July 30, 2020, 12:48am UTC](https://discourse.julialang.org/t/how-to-resize-a-very-large-array-or-image-without-changing-its-number-type/43936/1 "2020-07-30T00:48:39Z")

</div>

Hi,

I would like to resize an array (or perhaps an image) and get a result of the same type. For instance, in my code below:

```julia
N = 10
img = rand(Int8,N,N,3)
img = imresize(img,ratio=(0.7, 0.7, 1))

```

the outupt img changes from Array{Int8,3} to Array{Float64,3}. If my N was small, my naïve attempt

`img = Int8.(round.(imresize(img,ratio=(0.7, 0.7, 1))))`

would solve the problem, but I am struggling with memory because my N is ~40000.

Please, how this could be done in a better way?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [July 30, 2020, 8:51am UTC](https://discourse.julialang.org/t/how-to-resize-a-very-large-array-or-image-without-changing-its-number-type/43936/2 "2020-07-30T08:51:14Z")

</div>

imresize does interpolation ([https://github.com/JuliaImages/ImageTransformations.jl/blob/a83895e0b1386aee833c8337d2b8029842c61b1b/src/resizing.jl#L316](https://github.com/JuliaImages/ImageTransformations.jl/blob/a83895e0b1386aee833c8337d2b8029842c61b1b/src/resizing.jl#L316))  
so, it naturally outputs floating point numbers.

Maybe the best option is to use normed fixed-point numbers (instead of floating-point numbers).  
For example the type `N0f8 ` is a 8-bit number which represents numbers between 0 and 1. [1]

The equivalent code is

```julia
using ImageTransformations, Colors, ImageCore
N = 10
img = rand( RGB{N0f8}, N, N)
img = imresize(img, ratio=(0.7,0.7))

```

or, if you want to keep the channels as a third dimension:

```julia
img = rand( N0f8, N, N, 3)
img = imresize(img, ratio=(0.7,0.7,1.0))

```

However, due to the memory arrangement of multidimensional arrays, it is better to have the channel in the first dimension and it is best to use `RGB{N0f8}`. You can use `channelview(img)` [2] if you want to access only particular channels.

Links: [1] [GitHub - JuliaMath/FixedPointNumbers.jl: fixed point types for julia](https://github.com/JuliaMath/FixedPointNumbers.jl)  
[2] [Views · ImageCore](https://juliaimages.org/ImageCore.jl/latest/views.html#View-types-defined-in-ImageCore-1)
