python convert 12 bit image encoded in a string to 8 bit png

Posted by ks on Stack Overflow See other posts from Stack Overflow or by ks
Published on 2010-05-12T04:32:20Z Indexed on 2010/05/12 4:34 UTC
Read the original article Hit count: 138

Filed under:
|
|

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-bit png from this string by ignoring the lowest 4 bits.

I can convert it to a 16-bit image where the highest 4 bits are always zero using PIL with

import Image

imageStr is the image string

imageSize is the image size

img=Image.fromstring("I", imageSize, imageStr, "raw", "I;16", 0,1) img.save("MyImage.png", "PNG")

Anyway I can do something similar to create a 8-bit image without completely unpacking the string doing arithmetic and making a new string?

© Stack Overflow or respective owner

Related posts about python

Related posts about bits