将 PIL 图像转换为 pygame 表面图像
Convert PIL Image into pygame surface image
我正在尝试使用 PIL.Image 将 .png 图像加载到程序中,以便我可以操纵它们,准备用作精灵中的 pygame 表面。以下代码显示了我如何尝试将这些 Pil 图像转换为 pygame 图像:
bytes = someImagefile.tobytes()
new_image = pygame.image.fromstring(bytes, size, "RGB")
我得到:"ValueError: String length does not equal format and resolution size"
有没有办法在玩完之后不保存新的 .png 副本来执行此操作?
以下代码适用于我。 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.Image 将 .png 图像加载到程序中,以便我可以操纵它们,准备用作精灵中的 pygame 表面。以下代码显示了我如何尝试将这些 Pil 图像转换为 pygame 图像:
bytes = someImagefile.tobytes()
new_image = pygame.image.fromstring(bytes, size, "RGB")
我得到:"ValueError: String length does not equal format and resolution size"
有没有办法在玩完之后不保存新的 .png 副本来执行此操作?
以下代码适用于我。 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)