'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). And put in the input/ folder where the script is located. I can already do the first image but what about the second?
Do I need to use a different image library (preferably in 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:
Image 2:
This is what I get if I try BGR;16 for the second image:

Solution 1:[1]
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). At least that might move the loops inside numpy...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ture PÄlsson |
