def color_ratios(img): check_img_arg(img) n = np.prod(img.shape[:2]) imh = img_to_2d_num_hash(img) counts = Counter(np.ravel(imh)) color_ratios = [float(count) / n for _, count in counts.items()] return color_ratios
def save(img, savefile, use_default=False): check_img_arg(img) if use_default: savefile = os.path.join(DEFAULT_DIR, savefile) else: savedir = os.path.dirname(savefile) if not os.path.isdir(savedir): print('WARNING: creating directory {}'.format(savedir)) os.mkdir(savedir) mplim.imsave(savefile, img)
def save_as_gif(imgs, savefile, use_default=False): if not isinstance(imgs, list): raise TypeError('Expecting a list as first argument.') check_img_arg(imgs[0], allow_grayscale=True) if use_default: savefile = os.path.join(DEFAULT_DIR, savefile) else: savedir = os.path.dirname(savefile) if not os.path.isdir(savedir): print('WARNING: creating directory {}'.format(savedir)) os.mkdir(savedir) with imageio.get_writer(savefile, mode='I') as writer: for img in imgs: writer.append_data(img)
def display_info(imgfile, img=None): check_imgfile_arg(imgfile) # size of file, format, dimension etype = imgfile.split('.')[-1].upper() ftype = identify_format(imgfile) fsize, size_descr = identify_filesize(imgfile) if img is not None: check_img_arg(img) fdim = img.shape[:2] else: fdim = identify_dimensions(imgfile) print('Extension : {}'.format(etype)) print('True format : {}'.format(ftype)) print('Dimensions : {} x {}'.format(*fdim)) print('Size of file : {} {}'.format(fsize, size_descr))
def background_color(img): check_img_arg(img) mode = Counter(np.ravel(img_to_2d_num_hash(img))).most_common(1)[0][0] return np.array(num_hash_to_rgb(int(mode)))
def dist_to_color(img, col): check_img_arg(img) col = check_color(col, to_numpy=True) return np.linalg.norm(img - col[np.newaxis, np.newaxis, :])
def glob_luminance(img): check_img_arg(img) return np.mean(img_to_luminance(img))
def num_colors(img): check_img_arg(img) return len(np.unique(np.ravel(img_to_2d_num_hash(img))))
def complement(img): check_img_arg(img) h, w = img.shape[:2] img_mod = np.array( [complementary_color(img[i, j]) for i in range(h) for j in range(w)]) return img_mod.reshape(img.shape)