Ejemplo n.º 1
0
def create_image_instance(data: str, is_local=True) -> Union[None, Image]:
    try:
        # Read images and get dominant colors
        cv_image = get_cv_image_from_b64str(data)
        cv_image = scale_cv_image_to_max_dim(cv_image, MAX_DIM)
        dominant_colors_bgr = get_n_dominant_cv_image_colors(cv_image, DOMINANT_COLORS)
        dominant_colors_hsv = [ bgr_to_hsv(color[0], color[1], color[2]) for color in dominant_colors_bgr ]

        # Detect white-black-gray colors. If they are not the only dominant, then just set that
        #   hue to some unreachable value, so they are never reached when querying the database.
        #   If they are the only colors though, then we just skip adding this image
        only_boring = True
        hues = []
        for i in range(len(dominant_colors_hsv)):
            r = dominant_colors_bgr[i][2]
            g = dominant_colors_bgr[i][1]
            b = dominant_colors_bgr[i][0]

            if is_boring_rgb(r,g,b, BORING_MARGIN):
                hues.append(10000)
            else:
                only_boring = False
                hues.append(dominant_colors_hsv[i][0])

        if only_boring:
            return None

        # Compress jpeg bytes
        byte_array = get_cv_image_as_jpeg_bytes(cv_image)
        compressed_bytes = compress_bytes(byte_array)

        # And finally save it in the database, while doing some Django trickery for saving in-memory binary data
        image = Image(is_local=is_local)
        image.file = ContentFile(compressed_bytes)
        image.file.name = str(uuid.uuid4()).replace('-', '') + '.lzma'

        for i in range(len(hues)):
            setattr(image, f'hue{i+1}', hues[i])

        return image
    except:
        pass

    return None