win2xcur/win2xcur/utils.py
2020-10-04 04:17:20 -04:00

13 lines
337 B
Python

from typing import cast
import numpy
def premultiply_alpha(source: bytes) -> bytes:
buffer = numpy.frombuffer(source, dtype=numpy.uint8).astype(numpy.double)
alpha = buffer[3::4] / 255.0
buffer[0::4] *= alpha
buffer[1::4] *= alpha
buffer[2::4] *= alpha
return cast(bytes, buffer.astype(numpy.uint8).tobytes())