Hi everyone,
I am 18 hours into exploring Cython and trying to use it to speed up some Image processing I am doing. (Please note, I do not want to introduce a dependency on numpy, the whole reason I am interested in Cython is that it avoids needing to worry about distributing numpy alongside the tool I'm building).
Basically, is there a nice way to get at the underlying buffer representation of an image from PIL in Cython that avoids copying memory and ideally gives me [i, j] indexing to get a pixel value
I have two images - one input and one output. I had some code that looked something like this:
cdef c_array.array in_array = array('i', in_image.getdata(0))
cdef int* in_val = in_array.data.as_ints
cdef c_array.array out_array = array('i', [default] * len(in_array))
cdef int* out = out_array.data.as_ints
pixel = in_val[x + (y * width)]
However it would seem to me that this is copying the data and also just looks like the wrong way to do things. I feel that something like the following should work:
cdef int [:, :] in_memoryview = in_image
cdef int [:, :] out_memoryview = out_image
as everything talking about the "buffer interface" says that "PIL uses it extensively" but without any associated code examples to show how you get to the buffer. Also nothing on whether it is possible to get a writeable memoryview on an image or whether I need to construct a writeable buffer and then map a PIL image on to that.
As I said, I'm just starting out with Cython, so maybe I've got my terminology wrong and am asking the wrong question. But does anyone know what specific piece of voodoo I am asking for and how to get it?
Thanks!