def display_image(np_rgb, text=None, scale_up=False): """ Display an image with optional text above image. Args: np_rgb: RGB image tile as a NumPy array text: Optional text to display above image scale_up: If True, scale up image to display by slide.SCALE_FACTOR """ if scale_up: np_rgb = np.repeat(np_rgb, slide.SCALE_FACTOR, axis=1) np_rgb = np.repeat(np_rgb, slide.SCALE_FACTOR, axis=0) img_r, img_c, img_ch = np_rgb.shape if text is not None: np_t = np_text(text) t_r, t_c, _ = np_t.shape t_i_c = max(t_c, img_c) t_i_r = t_r + img_r t_i = np.zeros([t_i_r, t_i_c, img_ch], dtype=np.uint8) t_i.fill(255) t_i[0:t_r, 0:t_c] = np_t t_i[t_r:t_r + img_r, 0:img_c] = np_rgb np_rgb = t_i pil_img = util.np_to_pil(np_rgb) pil_img.show()
def create_summary_pil_img(np_img, title_area_height, row_tile_size, col_tile_size, num_row_tiles, num_col_tiles): """ Create a PIL summary image including top title area and right side and bottom padding. Args: np_img: Image as a NumPy array. title_area_height: Height of the title area at the top of the summary image. row_tile_size: The tile size in rows. col_tile_size: The tile size in columns. num_row_tiles: The number of row tiles. num_col_tiles: The number of column tiles. Returns: Summary image as a PIL image. This image contains the image data specified by the np_img input and also has potentially a top title area and right side and bottom padding. """ r = row_tile_size * num_row_tiles + title_area_height c = col_tile_size * num_col_tiles summary_img = np.zeros([r, c, np_img.shape[2]], dtype=np.uint8) # add gray edges so that tile text does not get cut off summary_img.fill(120) # color title area white summary_img[0:title_area_height, 0:summary_img.shape[1]].fill(255) summary_img[title_area_height:np_img.shape[0] + title_area_height, 0:np_img.shape[1]] = np_img summary = util.np_to_pil(summary_img) return summary
def apply_filters_to_image(slide_name, save=True, display=False): """ Apply a set of filters to an image and optionally save and/or display filtered images. Args: slide_name: The slide name. save: If True, save filtered images. display: If True, display filtered images to screen. Returns: Tuple consisting of 1) the resulting filtered image as a NumPy array, and 2) dictionary of image information (used for HTML page generation). """ t = Time() print(f"Processing slide {slide_name}") info = dict() if save and not os.path.exists(slide.FILTER_DIR): os.makedirs(slide.FILTER_DIR) np_orig = slide.get_slide(slide_name) filtered_np_img = apply_image_filters(np_orig, slide_name, info, save=False, display=False) if save: result_path = slide.get_filter_image_result(slide_name) pil_img = util.np_to_pil(filtered_np_img) pil_img.save(result_path) return filtered_np_img, info
def add_tile_stats_to_top_tile_summary(pil_img, tiles, z): np_sum = util.pil_to_np_rgb(pil_img) sum_r, sum_c, sum_ch = np_sum.shape np_stats = np_tile_stat_img(tiles) st_r, st_c, _ = np_stats.shape combo_c = sum_c + st_c combo_r = max(sum_r, st_r + z) combo = np.zeros([combo_r, combo_c, sum_ch], dtype=np.uint8) combo.fill(255) combo[0:sum_r, 0:sum_c] = np_sum combo[z:st_r + z, sum_c:sum_c + st_c] = np_stats result = util.np_to_pil(combo) return result
def pil_hue_histogram(h): """ Create Matplotlib histogram of hue values for an HSV image and return the histogram as a PIL image. Args: h: Hue values as a 1-dimensional int NumPy array (scaled 0 to 360) Returns: Matplotlib histogram of hue values converted to a PIL image. """ np_hist = np_hsv_hue_histogram(h) pil_hist = util.np_to_pil(np_hist) return pil_hist
def save_filtered_image(np_img, slide_name, filter_num, filter_text): """ Save a filtered image to the file system. Args: np_img: Image as a NumPy array. slide_name: The slide number. filter_num: The filter number. filter_text: Descriptive text to add to the image filename. """ t = Time() filepath = slide.get_filter_image_path(slide_name, filter_num, filter_text) pil_img = util.np_to_pil(np_img) pil_img.save(filepath) print("%-20s | Time: %-14s Name: %s" % ("Save Image", str(t.elapsed()), filepath))
def get_pil_scaled_tile(self): return util.np_to_pil(self.np_scaled_tile)