JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.45k stars 186 forks source link

Convert memory address to Python pointer #1051

Closed ejmeitz closed 10 months ago

ejmeitz commented 10 months ago

I am using the PySide6 library and end up with a QImage. I would like to get the raw data of this image into a Matrix in Julia and the best approach to do that seems to involve obtaining the pointer to the image in memory and then doing an unsafe_wrap in Julia. However, I have no clue how to get the pointer from Python to a Julia Ptr{T} type. Currently, in Julia I have this object: PyObject <memory at 0x0000016112C022C0> and want a Ptr{Float64}(...)

This is similar to #868 but this should not be a void pointer and it seems that ctypes does not support c_float_p which would be the type of my raw image data. There might be some way to do this with ctypes still but is there any way to do this without an external library? Any help would be appreciated, thanks!

stevengj commented 10 months ago

How would you do it in Python? If you can figure out how to get a numpy array, then that will give you a Julia array easily.

stevengj commented 10 months ago

Closing this issue as it's more of a Python-library usage question than a PyCall issue per se. You might ask about it on Julia Discourse if you can't figure it out.

ejmeitz commented 10 months ago

Hm didn't think to look at the Python options, been a bit stuck in Julia-land. In case anyone else runs into this you can use the numpy function below to take a pointer in python and get a numpy array.

raw_data = np.ctypeslib.as_array(ptr, shape=(height * width * n_color_channels))