I have python experience but totally new to APIs via Python and Sentinel-1 API.
I am doing research on the Antarctic using Sentinel-1 SAR images from different dates. But getting an error UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x124112b10>.
I have followed the beginner’s guide and have the current code, with the first Sentinel-1 example copied. The code is simply designed to have my code working, so then I can use it for Antarctic regions by changing my bbox coordinates:
from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session
from PIL import Image
import io
import numpy as np
import matplotlib.pyplot as plt
CLIENT_ID = "**************"
CLIENT_SECRET = "***************"
# set up credentials
client = BackendApplicationClient(client_id=CLIENT_ID)
oauth = OAuth2Session(client=client)
# get an authentication token
token = oauth.fetch_token(token_url='https://services.sentinel-hub.com/auth/realms/main/protocol/openid-connect/token',
client_secret=CLIENT_SECRET, include_client_id=True)
bbox = [
-60.54347,
-78.699106,
-27.896624,
-72.127936
]
start_date = "2020-06-01"
end_date = "2020-08-31"
collection_id = "sentinel-1-grd"
evalscript: """
//VERSION=3
function setup() {
return {
input: ["VV"],
output: { id:"default",
bands: 1}
};
}
function evaluatePixel(samples) {
return [2 * samples.VV]}":
"""
# evalscript
evalscript = """
//VERSION=3
function setup() {
return {
input: ["B02", "B03", "B04"],
output: { id: 'default',
bands: 3 }
};
}
function evaluatePixel(sample) {
return [2.5 * sample.B04, 2.5 * sample.B03, 2.5 * sample.B02];
}
"""
# request body/payload
json_request = {
"input": {
"bounds": {
"bbox": [
1360000,
5121900,
1370000,
5131900
],
"properties": {
"crs": "http://www.opengis.net/def/crs/EPSG/0/3857"
}
},
"data": [
{
"dataFilter": {
"timeRange": {
"from": "2019-02-02T00:00:00Z",
"to": "2019-04-02T23:59:59Z"
}
},
"processing": {
"orthorectify": "true"
},
"type": "sentinel-1-grd"
}
]
},
"output": {
"width": 512,
"height": 511.727,
"responses": [
{
"identifier": "default",
"format": {
"type": "image/png"
}
}
]
},
'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
)
# read the image as numpy array
image_arr = np.array(Image.open(io.BytesIO(response.content)))
# plot the image for visualization
plt.figure(figsize=(16,16))
plt.axis('off')
plt.tight_layout()
plt.imshow(image_arr)
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
Cell In[26], line 2
1 # read the image as numpy array
----> 2 image_arr = np.array(Image.open(io.BytesIO(response.content)))
4 # plot the image for visualization
5 plt.figure(figsize=(16,16))
File ~/anaconda3/envs/SentinelHub_VM/lib/python3.11/site-packages/PIL/Image.py:3339, in open(fp, mode, formats)
3337 warnings.warn(message)
3338 msg = "cannot identify image file %r" % (filename if filename else fp)
-> 3339 raise UnidentifiedImageError(msg)
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x124112b10>
If anyone is able to help make my code run well or already has Sentinel-1 SAR code (doesn’t matter on the bands etc) that would be willing to provide, it would be much appreciated.