The white artifacts you can see in the picture are caused because the JPG format is a compressed format.
The compression is not lossless. This means the colors around the weapon are not exactly white (255, 255, 255). The color appear to be white for the human eye, but actually the color channels have a value lass than 255, but near to 255.
You can try to correct this manually. Ensure that format of the image has an alpha channel by pygame.Surface.convert_alpha()
. Identify all the pixel, which have a red green and blue color channel above a certain threshold (e.g. 230). Change the color channels and the alpha channel of those pixels to (0, 0, 0, 0):
img = pygame.image.load(IMAGE).convert_alpha()
threshold = 230
for x in range(img.get_width()):
for y in range(img.get_height()):
color = img.get_at((x, y))
if color.r > threshold and color.g > threshold and color.b > threshold:
img.set_at((x, y), (0, 0, 0, 0))
Of course you are in danger to change pixels which you don't want to change to. If the weapon would have some very "bright" areas, then this areas my become transparent, too.
Note, an issue like this can be avoided by using a different image format like BMP or PNG.
With this formats the pixel can be stored lossless.
You can try to "photo shop" the image. Manually change the pixel around the weapon and store the image with a different format.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…