简体   繁体   English

将 RGBA565 和 RGBA566 图像转换为 PNG

[英]Convert RGBA565 & RGBA566 image to PNG

How do I speed this up?我该如何加快速度? Image1 is composed out of binary RGB565 colours (in little endian) and Image2 is composed out of RGB556 colours (in little endian). Image1由二进制 RGB565 颜色(小端)组成, Image2由 RGB556 颜色(小端)组成。 And put in the input/ folder where the script is located.并放入脚本所在的input/文件夹中。 I can already do the first image but what about the second?我已经可以做第一张图片了,但是第二张呢?
Do I need to use a different image library (preferably in C)?我需要使用不同的图像库(最好是 C 语言)吗?

from os import makedirs
from os.path import join as osjoin, dirname
from PIL import Image
def uncompressed2(filename, width, height):
    # BGR565
    byte = open(osjoin(application_path, "input", filename + ".PTX"), "rb").read()
    Image.frombuffer("RGB", (width, height), byte, "raw", "BGR;16", 0, 1).save(osjoin(application_path, "output2", filename) + ".PNG")
    print("wrote " + filename)

def uncompressed3Manual(filename, width, height):
    # BGR655
    byte = open(osjoin(application_path, "input", filename + ".PTX"), "rb").read()
    img = Image.new('RGB', (width, height))
    index = 0
    for y in range(0, height):
        for x in range(0, width):
            a = byte[index]
            b = byte[index + 1]
            img.putpixel((x,y), (b & 248, 36 * (b & 7) + (a & 192) // 8, 4 * (a & 63)))
            index += 2
    img.save(osjoin(application_path, "output3", filename) + ".PNG")
    print("wrote " + filename)

# Make dirs
makedirs(osjoin(application_path, "output2"), exist_ok = True)
makedirs(osjoin(application_path, "output3"), exist_ok = True)

# uncompressed2("AQUARIUM1", 568, 320)
# RGB556 image
uncompressed3Manual("BACKGROUND1UNSODDED", 1580, 640)

Image 1:图片 1: Image 2:图片 2: This is what I get if I try BGR;16 for the second image:如果我尝试 BGR;16 第二张图片,这就是我得到的结果:

Heading towards swatting-flies-with-anti-aircraft-gun territory, but you could probably read the data into a numpy array, do the arithmetic with numpy, and build the image from that (if I remember correctly, PIL can take numpy arrays as input).朝着用高射炮打苍蝇的方向前进,但您可能会将数据读入 numpy 数组,用 numpy 进行算术运算,然后从中构建图像(如果我没记错的话,PIL 可以采用 numpy arrays作为输入)。 At least that might move the loops inside numpy...至少这可能会移动 numpy 内的循环......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM