如何反转图像的颜色在pygame? [英] How to invert colors of an image in pygame?

查看:1737
本文介绍了如何反转图像的颜色在pygame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pygame Surface,并且想反转颜色。有什么办法更快,比这更pythonic吗?这是很慢。



我知道从255减去值不是唯一的定义倒色,但它是我想要的。



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



感谢您的帮助!

  import pygame 

def invertImg(img):
颜色的一个pygame屏幕

img.lock()

对于范围内的x(img.get_width()):
for y in range img.get_height()):
RGBA = img.get_at((x,y))
对于范围(3)中的i:
#反转RGB而不是Alpha
RGBA [i] = 255 - RGBA [i]
img.set_at((x,y),RGBA)

img.unlock()


解决方案

Winston的回答很好,但是为了完整起见,在Python中逐像素,应该避免循环遍历每个像素,无论使用哪个图像库。



幸运的是,优秀的 NumPy 库可以帮助在字节流中执行几个标量操作,循环本地代码中的每个数字,这比单纯在Python中执行的数量级快几个数量级。对于这个特定的操作,如果我们使用(2 ^ 32 - 1)操作 xor ,我们可以委托



这个例子,你可以直接粘贴到你的Python控制台,将像素立即翻转为白色(如果你有NumPy安装):

  import pygame 

srf = pygame.display.set_mode((640,480))
pixels = pygame.surfarray.pixels2d(srf)
pixels ^ = 2 ** 32 - 1
del pixels

pygame.display.flip()

没有安装NumPy, pygame.surfarray 方法返回普通Python数组(从 stdlib 数组模块),你必须找到另一种方法来操作这些数字,因为普通的Python数组不会对所有元素进行操作, pixels ^ = 2 ** 32 - 1


I have a pygame Surface and would like to invert the colors. Is there any way quicker & more pythonic than this? It's rather slow.

I'm aware that subtracting the value from 255 isn't the only definition of an "inverted color," but it's what I want for now.

I'm surprised that pygame doesn't have something like this built in!

Thanks for your help!

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()

解决方案

Winston's answer is nice, but for the sake of completeness, when one has to manipulate an image pixel-by-pixel in Python, one should avoid looping through every pixel, no matter which image library is in use. This is CPU-intensive due to the nature of the language, and can rarely be made to work in realtime.

Fortunately, the excellent NumPy library can help perform several scalar operations in streams of bytes, looping over each number in native code, which is orders of magnitude faster than doing it solely in Python. For this particular operation, if we use an xor operation with (2^32 - 1), we can delegate the operation to the inner loop in native code.

This example, which you can paste directly into your Python console, will flip the pixels instantly to white (if you have NumPy installed):

import pygame

srf = pygame.display.set_mode((640,480))
pixels = pygame.surfarray.pixels2d(srf)
pixels ^= 2 ** 32 - 1
del pixels

pygame.display.flip()

Without NumPy installed, pygame.surfarray methods return ordinary Python arrays (from the stdlib array module) and you would have to find another way to operate on these numbers, since the ordinary Python array does not operate on all elements when a line such as pixels ^= 2 ** 32 - 1 is given.

这篇关于如何反转图像的颜色在pygame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆