def get_pil_image(img): """ Get image in python friendly format Assumptions are that the image has byte pixels. :return: array containing image :rtype: pil image """ def pil_mode_from_image(img): """ Determine image format from pixel properties May return None if our current encoding does not map to a PIL image mode. """ if img.pixel_type() == img.PIXEL_UNSIGNED and img.pixel_num_bytes( ) == 1: if img.depth() == 3 and img.d_step() == 1 and img.w_step() == 3: return "RGB" elif img.depth() == 4 and img.d_step() == 1 and img.w_step() == 4: return "RGBA" elif img.depth() == 1 and img.w_step() == 1: return "L" elif img.depth() == 1 and img.w_step() == 1: if img.pixel_type() == img.PIXEL_BOOL and img.pixel_num_bytes( ) == 1: return "1" elif img.pixel_type() == img.PIXEL_SIGNED and img.pixel_num_bytes( ) == 4: return "I" elif img.pixel_type() == img.PIXEL_FLOAT and img.pixel_num_bytes( ) == 4: return "F" return None mode = pil_mode_from_image(img) if not mode: # make a copy of this image using contiguous memory with interleaved channels new_img = Image(img.width(), img.height(), img.depth(), True, img.pixel_type(), img.pixel_num_bytes()) new_img.copy_from(img) img = new_img mode = pil_mode_from_image(img) if not mode: raise RuntimeError("Unsupported image format.") # get buffer from image img_pixels = buffer(bytearray(img)) return _pil_image_from_bytes(mode, (img.width(), img.height()), img_pixels, "raw", mode, img.h_step() * img.pixel_num_bytes(), 1)
def get_pil_image(img): """ Get image in python friendly format Assumptions are that the image has byte pixels. :return: array containing image :rtype: pil image """ def pil_mode_from_image(img): """ Determine image format from pixel properties May return None if our current encoding does not map to a PIL image mode. """ if img.pixel_type() == img.PIXEL_UNSIGNED and img.pixel_num_bytes() == 1: if img.depth() == 3 and img.d_step() == 1 and img.w_step() == 3: return "RGB" elif img.depth() == 4 and img.d_step() == 1 and img.w_step() == 4: return "RGBA" elif img.depth() == 1 and img.w_step() == 1: return "L" elif img.depth() == 1 and img.w_step() == 1: if img.pixel_type() == img.PIXEL_BOOL and img.pixel_num_bytes() == 1: return "1" elif img.pixel_type() == img.PIXEL_SIGNED and img.pixel_num_bytes() == 4: return "I" elif img.pixel_type() == img.PIXEL_FLOAT and img.pixel_num_bytes() == 4: return "F" return None mode = pil_mode_from_image(img) if not mode: # make a copy of this image using contiguous memory with interleaved channels new_img = Image(img.width(), img.height(), img.depth(), True, img.pixel_type(), img.pixel_num_bytes()) new_img.copy_from(img) img = new_img mode = pil_mode_from_image(img) if not mode: raise RuntimeError("Unsupported image format.") # get buffer from image if six.PY2: img_pixels = buffer(bytearray(img)) else: img_pixels = memoryview(bytearray(img)).tobytes() pil_img = _pil_image_from_bytes(mode, (img.width(), img.height()), img_pixels, "raw", mode, img.h_step() * img.pixel_num_bytes(), 1) return pil_img