mapbox_rgb.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import numpy as np
  2. class _RGB:
  3. def process(self, image):
  4. width = image.shape[1]
  5. height = image.shape[0]
  6. data = np.zeros((height, width, 3), np.uint8)
  7. for row in range(height):
  8. for column in range(width):
  9. pixel = data[row][column]
  10. v = int((image[row][column] + 10000) * 10)
  11. pixel[0] = v >> 16 & 0x0000FF
  12. pixel[1] = v >> 8 & 0x0000FF
  13. pixel[2] = v & 0x0000FF
  14. pass
  15. pass
  16. return data, 'RGB'
  17. pass
  18. class _RGBA:
  19. def __init__(self, nodata):
  20. self._nodata = nodata
  21. pass
  22. def process(self, image):
  23. width = image.shape[1]
  24. height = image.shape[0]
  25. data = np.zeros((height, width, 4), np.uint8)
  26. for row in range(height):
  27. for column in range(width):
  28. pixel = data[row][column]
  29. v = int((image[row][column] + 10000) * 10)
  30. pixel[0] = v >> 16 & 0x0000FF
  31. pixel[1] = v >> 8 & 0x0000FF
  32. pixel[2] = v & 0x0000FF
  33. pixel[3] = 0 if v == self._nodata else 255
  34. pass
  35. pass
  36. return data, 'RGBA'
  37. pass
  38. class RendererMapboxRGB:
  39. def __init__(self, datatype, nodata = None):
  40. self._datatype = datatype
  41. self._nodata = nodata
  42. if self._nodata is None:
  43. self._implement = _RGB()
  44. pass
  45. else:
  46. self._implement = _RGBA(nodata)
  47. pass
  48. pass
  49. def process(self, image):
  50. return self._implement.process(image)
  51. pass