python - 将 pygame 表面转换为图像?

标签 python pygame

有没有办法将 pygame 表面转换为 png 图像?

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

我已经试过了,但它不起作用,因为它会横向翻转图像。将其设为灰度并且实际上并不保存它。

最佳答案

使用 save image 模块的功能:

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.

所以只需像这样使用它:

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

关于python - 将 pygame 表面转换为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62528716/

相关文章:

python - 有没有办法将html嵌入到pygame中

macos - 在 OS X 上使用 Pygame 进行错误的颜色抗锯齿

python - Pygame 的基本网络

python - Pygame:细胞物理学

python - 在python脚本中检测相似文档的算法

python - 打印由任何字符而不是逗号分隔的列表项 (Python)

python - "too many resources for launch"的解释

python - 如何用Python通过串口读取MCU发送的字节

python - numpy 数组的 glDrawPixels 未显示

python - 使用 BeautifulSoup 导入特定列的数据