convert a pygame surface into an image?

試著忘記壹切 提交于 2020-08-25 02:33:07

问题


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 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")


来源:https://stackoverflow.com/questions/62528716/convert-a-pygame-surface-into-an-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!