[英]Pixel RGB values inverted when using set_at in pygame
In a pygame application I'm writing, I directly manipulate pixels using the Surface.set_at() and get_at() methods. 在我写的pygame应用程序中,我使用Surface.set_at()和get_at()方法直接操作像素。 It's not time-sensitive, so no problem there. 它不是时间敏感的,所以没问题。 But I'm seeing some weird behavior. 但我看到一些奇怪的行为。 After being asked to put together an mcve, I identified the exact set of circumstances where the problem occurs. 在被要求组建一个mcve后,我确定了问题发生的确切情况。 My code: 我的代码:
import pygame
def is_color(surface,position,color):
col=surface.get_at(position)
return (col.r,col.g,col.b)==color
def flood_fill(surface, position, fill_color):
frontier = [position]
fill=pygame.Color(fill_color[0],fill_color[1],fill_color[2],255)
n=0
while len(frontier) > 0 and n<50000:
x, y = frontier.pop()
try:
col=surface.get_at((x,y))
if is_color(surface,(x,y),fill_color):
continue
except IndexError:
continue
surface.set_at((x,y),fill)
n+=1
frontier.append((x + 1, y))
frontier.append((x - 1, y))
frontier.append((x, y + 1))
frontier.append((x, y - 1))
ROOD = (150,0,0)
pygame.init()
screen=pygame.display.set_mode((200,200))
colors=pygame.Surface((200,200))
pygame.draw.circle(colors,ROOD,(50,50),20,2)
pygame.draw.circle(colors,ROOD,(150,150),20,2)
flood_fill(colors,(50,50),ROOD)
pygame.image.save(colors,"circles.png")
del colors
colors=pygame.image.load("circles.png")
flood_fill(colors,(150,150),ROOD)
screen.blit(colors,(0,0))
pygame.display.flip()
When I run as is (Windows 10), the first circle is filled, the second fill operation fails. 当我按原样运行时(Windows 10),第一个圆圈被填充,第二个填充操作失败。 The problem appears to be in the reading from PNG file: when I change the filename to circles.bmp, no problem. 问题似乎出现在PNG文件的读取中:当我将文件名更改为circles.bmp时,没问题。 So I now have a work-around. 所以我现在有一个解决方法。 Is this a bug in PNG file handling, or am I missing a subtlety of how these things are supposed to work? 这是PNG文件处理中的错误,还是我错过了这些东西应该如何工作的微妙之处?
我不确定发生了什么,但你可以通过转换加载.png文件后获得的表面来修复问题(通常应该转换(或convert_alpha)新加载的图像)。
colors = pygame.image.load("circles.png").convert()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.