# Convert JPG image to pixel values

**URL:** https://discourse.julialang.org/t/convert-jpg-image-to-pixel-values/71302
**Category:** General Usage
**Tags:** images
**Created:** [November 11, 2021, 2:58am UTC](https://discourse.julialang.org/t/convert-jpg-image-to-pixel-values/71302 "2021-11-11T02:58:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)
#### Post date: [November 11, 2021, 2:58am UTC](https://discourse.julialang.org/t/convert-jpg-image-to-pixel-values/71302/1 "2021-11-11T02:58:59Z")

</div>

Hi, I’m using `Images` package to convert JPG image to an array with pixel values. I first loaded the image and then used `channelview` but I’m getting some weird values in the array. Can someone tell me how to convert this to pixel values?

```julia
img = load("IMG_2372.jpg")
img = channelview(img)

```

Printing `img[1,1,1]` returns `0.624N0f8`.

Thanks

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [November 11, 2021, 3:19am UTC](https://discourse.julialang.org/t/convert-jpg-image-to-pixel-values/71302/2 "2021-11-11T03:19:45Z")

</div>

Don’t jpegs load as an array of RGB pixels? Or do you want black and white?

Those N0f8 values are normalized fixed point Uints. Basically they are an UInt8 but instead of going from 0 to 255 they go from 0.0 to 1.0.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [November 11, 2021, 5:55am UTC](https://discourse.julialang.org/t/convert-jpg-image-to-pixel-values/71302/3 "2021-11-11T05:55:59Z")

</div>

If you want to look at the underlying integer-valued counts, you can use something like this:

```julia
imagecounts(img) = (rawview ∘ channelview)(img)

```

That’ll give a `3 x h x w` array of `UInt8`s that you can convert to something like `Int64` for further inspection. I’ve often found myself writing something like this - maybe it should live somewhere in Images.jl? It’s only a sensible transformation for raw image sensor data, but that’s the only type of data I work with, and I imagine other experimentalists are similarly situated.

Why should you have to worry about this? Well, pixels in Images.jl are stored in a packed format with all the color channels adjacent to each other in memory. Taking an individual pixel, we can peer into the underlying storage:

```julia
julia> img = rand(RGB{N0f8}, 16, 16);

julia> px = img[1, 1]
RGB{N0f8}(0.043,0.125,0.678)

julia> dump(px)
RGB{N0f8}
  r: N0f8
    i: UInt8 0x0b
  g: N0f8
    i: UInt8 0x20
  b: N0f8
    i: UInt8 0xad

julia> Base.summarysize(px) # number of bytes needed for storage
3

```

What’s the advantage of using `RGB{N0f8}` instead of `UInt8` by default? It lets us harness Julia’s method dispatch to make operations on pixels behave as you’d expect them to:

```julia
julia> using Statistics

julia> mean(img)
RGB{Float64}(0.5031403186274511,0.5014246323529411,0.5300551470588235)

julia> mean(imagecounts(img))
130.44270833333334

```

Averaging an image _should_ retain the color channel information, which the former does and the latter doesn’t. Great!
