diff --git a/app/api/common.py b/app/api/common.py index 4f58d333..024f7526 100644 --- a/app/api/common.py +++ b/app/api/common.py @@ -40,4 +40,25 @@ def path_traversal_check(unsafe_path, known_safe_path): raise SuspiciousFileOperation("{} is not safe".format(unsafe_path)) # Passes the check - return unsafe_path \ No newline at end of file + return unsafe_path + +def hex2rgb(hex_color, with_alpha=False): + """ + Adapted from https://stackoverflow.com/questions/29643352/converting-hex-to-rgb-value-in-python/29643643 + """ + hex_color = hex_color.lstrip('#') + if len(hex_color) != 6: + if with_alpha: + return tuple((255, 255, 255, 255)) + else: + return tuple((255, 255, 255)) + try: + v = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) + if with_alpha: + v += (255, ) + return v + except ValueError: + if with_alpha: + return tuple((255, 255, 255, 255)) + else: + return tuple((255, 255, 255)) \ No newline at end of file diff --git a/app/api/custom_colormaps_helper.py b/app/api/custom_colormaps_helper.py index bf1176cc..5838fc64 100644 --- a/app/api/custom_colormaps_helper.py +++ b/app/api/custom_colormaps_helper.py @@ -1,19 +1,14 @@ -import matplotlib -import numpy -def generate_discrete_color_map_from_list_of_hex(list_of_hex_colors): - colormap = matplotlib.colors.ListedColormap(list_of_hex_colors) - color_map_dict = extract_colormap_dict_from_arr(colormap) - return color_map_dict -def generate_linerar_segmented_color_map_from_list_of_hex(list_of_hex_colors, name_of_colormap="default_name", N=256): - colormap = matplotlib.colors.LinearSegmentedColormap.from_list(name_of_colormap, list_of_hex_colors, N) - color_map_dict = extract_colormap_dict_from_arr(colormap) - return color_map_dict -def extract_colormap_dict_from_arr(colormap): - x = numpy.linspace(0, 1, 256) - cmap_vals = colormap(x)[:, :] - cmap_uint8 = (cmap_vals * 255).astype('uint8') - ndvi_dict = {idx: value.tolist() for idx, value in enumerate(cmap_uint8)} - return ndvi_dict +import math +from .common import hex2rgb + +def discrete_cmap_from_hex(hex_colors): + rgb_colors = [hex2rgb(h, with_alpha=True) for h in hex_colors] + res = {} + for x in range(0, 255): + idx = math.floor(x / 256.0 * len(rgb_colors)) + res[x] = rgb_colors[idx] + return res + ndvi_arr = [ '#AD0028', @@ -46,7 +41,7 @@ contrast_ndvi_arr = [ ] custom_colormaps = [ - {"discrete_ndvi": generate_discrete_color_map_from_list_of_hex(contrast_ndvi_arr)}, - {"better_discrete_ndvi": generate_discrete_color_map_from_list_of_hex(ndvi_arr)}, + {"discrete_ndvi": discrete_cmap_from_hex(contrast_ndvi_arr)}, + {"better_discrete_ndvi": discrete_cmap_from_hex(ndvi_arr)}, #add custom maps here ] diff --git a/app/api/imageuploads.py b/app/api/imageuploads.py index d88f8a78..6fa2d8a0 100644 --- a/app/api/imageuploads.py +++ b/app/api/imageuploads.py @@ -9,6 +9,7 @@ from app.models.task import assets_directory_path from PIL import Image, ImageDraw, ImageOps from django.http import HttpResponse from .tasks import download_file_response +from .common import hex2rgb import numpy as np def normalize(img): @@ -26,18 +27,6 @@ def normalize(img): return Image.fromarray(arr) -def hex2rgb(hex_color): - """ - Adapted from https://stackoverflow.com/questions/29643352/converting-hex-to-rgb-value-in-python/29643643 - """ - hex_color = hex_color.lstrip('#') - if len(hex_color) != 6: - return tuple((255, 255, 255)) - try: - return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) - except ValueError: - return tuple((255, 255, 255)) - class Thumbnail(TaskNestedView): def get(self, request, pk=None, project_pk=None, image_filename=""): """ diff --git a/requirements.txt b/requirements.txt index be7b4bf6..98d5c760 100644 --- a/requirements.txt +++ b/requirements.txt @@ -63,5 +63,4 @@ Shapely==1.7.0 ; sys_platform == "win32" eventlet==0.25.1 ; sys_platform == "win32" pyopenssl==19.1.0 ; sys_platform == "win32" numpy==1.21.1 -urllib3~=1.24.1 -matplotlib==3.4.3 \ No newline at end of file +urllib3~=1.24.1 \ No newline at end of file