|
|
|
import requests |
|
import json |
|
import base64 |
|
from PIL import Image |
|
import numpy as np |
|
from io import BytesIO |
|
import datetime |
|
with open("spacecrafts.json") as f: |
|
spacecrafts = json.load(f) |
|
|
|
url = "https://earthnow.usgs.gov/observer/passes?op=recorded" |
|
|
|
recordings = requests.get(url).json() |
|
|
|
def remove_black_horizontal_lines(arr): |
|
h, w, *_ = arr.shape |
|
|
|
img_arr = arr.copy() |
|
img_arr[img_arr < 10] = 0 |
|
black_pixels_mask = np.all(img_arr == [0, 0, 0], axis=-1) |
|
|
|
img_arr[black_pixels_mask] = [255,0,0] |
|
black_pixels_mask = np.argwhere(black_pixels_mask) |
|
|
|
delete_ys = [] |
|
for y,x in sorted(zip(*np.unique(black_pixels_mask[:,0], return_counts=True)), key=lambda x: x[1], reverse=True): |
|
if w-x < (w/3): |
|
delete_ys.extend(range(max(y-5, 0), min(y+5, h-1))) |
|
|
|
|
|
new_img_arr = np.delete(arr, delete_ys, 0) |
|
return new_img_arr |
|
|
|
def find_where(l, key, value): |
|
return [d for d in l if d[key] == value][0] |
|
|
|
|
|
for recording in recordings: |
|
spacecraft = find_where(spacecrafts, 'id', recording['spacecraft']) |
|
true_color_bands = find_where(spacecraft['bandCombinations'], 'name', 'True colour')['id'] |
|
|
|
start_time = recording['acquisition-date']['epochSecond'] |
|
start_time_dt = datetime.datetime.fromtimestamp(start_time) |
|
print(recording['spacecraft'], start_time_dt.isoformat()) |
|
max_count = 200 |
|
|
|
request_data = {"op": "allSegments", "pass": recording['id'], "time": start_time, "band": true_color_bands, "count": max_count}; |
|
|
|
resp = requests.get('https://earthnow.usgs.gov/observer/passes', params=request_data) |
|
data = resp.json() |
|
segments = [] |
|
for segment in sorted(data, key=lambda d: int(d['id'])): |
|
id = segment["id"] |
|
b64_data = segment.get('band') |
|
if not b64_data: |
|
continue |
|
bytes = base64.b64decode(b64_data) |
|
img = Image.open(BytesIO(bytes)) |
|
segments.append(np.asarray(img)) |
|
if not segments: |
|
print("no data") |
|
continue |
|
|
|
imgs_comb = np.vstack( segments ) |
|
imgs_comb = remove_black_horizontal_lines(imgs_comb) |
|
imgs_comb = Image.fromarray( imgs_comb) |
|
imgs_comb.save(f"images/{recording['id']}.jpg") |