# Image resizing while preserving average

**URL:** https://discourse.julialang.org/t/image-resizing-while-preserving-average/29704
**Category:** General Usage
**Tags:** images, statistics
**Created:** [October 10, 2019, 1:06am UTC](https://discourse.julialang.org/t/image-resizing-while-preserving-average/29704 "2019-10-10T01:06:52Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 10, 2019, 2:03am UTC](https://discourse.julialang.org/t/image-resizing-while-preserving-average/29704/2 "2019-10-10T02:03:43Z")

</div>

Image resizing is actually surprisingly complicated, both from a performance perspective and an accuracy perspective. I’ve taken a couple of computer vision classes, but I suspect I would get it wrong a few times if I had to implement it myself. You can see the implementation of `imresize` here: [https://github.com/JuliaImages/ImageTransformations.jl/blob/master/src/resizing.jl](https://github.com/JuliaImages/ImageTransformations.jl/blob/master/src/resizing.jl) including some helpful documentation about what it’s trying to do.

In answer to your question, I suspect the easiest way to handle this would be to record the previous average, perform the resize, and then add an offset to the resized image to fix its mean. E.g.

```julia
julia> a = randn(8, 8);

julia> b = imresize(a, (2, 2));

julia> b .+= mean(a) - mean(b)
2×2 Array{Float64,2}:
 0.189495 -0.207325
 0.744529 0.351593

julia> mean(b) ≈ mean(a)
true

```

Does that work for your case?

By the way, you can format your code nicely using the instructions at [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

_[View the full topic](https://discourse.julialang.org/t/image-resizing-while-preserving-average/29704)._
