def parseNpf(self, buffer, imageWidth, imageHeight):
# Read the header
sectionLengths = self._readUgarHeader(buffer)
# Read the palette data (section number 1)
paletteData = np.frombuffer(buffer.read(roundToPower(sectionLengths[0])), dtype=np.uint16)
# Read the image data (section number 2)
imageData = np.frombuffer(buffer.read(sectionLengths[1]), dtype=np.uint8)
# NPF image data uses 1 byte per 2 pixels, so we need to split that byte into two
imageData = np.stack((np.bitwise_and(imageData, 0x0f), np.bitwise_and(imageData >> 4, 0x0f)), axis=-1).flatten()
# Unpack palette colors
palette = unpackColors(paletteData, useAlpha=False)
# Convert each pixel from a palette index to full color
pixels = np.fromiter((palette[i] if i > 0 else 0 for i in imageData), dtype=">u4")
# Clip the image data and create a Pillow image from it
return Image.fromarray(self._clipImageData(pixels, (imageWidth, imageHeight)), mode="RGBA")
# Write the image as an npf to buffer
评论列表
文章目录