如何将 RGBA 字节转换为 PNG 图像?

How to convert RGBA bytes to PNG image?

如何加快速度? 我的 image 由二进制 RGBA 颜色组成。

from PIL import Image
redOffset = 0
blueOffset = 2

byte = open("ALWAYSLOADED_384_00.PTX", "rb").read()
width = 256
height = 256
img = Image.new('RGBA', (width, height))
index = 0
for y in range(0, height):
    for x in range(0, width):
        img.putpixel((x,y), (byte[index + redOffset],byte[index + 1],byte[index + blueOffset], byte[index + 3]))
        index += 4
    
img.save("ALWAYSLOADED_384_00.PNG")

让 PIL 直接在您的缓冲区上运行,而不是创建新图像。

data = open("ALWAYSLOADED_384_00.PTX", "rb").read()
img = Image.frombuffer("RGBA", (256, 256), data, "raw", "RGBA", 0, 1)
img.save("ALWAYSLOADED_384_00.PNG")

文档:PIL.Image.frombuffer.