这是一种使用像素阵列的方法。像素阵列很慢,但是如果速度不是问题,它们可以满足您的需要,而不必下载任何外部库。此外,像素阵列也很容易理解。在import pygame
# You would load your image as a sprite here.
# But let's create a demonstration sprite instead.
#
usecolor = (46,12,187,255) # Declare an example color.
sprite = pygame.Surface((10,10)) # Greate a surface. Let us call it a 'sprite'.
sprite.fill(usecolor) # Fill the 'sprite' with our chosen color.
#
# Now process the image.
array = pygame.PixelArray(sprite) # Create a pixel array of the sprite, locking the sprite.
sample = array[5,5] # Sample the integer holding the color values of pixel [5,5]
# We will feed this integer to pygame.Color()
sample_1 = sprite.get_at((5,5)) # Alternately, we can use the .get_at() method.
# Do the same for every pixel, creating a list (an array) of color values.
del array # Then delete the pixel array, unlocking the sprite.
m,r,g,b = pygame.Color(sample) # Note: m is for the alpha value (not used by .Color())
print("\n sample =",sample,"decoded by python.Color() to:")
print(" r >>",r)
print(" g >>",g)
print(" b >>",b)
print("\n or we could use .get_at()")
print(" sample_1 =",sample_1)
print()
exit()
只需测试每个r,g,b值,看看它们是否落在每个颜色分量所需的范围内。然后将每个像素复制到一个新的曲面上,用所需的替换颜色替换范围内的所有颜色。在
或者,在将像素放入新图像之前,可以对每个R、G、B颜色分量(如果color>;255:color=255)添加75。这会使所有的颜色逐渐变白,直到浅色消失。然后你可以重复这个过程,从每个剩余的像素减去75(分量值小于255),使颜色再次上移。我怀疑任何一个像样的验证码都是那么容易被击败的,但你去吧。在
好玩好玩!在