src = Image.open("test.jpg")
src.load()
src = src.convert('RGBA')
src_buf = cl.Image(self.ctx,
flags=mf.READ_ONLY | mf.USE_HOST_PTR,
format=fmt,
shape=src.size,
hostbuf=src
)
TypeError: expected an object with a writable buffer interface
On 31 March 2014 12:46, Andreas Kloeckner <[email protected]> wrote:
> "CRV§ADER//KY" <[email protected]> writes:
>
> > Hi,
> > how do I write a kernel that reads from a PIL.Image and writes to
> > another, without doing using any unnecessary malloc and memcpy
> operations?
> >
> > So far I could only implement it like this:
> >
> > SHAPE = (16,16)
> > def some_filter(src):
> > fmt = cl.ImageFormat(cl.channel_order.RGBA,
> > cl.channel_type.UNORM_INT8)
> > src_np = numpy.fromstring(src.tostring(),
> > dtype=numpy.uint32).reshape(src.size)
> > dst_np = numpy.empty(src.size, dtype=numpy.uint32)
> >
> > src_buf = cl.Image(self.ctx, flags=mf.READ_ONLY, format=fmt,
> > shape=src.size)
> > dst_buf = cl.Image(self.ctx, flags=mf.WRITE_ONLY, format=fmt,
> > shape=src.size)
> > cl.enqueue_copy(self.queue, src_buf, src_np, origin=(0,0),
> > region=src.size)
> > self.prg.somefilter(queue, src.size, SHAPE, src_buf, dst_buf)
> > cl.enqueue_copy(self.queue, dst_np, dst_buf, origin=(0,0),
> > region=src.size)
> >
> > dst = Image.frombytes('RGBA', src.size, dst_np)
> > return dst
> >
> > Kernel code:
> >
> > __constant sampler_t SAMPLER = CLK_NORMALIZED_COORDS_FALSE |
> > CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
> > kernel void somefilter(
> > __read_only image2d_t srcImage,
> > __write_only image2d_t dstImage,
> > ) {
> > const int2 pos = {get_global_id(0), get_global_id(1)};
> > float4 pixel = read_imagef(srcImage, SAMPLER, pos);
> > //omissis: actual filter operation
> > write_imagef(dstImage, pos, pixel);
> > }
> >
> > The above is terrible, because:
> > 1) performs a first copy when I invoke PIL.Image.tostring()
> > 2) performs a second copy when I invoke numpy.fromstring()
> > 3) performs a third copy with Image.frombytes()
> >
> > How do I read/write directly from/to a PIL.Image to/from the GPU memory?
>
> What error do you get if you pass the (PIL) Image as the hostbuf, with
> USE_HOST_PTR?
>
> Andreas
>
_______________________________________________
PyOpenCL mailing list
[email protected]
http://lists.tiker.net/listinfo/pyopencl