Ejemplo n.º 1
0
def test_jpg_pil():
    pil_png = PILImage.open(rgb_file_png, mode="r")
    pil_jpg = PILImage.open(rgb_file_jpg, mode="r")
    img_rgb_jpg_true = np.asarray(pil_jpg)
    img_rgb_jpg_true = np.transpose(img_rgb_jpg_true, (2, 0, 1))

    # Test equality when sending JPG buffer from PNG image
    jpg_buffer = io.BytesIO()
    pil_png.save(jpg_buffer, format='JPEG')
    jpg_bytes = jpg_buffer.getvalue()

    pesto_bytes_jpg = PestoImage.from_bytes(jpg_bytes).to_array()

    assert (np.all(pesto_bytes_jpg == img_rgb_jpg_true))

    # Test equality when sending JPG buffer from JPG image
    png_buffer = io.BytesIO()
    pil_jpg.save(png_buffer, format='PNG')
    png_bytes = png_buffer.getvalue()

    pesto_bytes_jpg = PestoImage.from_bytes(png_bytes).to_array()

    assert (np.all(pesto_bytes_jpg == img_rgb_jpg_true))

    # Test closeness of double comrpession
    buffer = io.BytesIO()
    pil_jpg.save(buffer, format='JPEG')
    pil_bytes = buffer.getvalue()

    pesto_bytes_jpg = PestoImage.from_bytes(pil_bytes).to_array()
    pil_bytes_jpg = PILImage.open(io.BytesIO(pil_bytes))
    pil_bytes_jpg = np.asarray(pil_bytes_jpg).transpose((2, 0, 1))

    assert np.all(np.isclose(pil_bytes_jpg, pesto_bytes_jpg))
Ejemplo n.º 2
0
    def _get_result(uri, key_type):
        response = requests.get(uri)
        content_type = response.headers["Content-Type"]

        if key_type == "#/definitions/Images":
            driver = content_type.split("/")[-1]
            path = "{}.{}".format(NamedTemporaryFile().name, driver)
            result = Image.from_bytes(response.content).to_path(path)
        elif key_type == "#/definitions/Image":
            driver = content_type.split("/")[-1]
            path = "{}.{}".format(NamedTemporaryFile().name, driver)
            result = Image.from_bytes(response.content).to_path(path)
        elif key_type in [
            "#/definitions/Metadata",
            "#/definitions/Metadatas",
            "#/definitions/Polygon",
            "#/definitions/Polygons",
            "#/definitions/Tag",
            "#/definitions/Tags",
        ]:
            result = response.json()
        elif key_type == "number":
            result = response.json()
        elif key_type == "string":
            result = response.text
        else:
            result = response.json()

        return result
Ejemplo n.º 3
0
def test_bytes_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()
        pesto_bytes_rgb = PestoImage.from_bytes(bytes_buffer).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()
        pesto_bytes_rgbn = PestoImage.from_bytes(bytes_buffer).to_array()

    assert np.all(img_rgb_true == pesto_bytes_rgb)
    assert np.all(img_rgbn_true == pesto_bytes_rgbn)
Ejemplo n.º 4
0
    def _save_image(self, array: np.ndarray, key: str) -> None:
        image = Image(array=array)

        if image.bands() > 4:
            driver = "tif"
        else:
            driver = self._default_driver

        filename = os.path.join(self.job_path, "{}.{}".format(key, driver))
        log.info("saving image : " + filename)
        image.to_path(filename)
Ejemplo n.º 5
0
def test_image_bytes_conversion():
    data = get_image()

    img = Image.from_array(data)
    img_bytes = img.to_bytes()
    img_back = Image.from_bytes(img_bytes)
    data_back = img_back.array
    data = np.moveaxis(data, 0, -1)
    data_back = np.moveaxis(data_back, 0, -1)

    assert np.array_equal(data.shape, data_back.shape)
Ejemplo n.º 6
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.º 7
0
def test_uri_rio():
    pesto_uri_rgb = PestoImage.from_uri("file://" + rgb_file_tif).to_array()
    pesto_uri_rgbn = PestoImage.from_uri("file://" + nir_file_tif).to_array()
    pesto_uri_rgb_png = PestoImage.from_uri("file://" +
                                            rgb_file_png).to_array()

    assert np.all(img_rgbn_true == pesto_uri_rgbn)
    assert np.all(img_rgb_true == pesto_uri_rgb)
    assert np.all(img_rgb_true == pesto_uri_rgb_png)

    # Ensure that the jpg driver of rasterio produces the same result as the PIL jpg decoder
    with PILImage.open(rgb_file_jpg, mode="r") as pil_jpg:
        img_rgb_jpg_true = np.asarray(pil_jpg)
        img_rgb_jpg_true = np.transpose(img_rgb_jpg_true, (2, 0, 1))

    pesto_uri_rgb_jpg = PestoImage.from_uri("file://" +
                                            rgb_file_jpg).to_array()
    assert np.all(img_rgb_jpg_true == pesto_uri_rgb_jpg)
Ejemplo n.º 8
0
def test_image_roi():
    data = os.path.abspath(
        os.path.join(os.path.dirname(__file__), 'resources/img_256x128.jpg'))
    data = "file://" + data

    roi = ImageROI({
        "target_in": data,
        "target_out": data,
        'lines': {
            'first': 0,
            'number': 100,
            'margin': 10
        },
        'columns': {
            'first': 0,
            'number': 200,
            'margin': 15
        }
    })
    img_full = Image.from_uri(data).array
    img = Image.from_uri(data, roi).array
    assert (img.shape[0] == img_full.shape[0])
Ejemplo n.º 9
0
    def process(self, payload: dict) -> Any:
        try:
            if self._unique_response():
                log.info('response mode : stateless : unique response')
                result_id = list(self.schema.keys())[0]
                path, data_type = self.result_service.get_partial_result(
                    result_id)
                if data_type == ResultType.image:
                    return path, ResultType.image

            log.info('response mode : stateless')
            for key in payload.keys():
                key_type = self.output_properties[key].get('$ref')
                if key_type == '#/definitions/Image':
                    payload[key] = Image.from_array(payload[key]).to_base64()
                elif key_type == '#/definitions/Images':
                    for index, img in enumerate(payload[key]):
                        payload[key][index] = Image.from_array(
                            payload[key][index]).to_base64()
            return payload, ResultType.json
        finally:
            job_path = os.path.join(JobListService.PESTO_WORKSPACE,
                                    self.job_id)
            shutil.rmtree(job_path, ignore_errors=True)
Ejemplo n.º 10
0
def test_roi_rio():
    payload = {
        "target_in": nir_file_tif,
        "target_out": nir_file_tif.replace(".tif", "_out.tif"),
        "lines": {
            "first": 128,
            "number": 128,
            "margin": 32
        },
        "columns": {
            "first": 128,
            "number": 128,
            "margin": 32
        }
    }

    roi = ImageROI(roi=payload)
    pesto_uri_rgbn = PestoImage.from_uri(uri=nir_file_tif, roi=roi).to_array()
    assert np.all(img_rgbn_true[:, (128 - 32):(128 + 128 + 32),
                                (128 - 32):(128 + 128 + 32)] == pesto_uri_rgbn)
Ejemplo n.º 11
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
Ejemplo n.º 12
0
def test_roi():
    payload = create_payload(
        os.path.join(os.path.dirname(__file__), 'resources/img_256x128.jpg'))

    pesto = payload.pop('pesto')
    roi_conf = pesto.pop('roi')
    roi = ImageROI(roi_conf)

    payload = roi.compute_crop_infos().process(payload)

    # load images with cropping
    for _ in roi.target_in:
        payload[_] = Image.from_uri(payload[_], roi).array
        shape = payload[_].shape
        assert shape == (3, 50, 116)

    # remove margins
    payload = roi.remove_margin().process(payload)

    for _ in roi.target_out:
        shape = payload[_].shape
        assert shape == (3, roi.lines.number, roi.columns.number)
Ejemplo n.º 13
0
def open_url(url: str):
    with rasterio.Env(GDAL_HTTP_UNSAFESSL='YES'):
        image = Image.from_uri(url).to_array()
    return image
Ejemplo n.º 14
0
 def load(self, file_path: str) -> str:
     image = Image.from_uri(file_path)
     return image.to_base64()