# "Crunch" RGB pixels into 0-1 range

**URL:** <https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428>\
**Category:** Visualization\
**Created:** [March 24, 2022, 9:30pm UTC](https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428 "2022-03-24T21:30:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![theta](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@theta](https://discourse.julialang.org/u/theta)\
**Post date:** [March 24, 2022, 9:30pm UTC](https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428/1 "2022-03-24T21:30:53Z")

</div>

Hi,

I’m messing around with image generation in Julia. I just realized that some of the pixels I’m generating have red, green, or blue values outside of the range [0,1]. For example, one of my pixels looks like `RGB{Float64}(2.222500068146143,0.5746785271458048,2.1358479996207556)`. This image actually shows up great with imshow(), but when I go to save it as a .PNG file, the file turns out corrupted. Is there a quick way to “crunch” all the n0f8 values in my image into a 0-1 range? I could write a nested for loop, but it’s kinda ugly and I’d like something more efficient.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 24, 2022, 9:34pm UTC](https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428/2 "2022-03-24T21:34:29Z")

</div>

Broadcast `clamp` o `some_function`?

---

<div class="post-metadata">

**Author:** ![drvi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvi/32/13236_2.png) [@drvi](https://discourse.julialang.org/u/drvi)\
**Post date:** [March 24, 2022, 10:12pm UTC](https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428/3 "2022-03-24T22:12:04Z")

</div>

It seems that `ImageInTerminal.jl` clamps the values to 0 and 1, which you could do with:

```julia
clamp!(channelview(x), 0, 1)

```

There are other potentially useful approaches to this. If you know the scale of the floating point values used in your images, you could just subtract the minimum value from each color and then divide by the maximum range:

```julia
crunch!(x, lo, hi) = (c = channelview(x); c .-= lo; c ./= (hi - lo))

```

or even

```julia
crunch!(x) = (c = channelview(x); (lo, hi) = extrema(c); c .-= lo; c ./= (hi - lo))

```

depending on your use-case.

---

<div class="post-metadata">

**Author:** ![theta](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@theta](https://discourse.julialang.org/u/theta)\
**Post date:** [March 24, 2022, 11:17pm UTC](https://discourse.julialang.org/t/crunch-rgb-pixels-into-0-1-range/78428/4 "2022-03-24T23:17:36Z")

</div>

Hey, I ended up figuring it out by reading the error message from when I tried to save the image. What I wanted (saving the image so it looks like the output from imshow()) is achievable with `save("image.png",map(clamp01nan, img))`. Hope this helps someone else!
