简体   繁体   English

在pygame中获取和设置像素颜色的快速方法

[英]fast way to get and set pixel color in pygame

I am working on a simple project using pygame and I need to get and set pixel colors. 我正在使用pygame进行一个简单的项目,我需要获取并设置像素颜色。 Using pygame.get_at(()) and pygame.set_at(()) is VERY slow. 使用pygame.get_at(())pygame.set_at(())非常慢。

So I tried using PixelArray to get and set the colors manually. 所以我尝试使用PixelArray手动获取和设置颜色。 But this still is VERY slow 但这仍然很慢

Is there a much faster way to do this? 有更快的方法吗?

Is it possible to make a funtion to get and set a pixel in c++ so it would be faster or is there a way in python/pygame to do it? 是否有可能在c + +中获得和设置像素以使它更快,或者在python / pygame中有办法做到这一点?

For example: 例如:

a c++ function that returns the pixel color at a given coordinate and another function that sets the pixel at a given coordinate to a given color. 一个c ++函数,该函数返回给定坐标处的像素颜色,另一个函数将给定坐标处的像素设置为给定颜色。

Then I could call these functions in pygame somehow. 然后我可以以某种方式在pygame中调用这些函数。

Thank you very much. 非常感谢你。

C++ SetPixel and GetPixel function C ++ SetPixel和GetPixel函数

Part of window.h library window.h库的一部分

Set Pixel at coords with color 将像素设置为与颜色一致

COLORREF SetPixel(
  _In_  HDC hdc,
  _In_  int X,
  _In_  int Y,
  _In_  COLORREF crColor
);

Return color of pixel at specified coords 返回指定坐标处像素的颜色

COLORREF GetPixel(
  _In_  HDC hdc,
  _In_  int nXPos,
  _In_  int nYPos
);

SDL: SDL:

Get Color 获得颜色

Uint32 rawpixel = getpixel(surface, x, y);
Uint8 red, green, blue;

SDL_GetRGB(rawpixel, surface->format, &red, &green, &blue);

SetPixel at coords SetPixel在Coords

void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
    Uint32 *pixmem32;
    Uint32 colour;  

    colour = SDL_MapRGB( screen->format, r, g, b );

    pixmem32 = (Uint32*) screen->pixels  + y + x;
    *pixmem32 = colour;
}

Link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM