Python – Convert 16bit image to 24bit image 565 to 888

Doing the following is much better than using putpixel and a for loop, as numpy uses c-functions. The actual execution speed is about .3 seconds faster on a 240×320 RGBA image.


import numpy as np
arr = np.fromstring(buff,dtype=np.uint16).astype(np.uint32)
arr = 0xFF000000 + ((arr & 0xF800) >> 8 ) + ((arr & 0x07E0) << 5) + ((arr & 0x001F) << 19)
return Image.frombuffer('RGBA', (xdim,ydim), arr, 'raw', 'RGBA', 0, 1)

While it may seem like an odd bit shift, stuffing the channels into an int requires that it get stuffed as (most significant bit) ABGR (least significant bit). If you were to use the PIL call to putpixel, it would get stuffed as RGBA.

This entry was posted in Image Processing, Python, comp-e and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>