What is the simplest/cleanest way to rescale the intensities of a PIL Image?
Suppose that I have a 16-bit image from a 12-bit camera, so only the values 0–4095 are in use. I would like to rescale the intensities so that the entire range 0–65535 is used. What is the simplest/cleanest way to do this when the image is represented as PIL's Image type?
The best solution I have come up with so far is:
pixels = img.getdata()
img.putdata(pixels, 16)
That works, but always leaves the four least significant bits blank. Ideally, I would like to shift each value four bits to the left, then copy the four most significant bits to the four least significant bits. I don't know how to do that fast.