0

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.

Larry McMuffin
  • 229
  • 3
  • 12

2 Answers2

1

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)
marienbad
  • 1,461
  • 1
  • 9
  • 19
0

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

Alexis Clarembeau
  • 2,776
  • 14
  • 27
  • 2
    Would there be a way to access the pixel array and swap the green and red before taking the screenshot so it would turn out normal? I only need it on a small 60x30 area. I like to think outside of the box. :3 – Larry McMuffin May 21 '16 at 21:17
  • Yes, of course. To make a screenshot of a part of the screen, just use: `rect = pygame.Rect(25, 25, 100, 50) sub = screen.subsurface(rect)` Then, you can manipulate the surface "sub" :) To manipulate the surface you can have a look at: http://stackoverflow.com/questions/5891808/how-to-invert-colors-of-an-image-in-pygame – Alexis Clarembeau May 21 '16 at 21:20
  • I don't know much about accessing it. I'll do research on it. If I can't figure it out, I'll come back and ask. :P – Larry McMuffin May 21 '16 at 21:21