小编Ain*_*ain的帖子

如何在pygame中反转图像的颜色?

我有一个pygame Surface,想要反转颜色.有没有比这更快更pythonic的方法?这很慢.

我知道从255中减去这个值不是"倒置颜色"的唯一定义,但它是我现在想要的.

我很惊讶pygame没有这样内置的东西!

谢谢你的帮助!

import pygame

def invertImg(img):
    """Inverts the colors of a pygame Screen"""

    img.lock()

    for x in range(img.get_width()):
        for y in range(img.get_height()):
            RGBA = img.get_at((x,y))
            for i in range(3):
                # Invert RGB, but not Alpha
                RGBA[i] = 255 - RGBA[i]
            img.set_at((x,y),RGBA)

    img.unlock()
Run Code Online (Sandbox Code Playgroud)

python graphics pygame colors inversion

8
推荐指数
2
解决办法
5652
查看次数

标签 统计

colors ×1

graphics ×1

inversion ×1

pygame ×1

python ×1