简体   繁体   English

将 pygame 表面转换为图像?

[英]convert a pygame surface into an image?

is there a way to convert a pygame surface into a png image?有没有办法将 pygame 表面转换为 png 图像?

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.使其成为灰度,实际上并没有保存它。

Use the save function of the image modul:使用image模块的save function:

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.这会将您的 Surface 保存为 BMP、TGA、PNG 或 JPEG 图像。 If the filename extension is unrecognized it will default to TGA.如果文件扩展名无法识别,它将默认为 TGA。 Both TGA, and BMP file formats create uncompressed files. TGA 和 BMP 文件格式都创建未压缩文件。

So just use it like this:所以只需像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM