Python Convert 12 Bit Image Encoded In A String To 8 Bit Png
I have a string that is read from a usb apogee camera that is a 12-bit grayscale image with the 12-bits each occupying the lowest 12 bits of 16-bits words. I want to create a 8-bi
Solution 1:
Wump's comment about converting an image gave me an idea, and I did it by
#shifts by 4 bits and converts to 8-bit imageimg = img.point(lambda i: i * 16, "L")
Thanks Wump
Solution 2:
The only way I know how to do it would be:
data = numpy.fromstring(imageStr, numpy.uint16)
data >>= 4 # shift out four bits
data = numpy.array(data, dtype=numpy.uint8)
img = Image.fromarray(data.reshape(imageSize))
In principe, PIL can convert images this way:
img = img.convert("L")
But the problem is that it has no way to reduce the precision to 8 bits (AFAIK), so everything will be clipped to 255 :)
Edit: removed intermediate string conversion, it's going directly from numpy to PIL now
Post a Comment for "Python Convert 12 Bit Image Encoded In A String To 8 Bit Png"