问题
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.
回答1:
Use the save function of the image modul:
pygame.image.save()
save an image to disksave(Surface, filename) -> NoneThis 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")
来源:https://stackoverflow.com/questions/62528716/convert-a-pygame-surface-into-an-image