hbblue
hbblue

Reputation: 305

convert a pygame surface into an image?

is there a way to convert a pygame surface into a png image?

rgb_content = pygame.surfarray.array2d(canvas)
cv2.imwrite(file, rgb_content, [cv2.IMWRITE_PNG_COMPRESSION, 0])

I've tried this but it doesn't work, because it flips the image sideways. Makes it grayscale and doesn't actually save it.

Upvotes: 3

Views: 1972

Answers (1)

sloth
sloth

Reputation: 101072

Use the save function of the image modul:

pygame.image.save()
save an image to disk
save(Surface, filename) -> None

This will save your Surface as either a BMP, TGA, PNG, or JPEG image. If the filename extension is unrecognized it will default to TGA. Both TGA, and BMP file formats create uncompressed files.

So just use it like this:

pygame.image.save(your_surface, "your_filename.png")

Upvotes: 6

Related Questions