How to get image values from Process API using Python response

Hello,

input_arg = {
“bounds”: { “geometry”:poly,
“properties”: {“crs”: f"http://www.opengis.net/def/crs/EPSG/0/{EPSG_code}"}},
“data”: [{ “type”: “sentinel-2-l2a”,
“dataFilter”: {“timeRange”: {“from”: f"{sd}T00:00:00Z",
“to”: f"{ed}T00:00:00Z"}}}]
}

output_arg = {
“resx”: res,
“resy”: res,
“responses”: [{“identifier”: “default”,
“format”: { “type”: “image/tiff”}}]
}

evalscript = ‘’’
//VERSION=3
function setup() {
return {
input: [“B04”, “B08”],
output:
{
bands: 1,
sampleType: “FLOAT32”
}
}
}

function evaluatePixel(sample) {

return [(sample.B08-sample.B04)/(sample.B08+sample.B04)];
}
‘’’

json_request = {‘input’:input_arg,‘output’:output_arg,‘evalscript’:evalscript}

Set the request url and headers

url_request = ‘https://services.sentinel-hub.com/api/v1/process
headers_request = {
“Authorization” : “Bearer %s” %token[‘access_token’]
}

#Send the request
response = oauth.request(“POST”, url_request, headers=headers_request, json=json_request)

When I use this request I get one image which I can save to a tiff file:

dst = path_to_foldr/_sentinelHub.tif’
with open(dst, ‘wb’) as f:
f.write(response.content)

Now I would like to get the values of the images or the image as a numpy array (not to save as a tif file) but I can’t find how to do so.

When I try:

response.content

I get:

b’MM\x00*\x00\x00\x00\x08\x00\x11\x01\x00\x00\x03\x00\x00\x00\x01\x009\x00\x00\x01\x01\x00\x03\x00\x00\x00\x01\x00&\x00\x00\x01\x02\x00\x03\x00\x00\x00\x01\x00 \x00\x00\x01\x03\x00\x03\x00\x00\x00\x01\x80\xb2\x00\x00\x01\x06\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01\x11\x00\x04\x00\x00\x00\x02\x00\x00\x00\xdc\x01\x15\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01\x16\x00\x03\x00\x00\x00\x01\x00#\x00\x00\x01\x17\x00\x04\x00\x00\x00\x02\x00\x00\x00\xe4\x01\x1a\x00\x05\x00\x00\x00\x01\x00\x00\x00\xec\x01\x1b\x00\x05\x00\x00\x00\x01\x00\x00\x00\xf4\x01(\x00\x03\x00\x00\x00\x01\x00\x01\x00\x00\x01S\x00\x03\x00\x00\x00\x01\x00\x03\x00\x00\x83\x0e\x00\x0c\x00\x00\x00\x03\x00\x00\x00\xfc\x84\x82\x00\x0c\x00\x00\x00\x06\x00\x00\x01\x14\x87\xaf\x00\x03\x00\x00\x00\x1c\x00\x00\x01D\x87\xb1\x00\x02\x00\x00\x00\x0f\x00\x00\x01|\x00\x00\x00\x00\x00\x00\x00\x00\x01\x8b\x00\x00\x13\x93\x00\x00\x12\x08\x00\x00\x00\x8f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01?\x1a\r\xad\xe0<5\xe5?\x1a(\xc2\x1a\x95\ry\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@A\xc7%5C\xae\xab@@>\xbb\x01\xc9-\xdc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x00\x00\x00\x06\x04\x00\x00\x00\x00\x01\x00\x02\x04\x01\x00\x00\x00\x01\x00\x01\x04\x02\x87\xb1\x00\x06\x00\x00\x08\x00\x00\x00\x00\x01\x10\xe6\x08\x01\x87\xb1\x00\x06\x00\x07\x08\x06\x00\x00\x00\x01#\x8eWGS 84|WGS 84|\x00x^\xcd\x98gt\x94\xe5\xba\x86A1(R\x0c\x1d\xb2\x11P\xa4(DPJ\x0c\xf1\xbb \x10B(\t!\xa43\x93\x99

Hi @org_accounts,

I recommend checking how things are implemented in sentinelhub-py. E.g. decoding of a tiff image from bytes to a numpy array happens here.

1 Like

@maleksandrov - thanks, this link helped.

I used:

import tifffile as tiff
from io import BytesIO
img_array = tiff.imread(BytesIO(response.content))