【问题标题】:Get color of individual pixels of images in pygame在pygame中获取图像单个像素的颜色
【发布时间】:2016-09-16 19:11:53
【问题描述】:

我怎样才能得到一个图像像素的颜色值,它被 blitted 到一个 pygame 表面上?使用 Surface.get_at() 只返回表面层的颜色,而不是已经被 blitted 覆盖的图像。

【问题讨论】:

  • 请提供您的代码示例

标签: python image image-processing pygame


【解决方案1】:

surface.get_at 方法很好。 这是一个示例,显示了在没有 Alpha 通道的情况下对图像进行位图处理时的差异。

import sys, pygame
pygame.init()
size = width, height = 320, 240
screen = pygame.display.set_mode(size)
image = pygame.image.load("./img.bmp")
image_rect = image.get_rect()

screen.fill((0,0,0))
screen.blit(image, image_rect)
screensurf = pygame.display.get_surface()

while 1:

  for event in pygame.event.get():
     if event.type == pygame.MOUSEBUTTONDOWN :
        mouse = pygame.mouse.get_pos()
        pxarray = pygame.PixelArray(screensurf)
        pixel = pygame.Color(pxarray[mouse[0],mouse[1]])
        print pixel
        print screensurf.get_at(mouse)

  pygame.display.flip()

这里,点击红色像素会给出:

(0, 254, 0, 0)
(254, 0, 0, 255)

PixelArray 返回一个 0xAARRGGBB 颜色分量,而 Color 预期为 0xRRGGBBAA。还要注意屏幕表面的 Alpha 通道是 255。

【讨论】:

  • +1 好答案!只有两件事:您提到surface.getAt 和使用surface.get_at。这可能会令人困惑。其次,您在 pygame.display.flip 函数中使用了未定义的变量 e。我不确定它应该是什么,因为要更改的字符很少,我无法自己编辑它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2013-05-28
  • 2019-07-27
  • 2014-12-06
相关资源
最近更新 更多