def test_tiff_depths(self): """ Tests the conversions between all supported dtypes in tiff. """ tif_file = tempfile.NamedTemporaryFile(suffix='.tif', prefix="ipol_", delete=True) for src_depth in ('8i', '16i', '32i', '16f', '32f'): for dst_depth in ( '8i', '16i', '32i'): # float as a destination is not strictly equal data = self.data_matrix(src_depth) im = Image(data=data) im.convert_depth(dst_depth) im.write(tif_file.name) im = Image(src=tif_file.name) data = self.data_matrix(dst_depth) self.assertTrue( im.data.dtype is data.dtype, msg="dtype expected={} found={}, conversion failed {} > {}" .format(data.dtype, im.data.dtype, src_depth, dst_depth)) self.assertTrue( (im.data == data).all(), msg="data alteration during conversion {} > {}.".format( src_depth, dst_depth)) tif_file.close()
def convert_tiff_to_png(self, img): """ Converts the input TIFF to PNG. This is used by the web interface for visualization purposes """ data = {"status": "KO"} try: buf = base64.b64decode(img) # cv2.IMREAD_ANYCOLOR option try to convert to uint8, 7x faster than matrix conversion # but fails with some tiff formats (float) im = Image(buf=buf) im.convert_depth('8i') buf = im.encode('.png') data["img"] = binascii.b2a_base64(buf).decode() # re-encode bytes data["status"] = "OK" except Exception: message = "TIFF to PNG for client, conversion failure." self.logger.exception(message) return json.dumps(data, indent=4).encode()