将pygame表面转换为图像? [英] convert a pygame surface into an image?

查看:73
本文介绍了将pygame表面转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

使用 image模块的"noreferrer">save函数:

Use the save function of the image modul:

pygame.image.save()
将图像保存到磁盘
save(Surface, filename) ->无

这会将您的 Surface 保存为 BMP、TGA、PNG 或 JPEG 图像.如果无法识别文件扩展名,则默认为 TGA.TGA 和 BMP 文件格式都会创建未压缩的文件.

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

这篇关于将pygame表面转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆