1

I have calculated a matrix of RGB triples for an image and I would like to know the most straightforward technique to display them in an interactive window. I suspect that pygame will be involved. Techniques that minimize the use of pip will be given preference.

result = numpy.zeros([height,width, 3], dtype=numpy.uint8)
pyopencl.enqueue_copy(queue, result, result_g).wait()

surface = pygame.display.set_mode((width, height), pygame.DOUBLEBUF)
# now what?
Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
  • check this question out : http://stackoverflow.com/questions/30818367/how-to-present-numpy-array-into-pygame-surface/30970504#30970504 – Mikhail V Apr 23 '16 at 00:20

2 Answers2

1

The solution I was able to get working was this:

result = numpy.zeros([height,width, 3], dtype=numpy.uint8)
pyopencl.enqueue_copy(queue, result, result_g).wait()

surface = pygame.display.set_mode((width, height), pygame.DOUBLEBUF)
rgb2 = numpy.transpose(rgb, (1,0,2))          
pygame.pixelcopy.array_to_surface(surface, rgb2)
pygame.display.flip()

The transpose is only necessary because my opencl kernel computed an image arranged as result[y,x,:] = (r,g,b) whereas array_to_surface expects result[x,y,:] (which is backwards from how most framebuffers work). I could alter my opencl kernel to store things in column-major order if I wanted to avoid the transpose.

This solution only works because my surface is the exact same dimensions as my image. I will upvote any other good solutions that work when the surface and the pixel matrix are different dimensions (because someone might find this article when searching for that).

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
0

It is difficult to answer exactly without knowing what the code you have shown does, but something like this:

for c in range(width):
   for d in range(height):
      color = result(d,c)
      pygame.draw.line(surface, color, (c,d),(c+1,d))
marienbad
  • 1,461
  • 1
  • 9
  • 19