def mean_color(self, img, order: str = 'rgb'): """ Calculates and returns the mean/average color of an image Params: img: (numpy.array, PIL.image, cv2.image) order: (RGB, BGR) input order of the colors BGR/RGB. Deafult order: RGB Returns: Tuple of RGB values of the mean color calculated """ image = helpers.format_image_to_PIL(img, order) return ca.mean_color(image)
def trim_to_content(self, img, order: str = 'rgb'): """ Trim/Crop an image to its content (removes uniform color spaced padding around the image) Params: img: (numpy.array, PIL.image, cv2.image) order: (RGB, BGR) input order of the colors BGR/RGB. Deafult order: RGB Returns: numpy.array of the order specified """ image = helpers.format_image_to_PIL(img, order) return tr.trim(image, order)
def ahash(self, image, hash_size: int = 8, order: str = 'rgb'): """ Average Hash computation Params: image - must be a PIL instance image or numpy array in RGB or opencv image in BGR hash_size - (integer) default 8 for 64 bit hash order - (string) RGB, BGR: input order of the colors BGR/RGB. Deafult order: RGB Returns: <ImageHash> object. To get the hash value simply use - str(<ImageHash>) """ image = helpers.format_image_to_PIL(image, order) return ahash(image, hash_size)
def phash(self, image, hash_size=8, highfreq_factor=4, order: str = 'rgb'): """ Perceptual Hash computation. Params: image - must be a PIL instance image or numpy array in RGB or opencv image in BGR hash_size - an integer specifying the hash size (hash_size * highfreq_factor should be less than number of rows or columns of the gray_image) highfreq_factor - an integer specyfing the highfrequency factor Returns: <ImageHash> object. To get the hash value simply use - str(<ImageHash>) """ image = helpers.format_image_to_PIL(image, order) return phash(image, hash_size, highfreq_factor)
def image_segmentation(image, rgb_list, order: str = 'rgb'): """ reconstruct an image with only a specified list of colors Params: img: (numpy.array, PIL.image, cv2.image) rgb_list: colors list - a 2 dimensional np array with shape (n,3) 3 being the channel values in order RGB, eg: [[224, 166, 147], [110, 34, 71], [195, 98, 100]] order: (RGB, BGR) input order of the colors BGR/RGB. Deafult order: RGB Note: The output will be a numpy.array of the same order Returns: numpy.array of the order specified """ image = helpers.format_image_to_PIL(image, order) # create an array of pixel values from the given image pixels = np.array(image.getdata(), dtype=np.uint8) # convert rgb_list to a numpy array rgb_list = np.array(rgb_list) rgb_list = np.array([rgb_list]) if rgb_list.ndim == 1 else rgb_list if not rgb_list.ndim == 2 or not rgb_list.shape[1] == 3: raise ValueError( 'rgb_list must be a two dimensional array of shape (n, 3)') # create an array of empty pixel values new_pixels = [None] * len(image.getdata()) # assign new pixel the color closest to the original image's pixel value for idx, pixel in enumerate(pixels): shortest = float('Inf') for color in rgb_list: distance = helpers.calculate_distance(color, pixel) if distance < shortest: shortest = distance nearest = color new_pixels[idx] = nearest _w, _h = image.size # reconstruct the image np array with the new pixels new_pixels = np.asarray(new_pixels)\ .astype('uint8')\ .reshape((_h, _w, 3)) return helpers.format_output_order_input_RGB(new_pixels, order)
def whash(self, image, hash_size=8, image_scale=None, mode='haar', remove_max_haar_ll=True, order: str = 'rgb'): """ Wavelet Hash computation. Params: image - must be a PIL instance image or numpy array in RGB or opencv image in BGR hash_size - must be a power of 2 and less than 'image_scale' image_scale- must be power of 2 and less than image size. By default is equal to max power of 2 for an input image. mode (see modes in pywt library): 'haar' - Haar wavelets, by default 'db4' - Daubechies wavelets remove_max_haar_ll - remove the lowest low level (LL) frequency using Haar wavelet. Returns: <ImageHash> object. To get the hash value simply use - str(<ImageHash>) """ image = helpers.format_image_to_PIL(image, order) return whash(image, hash_size, image_scale, mode, remove_max_haar_ll)