Python Forum
[PyGame] BufferProxy.raw problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] BufferProxy.raw problem (/thread-10680.html)



BufferProxy.raw problem - Twitchy - May-31-2018

Hello,

I've been trying for a few days now to send the pixel data of a surface using get_view() through a pipe to a c progam.
But i can't get the raw values of the rgb pixels using BufferProxy.raw...

I can see the "start" and "end" but not the "buf.raw".

I'm using python 2.7.

i tried using "array = pygame.surfarray.pixels3d(screen)", i can see it when using "print array" but the data can't go through the pipe because it is not contiguous.

Do you have any idea where i went wrong ?

Thanks !

import pygame
import os, sys, time

os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.init()
pygame.display.set_mode((5,5), 0, 24)
screen = pygame.display.get_surface()

screen.fill(pygame.Color(b'black'))
pygame.draw.line(screen, (200, 0, 0), (0, 0),(4,0), 1)
pygame.display.update()

buf = screen.get_view('2')

# this line doesn't show anything
print(buf.raw)

pipe_name = "/tmp/pgmatrix"

if os.path.exists(pipe_name):
    os.remove(pipe_name)
if not os.path.exists(pipe_name):
  os.mkfifo(pipe_name)

pipe = os.open(pipe_name, os.O_WRONLY)

os.write(pipe, "start")
os.write(pipe, buf.raw)
os.write(pipe, "end")



RE: BufferProxy.raw problem - billtubbs - Nov-11-2018

(May-31-2018, 12:32 PM)Twitchy Wrote:
I can see the "start" and "end" but not the "buf.raw".

Sorry, I don't know why your code doesn't work. But the following works for me so maybe you can figure it out by trying to run this:

>>> import pygame
>>> import pygame.gfxdraw
>>> import numpy as np
>>> background_colour = (1, 1, 1)
>>> width, height = (256, 256)
>>> screen = pygame.Surface((width, height))
>>> pygame.draw.rect(screen, (8, 16, 32), (0, 0, 100, 100), 0)
<rect(0, 0, 100, 100)>
>>> s = screen.get_buffer()
>>> x = np.fromstring(s.raw, dtype='b').reshape(height, width, 4)
>>> x[0, 0]
array([32, 16,  8,  0], dtype=int8)
The problem I have is that it has switched the R, G, B values around. (I expected to get [8, 16, 32, 0] back).

Any idea why or how to avoid that would be appreciated.

(Nov-11-2018, 10:26 PM)billtubbs Wrote: The problem I have is that it has switched the R, G, B values around. (I expected to get [8, 16, 32, 0] back).

See this stackoverflow post for how I solved my problem.