将图像从请求转换为Pygame Surface

时间:2019-07-13 21:36:15

标签: python pygame python-requests

我想从URL获取图像并将其转换为Pygame表面以显示。

import pygame, requests

pygame.init()
screen = pygame.display.set_mode((400, 300))

#I'll use the python logo as an example
url = "https://www.python.org/static/img/python-logo.png"

img_data = requests.get(url).content

我该使用什么命令将接收到的数据(字节)转换为pygame表面以显示到pygame窗口?

1 个答案:

答案 0 :(得分:2)

您可以将图像加载到类似文件的对象中,在其中可以使用pygame读取图像:

import pygame, requests
import pygame.image
import io

.
.
.

r = requests.get(url)
img = io.BytesIO(r.content)

pygame.image.load(img, namehint="") # -> Surface
相关问题