如何在 Python 3.7 中显示用 Pygame 加载 Pillow 的图像?

How to display Images loaded with Pillow with Pygame in Python 3.7?

我使用

将图像导入到我的项目中
from PIL import Image
myImage = Image.open("myImageDirectory.png")

所以 myImage 现在被导入为 png 文件。但我想使用 Pygame 将它显示到屏幕上。通常我使用

import pygame
win = pygame.display.set_mode((500, 500))
win.blit(myImage, (50, 50))

现在我得到错误提示,该函数需要一个表面,而不是一个 png 文件。 有谁知道如何将图像转换为表面或如何显示它?

我还没有尝试太多,因为我没有找到任何可以解决我问题的方法。

编辑:

What is wrong with this way that I get the error: Couldn't open bg.png

参见PIL and pygame.image. Use Image.tobytes() get the image data as a bytes object and pygame.image.fromstring() to load the data to an pygame.Surface对象:

from PIL import Image
import pygame
pilImage = Image.open("myImageDirectory.png")
myImage = pygame.image.fromstring(pilImage.tobytes(), pilImage.size, pilImage.mode)