开发者

Pygame quick pixel reading

开发者 https://www.devze.com 2023-04-12 08:30 出处:网络
Alright, hopefully someone can help me with this because I\'m at a loss here. I\'m working on a game in python that uses the camera as the main controller. To play the game, you move a simple light a

Alright, hopefully someone can help me with this because I'm at a loss here.

I'm working on a game in python that uses the camera as the main controller. To play the game, you move a simple light around, and that controls the game.

So far, I've managed to get the camera image to a surface, and display it on the screen. But when it comes to finding the light, the program is slowed to a crawl.

My first attempt used the surface.get_at(x,y) function (this is not the actual code, this is an example):

maxL = 0
point = (0,0)
mysurface = get_cameraImg()
for i in range(mysurface.get_width()):
    for j in range(mysurface.get_height()):
        color = mysurface.get_at(i,j)
        lum 开发者_开发问答= get_lum(color.r,color.g,color.b)
        if(lum>maxL):
            maxL = lum
            point = (i,j)

This was horrendously slow, so I started doing my research. A quick check to the pygame docs showed that getting pixels 1 at a time using this method would be slow. So then I started trying using a surfarray:

maxL = 0
point = (0,0)
mysurface = get_cameraImg()
ar = pygame.surfarray.array3d(mysurface)
for i in range(len(ar)):
    for j in range(len(ar[i]):
        color = ar[i][j]
        lum = get_lum(color[0],color[1],color[2])
        if(lum>maxL):
            maxL = lum
            point = (i,j)

However, this was also too slow. A quick double check to the surfarray docs showed that pygame.surfarray.pixels3d(surface) would be faster (as no actual value is copied), but unfortunately my camera image is not 24 or 32 bit format. So, here's my question:

In pygame, is there any quick way to read ALL pixels?

I have to be able to read every pixel, do a lum calculation on it, (maxcolor+mincolor)/2, then see if it's lum is the max lum. Thank you for your time.


It seems that the surfarray object is backed by numpy arrays, so your chances of getting it faster are relying on their native methods.

Take a look at this: numpy.argmax

You might need to transform your 3d array to a 2d luminosity matrix first (but don't do it with nested loops! XD). Maybe you'll need some reshaping to get the planes, and then average them with simple matrix operations... Or instead of working in RGB, you could switch to other image format that is easier to work with like HSV or just grayscale.


Don't know if there's a way specific for Pygame, but there is a simple way to speed a lot of things up in many python programs: use Psycho, see here

http://psyco.sourceforge.net/introduction.html

Another thing that comes into my mind is: why do you have to "find the light" by reading pixel-by-pixel? Isn't the position of the light something you already know beforehand, so you could just check the camera position relative to the light position?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号