【发布时间】:2016-05-21 21:10:55
【问题描述】:
我正在尝试使用 pygame 截屏,但它会交换绿色和红色。(仅当我使用 png 时)有谁知道我如何在没有 PIL 的情况下解决这个问题? 我特别需要文件是.png。
【问题讨论】:
我正在尝试使用 pygame 截屏,但它会交换绿色和红色。(仅当我使用 png 时)有谁知道我如何在没有 PIL 的情况下解决这个问题? 我特别需要文件是.png。
【问题讨论】:
你可以使用 surfarray 类,这 3 个维度是红色#绿色/蓝色,所以这样的东西应该可以工作:
img = pygame.surfarray.array3d(*your image here*)
img_copy = pygame.surfarray.array3d(*your image name here*)
for y in range(0, *image height*):
for x in range(0, *img width*):
img_copy[x][y][0] = img[x][y][1]
img_copy[x][y][1] = img[x][y][0]
然后你可以这样做:
surf = pygame.surfarray.make_surface(img_copy)
【讨论】:
好像pygame做png导出有bug, 如此链接中所述: http://www.mzan.com/article/19296253-pygame-image-save-colors-distorted.shtml
所以,如果你绝对需要 PNG,你可以这样使用 PIL(即使是 PIL,你也会有一个 PNG 文件;))
首先,导出到 BPM,然后:
from PIL import
Image im = Image.open("screenshot.bmp")
im.save("screenshot.png")
你不能避免使用第三方库,因为它是一个 pygame 错误:s
【讨论】:
rect = pygame.Rect(25, 25, 100, 50) sub = screen.subsurface(rect) 然后,您可以操纵表面“sub” :) 要操纵表面,您可以看看:stackoverflow.com/questions/5891808/…