Trouble getting pixel byte representation

Trouble getting pixel byte representation

Postby toozie21 » Mon Jun 16, 2014 2:35 pm

I am fooling around with Pygame for the first time (and I am pretty new at python all around as well), and I was trying to get extract the RGB triplet byte representation for the pixels of a screen.

I was using the pong tutorial from here: http://trevorappleton.blogspot.co.uk/2014/04/writing-pong-using-python-and-pygame.html as my basis. I made sure I had the 1.9.2a pygame version so I could get the get_view class, and then I tried to run
Code: Select all
view = DISPLAYSURF.get_view('3')
.

My understanding was that that would give me three bytes for each pixel (R, G, and B values) and that I could do what I wanted with it. I can get it to run fine, but I cannot seem to access the bytes. I tried to write them to a file just to look at them and make sure they are doing what I wanted, but I can't seem to get that to do what I want.

At the end of the day, I want to pass the comma separated values on to an external module to process them, so if there is a better approach than what I am trying, I am all ears!!
Last edited by micseydel on Mon Jun 16, 2014 4:34 pm, edited 1 time in total.
Reason: First post lock.
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby Mekire » Mon Jun 16, 2014 11:21 pm

I have honestly never seen anyone use pygame.Surface.get_view(). It probably isn't what you want (but who knows). You might want to try PixelArray or surfarray. You can also use pygame.Surface.get_at() to get the color at a single point (though I wouldn't encourage trying to get every pixel in an image this way).


-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Trouble getting pixel byte representation

Postby toozie21 » Tue Jun 17, 2014 11:37 am

Thank Mek, That pygame.surfarray.pixels3d() function looks pretty close to what I need. I gave it a try last night, but I think I am still not writing the values to a file properly. I had said
Code: Select all
view2d = pygame.surfarray.pixels3d(DISPLAYSURF)
and then I wrote
Code: Select all
str(view2d)
to a file. Is that not the way to do it?

Thanks!
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby toozie21 » Wed Jun 18, 2014 11:42 am

OK, I got it to work with pixel3d and that seems to be giving me the data I want. The last issue I have is that it wants to put the data in brackets (for each pixel), so I can print it as [255 255 255], [0 0 0],... etc. But what I would like is 255, 255, 255, 0, 0, 0,... (just a string of values for all the colors for all the pixels). Any know how I can do that easily?
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby DrakeMagi » Wed Jun 18, 2014 1:13 pm

Code: Select all
pystring = str([255, 255, 255])[1:-1]
print pystring
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Trouble getting pixel byte representation

Postby toozie21 » Wed Jun 18, 2014 8:52 pm

That is close, but since the data looks like [255,255,255],[255,255,255] it seems to think it is two arguments and won't take it.

<<edit>>
it looks like the variable is of type numpy.ndarray if that helps. Also, I am running Python 2.7

<<edit2>>
But if I look at the len, it says how many are in that first row, not overall....
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby DrakeMagi » Thu Jun 19, 2014 6:24 pm

maybe this will work.
Code: Select all
from itertools import chain
data = #your data
data = list(chain(*data))
pystring = str(data)[1:-1]
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Trouble getting pixel byte representation

Postby toozie21 » Fri Jun 20, 2014 12:46 am

OK, I have a little more info since the last thing didn't work either (but thank you for trying). Instead of posting from memory when away from my PC, I sat down and tried to be a little more thorough.

If I do a type(view2d) on my variable, it returns numpy.ndarray.

I then did a view2d.shape, and it returns (40,30,3) (I have reduced the original screen size to 40x30 from 400x300 to make debugging easier while I parse through files)

So now I believe that I have a 3D array of type ndarray. If I print(str(view2d)), I see a bunch of things that look like:
Code: Select all
[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...,
 [[255 255 255]
  [255 255 255]
  [255 255 255]

...and so it continues to the end which has three closing brackets


So it looks like lists (with no commas separating them). The inner brackets are the pixel's RGB color, then I guess the middle brackets are the different rows, and the outer bracket encompasses everything.

Is there a way to end up with a list that looks like 255, 255, 255......255,255,255 for all the 3600 values (40*30*3)?
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby DrakeMagi » Fri Jun 20, 2014 12:47 pm

found that numpy has it own flatten instead of using itertools.chain
Code: Select all
import numpy  # if needed
data = yourdata.flatten()
print str(data)[1:-1]
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Trouble getting pixel byte representation

Postby DrakeMagi » Fri Jun 20, 2014 10:44 pm

Here another example. Full numpy.
Code: Select all
import numpy

yourdata = numpy.array([
      [[255,255,255],[255,255,255],[255,255,255]],
      [[255,255,255],[255,255,255],[255,255,255]],
      [[255,255,255],[0,0,0],[255,255,255]],
      [[255,255,255],[255,255,255],[255,255,255]]
      ])
      
data = yourdata.flatten()
numpy.savetxt('numpytest.text', data, fmt="%d, ", newline="")
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Trouble getting pixel byte representation

Postby toozie21 » Mon Jun 23, 2014 6:35 pm

Sorry for the late repsonse (for some reason I am not getting email notifications even though I have it turned on).

I will give it a try tonight. One thing that has me concerned though is that my data doesn't have the commas between the 255s within the brackets. I assume it makes a difference, but I haven't figured out what it means (it is tough to google that subtlty).
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm

Re: Trouble getting pixel byte representation

Postby toozie21 » Tue Jun 24, 2014 9:43 am

As an update, it looks like you are a genius! I get one continuous line of data, all comma separated!! Thanks so much for the help!
toozie21
 
Posts: 7
Joined: Sun Jun 15, 2014 4:13 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests