Larry McMuffin
Larry McMuffin

Reputation: 227

How To Swap The Red And Green Channels On An Image File Without PIL

I'm trying to take a screenshot using pygame, but it swaps the green and red colors.(only when I use png) Does anyone know how I can fix this without PIL? I specifically need the file to be .png.

Upvotes: 0

Views: 1535

Answers (2)

marienbad
marienbad

Reputation: 1453

You can use the surfarray class for this, the 3 dimensions are red#green/blue, so something like this should work:

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]

and then you can do:

surf = pygame.surfarray.make_surface(img_copy)

Upvotes: 1

Alexis Clarembeau
Alexis Clarembeau

Reputation: 2964

It seems that there's a bug in pygame to do png export, as reported in this link: http://www.mzan.com/article/19296253-pygame-image-save-colors-distorted.shtml

So, if you absolutely need PNG, you can use PIL in this way (even if it's PIL, you will have a PNG file ;) )

First, export to BPM, then, do:

from PIL import 
Image im = Image.open("screenshot.bmp") 
im.save("screenshot.png") 

You cannot avoid using a third party library as it's a pygame bug :s

Upvotes: 0

Related Questions