Ejemplo n.º 1
0
def test_b64_rio():
    # Test RGB
    output_profile = dict(
        driver="GTiff",
        dtype=img_rgb_true.dtype,
        count=img_rgb_true.shape[0],
        height=img_rgb_true.shape[1],
        width=img_rgb_true.shape[2],
    )

    with rasterio.MemoryFile() as memfile:
        with memfile.open(**output_profile) as dst:
            dst.write(img_rgb_true)

        bytes_buffer = memfile.read()
        bytes_b64 = base64.b64encode(bytes_buffer)
        string_b64 = bytes_b64.decode('utf-8')
        pesto_b64_rgb = PestoImage.from_base64(string_b64).to_array()

    # Test RGBN
    output_profile = dict(
        driver="GTiff",
        dtype=img_rgbn_true.dtype,
        count=img_rgbn_true.shape[0],
        height=img_rgbn_true.shape[1],
        width=img_rgbn_true.shape[2],
    )

    with rasterio.MemoryFile() as memfile:
        with memfile.open(**output_profile) as dst:
            dst.write(img_rgbn_true)

        bytes_buffer = memfile.read()
        bytes_b64 = base64.b64encode(bytes_buffer)
        string_b64 = bytes_b64.decode('utf-8')
        pesto_b64_rgbn = PestoImage.from_base64(string_b64).to_array()

    assert np.all(img_rgb_true == pesto_b64_rgb)
    assert np.all(img_rgbn_true == pesto_b64_rgbn)
Ejemplo n.º 2
0
    def _load_image(self, image_ref: Union[str, bytes]) -> np.ndarray:
        if isinstance(image_ref, str) and any(
            [image_ref.startswith(uri) for uri in self.URI_STRINGS]):
            image = Image.from_uri(image_ref, roi=self.roi)
            source = "uri"
        else:
            try:
                image = Image.from_bytes(image_ref)
                source = "bytes"
            except:
                image = Image.from_base64(image_ref)
                source = "base64"

        data = image.array
        log.info("loading image : {} : shape={}".format(source, data.shape))
        return data
Ejemplo n.º 3
0
    def _compare_vals(expected_v: Any, actual_v: Any) -> bool:

        if _is_file(expected_v) and _is_file(actual_v):
            expected_path = expected_v.replace("file://", "")
            response_path = actual_v.replace("file://", "")
            return filecmp.cmp(response_path, expected_path)
        elif (not _is_file(actual_v)) and (_is_file(expected_v)
                                           and _is_image(expected_v)):
            response_path = "{}{}".format(NamedTemporaryFile().name,
                                          os.path.splitext(expected_v)[1])
            response_path = Image.from_base64(actual_v).to_path(response_path)
            response_path = response_path.replace("file://", "")
            expected_path = expected_v.replace("file://", "")
            return filecmp.cmp(response_path, expected_path)
        elif isinstance(expected_v, list):
            return all([
                any([_compare_vals(v1, v2) for v2 in actual_v])
                for v1 in expected_v
            ])
        else:
            return expected_v == actual_v