api.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # main imports
  2. import os
  3. import requests
  4. import json
  5. # image processing imports
  6. from io import BytesIO
  7. from PIL import Image
  8. # import config variables
  9. from ..config import GET_SCENE_IMAGE_API_URL, GET_SCENE_QUALITIES_API_URL, GET_SCENES_API_URL
  10. from ..config import DIRAN_DOMAIN_NAME
  11. def get_image(scene, img_quality):
  12. '''
  13. Return specific image of scene dataset with quality
  14. '''
  15. if not type(img_quality) == str:
  16. img_quality = str(img_quality)
  17. # get URL to contact
  18. url = GET_SCENE_IMAGE_API_URL.format(scene, img_quality)
  19. # Make a get request to get information of scene image with quality of 200
  20. response = requests.get(url)
  21. # Print the content of the response formatted into JSON
  22. content_json = json.loads(response.content)
  23. # Access to link of image using 'key' (data & link) from json data
  24. api_link = content_json['data']['link']
  25. image_url = DIRAN_DOMAIN_NAME + api_link
  26. print(image_url)
  27. # Ask API to get acess to image
  28. response_img = requests.get(image_url)
  29. # Convert content of the response (the whole image) and parse it using BytesIO
  30. print("Access to image located at : ", image_url)
  31. return Image.open(BytesIO(response_img.content))
  32. def get_scene_qualities(scene):
  33. '''
  34. Return all qualities known from scene
  35. '''
  36. # construct `url` to get qualities
  37. url = GET_SCENE_QUALITIES_API_URL.format(scene)
  38. response = requests.get(url)
  39. # Print the content of the response formatted into JSON
  40. content_json = json.loads(response.content)
  41. # return list of qualities
  42. return content_json['data']
  43. def get_scenes():
  44. '''
  45. Return list of scenes available into dataset
  46. '''
  47. url = GET_SCENES_API_URL
  48. # get scene list
  49. response = requests.get(url)
  50. # Print the content of the response formatted into JSON
  51. content_json = json.loads(response.content)
  52. # return list of scenes
  53. return content_json['data']