width = int(4)
height = int(4)
image_data_raw = ['\xf1', '\xf1', '\xf1', '\xf1',
'\xf1', '\x00', '\x00', '\xf1',
'\xf1', '\x00', '\x00', '\xf1',
'\xf1', '\xf1', '\xf1', '\xf1']
def make_surface_from_raw_data():
global image_data_raw
global width
global height
image_data_raw_bytes = [ord(i) for i in image_data_raw]
test_surface = pygame.Surface((width, height))
pxarray = pygame.PixelArray(test_surface)
i = int(0)
for y in range(height):
for x in range(width):
pxarray[x, y] = pygame.Color(image_data_raw_bytes[i],
image_data_raw_bytes[i],
image_data_raw_bytes[i])
i += 1
new_surface = pxarray[:, :].make_surface()
image_data_2d_surface = new_surface
return image_data_2d_surface
但是,我不满意它,因为它太慢了。
我想知道是否有更快的方法来完成这项任务。在我的循环中,有几个从char到8位int再到rgba的转换。
非常感谢你。