Converting a PyCall numpy ×3 Array{UInt8, 3} to RGB

Hey guys, I use the NumPy per PyCall to get screenshots of my desktop.

I get these images back as 1440×3440×3 Array{UInt8, 3}

Discord user borodi#9175 already shared a function of him that converts this array to a BGR.

opencvtojulia(abc) = BGR.(reinterpret.(N0f8,abc[:,:,3]),reinterpret.(N0f8,abc[:,:,2]),reinterpret.(N0f8,abc[:,:,1]))

Because it changes a lot of colors to the wrong ones i have to fix something in it.

Has somebody an idea how to get RGB images from this function or what else can get me RGB?

Thanks for your help!

1 Like

The precise steps may depend on your data (e.g., RGB vs BGR; Julia supports both), but this demo may get you started:

julia> using PyCall, ImageCore, ImageView

julia> si = pyimport("skimage.data")
PyObject <module 'skimage.data' from '/usr/lib/python3/dist-packages/skimage/data/__init__.py'>

julia> dat = pycall(si.rocket, PyArray);

julia> img = reinterpretc(RGB{N0f8}, PermutedDimsArray(dat, (3, 1, 2)));

julia> imshow(img)

ImageCore has an extensive set of composable no-copy operations for reinterpreting the meaning of raw data; with them, you should be able to do anything you want. Please see the docs at ImageCore.jl · ImageCore

5 Likes

Wow! Thanks what a great solution!

reinterpretc(RGB{N0f8}, PermutedDimsArray(dat, (3, 1, 2))); solves it alone!

Many thanks!

1 Like

Thank you so much!