# Array/Image zoom

**URL:** https://discourse.julialang.org/t/array-image-zoom/52085
**Category:** General Usage
**Tags:** images
**Created:** [December 19, 2020, 12:13pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085 "2020-12-19T12:13:06Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [December 19, 2020, 12:13pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/1 "2020-12-19T12:13:06Z")

</div>

Does anyone know if there is a package providing a function like `scipy.ndimage.zoom` [https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html](https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html)?

---

<div class="post-metadata">

### Author: ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)
#### Post date: [August 3, 2024, 10:31am UTC](https://discourse.julialang.org/t/array-image-zoom/52085/2 "2024-08-03T10:31:20Z")

</div>

Sorry for necro-ing this older post, but since this is hasn’t been answered yet, I’m share my experience.

The closest thing I have found is `imresize()` from the `Images` package. However, I have been unable to achieve the exact same results. We are using

```
ndimage.zoom(data, scale, order=1)

```

in Python, and I thought

```
imresize(data, ratio=scale, method=BSpline(Linear()))

```

but AFAICS the results are not exactly the same.

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [August 3, 2024, 2:05pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/3 "2024-08-03T14:05:37Z")

</div>

`imresize` works very well.  
To get the same result, you must call `scipy.ndimage.zoom` for each image channel and stack the corresponding outputs to get the zoomed image.  
See this [SO answer](https://stackoverflow.com/questions/51516644/why-scipy-ndimage-zoom-add-new-channel).

---

<div class="post-metadata">

### Author: ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)
#### Post date: [August 3, 2024, 3:44pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/4 "2024-08-03T15:44:58Z")

</div>

Thanks for the reply, but I’m afraid that’s not the issue I’m encountering. The image is single-channel.

FWIW `imresize` _does_ work well. I’m just mentioning that the interpolation algorithm doesn’t seem to be the same as `ndimage.zoom`, so the resulting scaled array will not be the same (I’ve done some random sampling and I’m seeing differences even in the second significant decimal digit). I’m not saying the results are _wrong_, but it would be nice if there was an algorithm equivalent ot he `scipy` one (I’m not even sure what that is).

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [August 3, 2024, 3:53pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/5 "2024-08-03T15:53:11Z")

</div>

> [@Oblomov](#):
>
> (I’m not even sure what that is)

That’s often the thing with using algorithms from libraries. If you want to have control of exactly what happens or want to get the same results in different implementation/environments, you’re better off implementing them yourself. If you just want to have _some_ valid result, using a library is of course fine, most of the time.

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [August 3, 2024, 3:58pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/6 "2024-08-03T15:58:37Z")

</div>

The default `order` for `ndimage.zoom` is 3, i.e. Julia `BSpline(Cubic())`, while for `imresize° is BSpline(Linear())`. If you set the same interpolation method for both you get the same image.

---

<div class="post-metadata">

### Author: ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)
#### Post date: [August 3, 2024, 9:01pm UTC](https://discourse.julialang.org/t/array-image-zoom/52085/7 "2024-08-03T21:01:41Z")

</div>

As I mentioned in my first reply, we are using `order=1` for `zoom`, and `BSpline(Linear())` for `imresize`, and the results are different.

---

<div class="post-metadata">

### Author: ![Oblomov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oblomov/32/211073_2.png) [@Oblomov](https://discourse.julialang.org/u/Oblomov)
#### Post date: [August 4, 2024, 8:14am UTC](https://discourse.julialang.org/t/array-image-zoom/52085/8 "2024-08-04T08:14:23Z")

</div>

I’ve done some additional testing, and I think I’ve found the hangup.

So, my first test was to use `gdalwarp` (via `ArchGDAL`) instead of `imresize`: the bilinear interpolation from GDAL gives essentially the same results as `imresize`.

At this point, I read the `scipy.ndimage.zoom` documentation, and found out that the default for it is `grid_mode=False`, which basically puts the matrix value at the pixel center. Using `grid_mode=True` (and `mode='nearest'`) in `zoom` seems to give the same result as `imresize()`.
