本文介绍了将PIL图像转换为pygame表面图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用PIL.Image将.png图像加载到程序中,以便我可以操作它们,准备好用作sprite中的pygame表面。以下代码显示了我如何尝试将这些Pil图像转换为pygame图像:
I'm trying to load .png images into a program using PIL.Image, so that I can manipulate them, ready for use as pygame surfaces in sprites. The following code shows how I've tried to convert those Pil Images into pygame images :
bytes = someImagefile.tobytes()
new_image = pygame.image.fromstring(bytes, size, "RGB")
我'得到:ValueError:字符串长度不等于格式和分辨率大小
I'm getting : "ValueError: String length does not equal format and resolution size"
有没有办法在我完成后不保存新的.png副本玩它吗?
Is there a way to do this without saving a new .png copy after I'm done with playing with it?
推荐答案
以下代码适用于我。 Python2.7 + PIL 2.5 + Pygame1.9.2
The following code works for me. Python2.7+PIL 2.5+Pygame1.9.2
import Image
import pygame
image = Image.open("SomeImage.png")
mode = image.mode
size = image.size
data = image.tobytes()
py_image = pygame.image.fromstring(data, size, mode)
这篇关于将PIL图像转换为pygame表面图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!