def map(self, L, x, y): dd = 1.0 / (2.0**(int(L))) relx = int(x) * dd rely = int(y) * dd DW = (self.ds.domain_right_edge - self.ds.domain_left_edge) xl = self.ds.domain_left_edge[0] + relx * DW[0] yl = self.ds.domain_left_edge[1] + rely * DW[1] xr = xl + dd * DW[0] yr = yl + dd * DW[1] frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256)) cmi, cma = get_color_bounds( self.data['px'], self.data['py'], self.data['pdx'], self.data['pdy'], self.data[self.field], self.ds.domain_left_edge[0], self.ds.domain_right_edge[0], self.ds.domain_left_edge[1], self.ds.domain_right_edge[1], dd * DW[0] / (64 * 256), dd * DW[0]) if self.ds._get_field_info(self.field).take_log: cmi = np.log10(cmi) cma = np.log10(cma) to_plot = apply_colormap(np.log10(frb[self.field]), color_bounds=(cmi, cma)) else: to_plot = apply_colormap(frb[self.field], color_bounds=(cmi, cma)) rv = write_png_to_string(to_plot) return rv
def map(self, L, x, y): dd = 1.0 / (2.0**(int(L))) relx = int(x) * dd rely = int(y) * dd DW = (self.ds.domain_right_edge - self.ds.domain_left_edge) xl = self.ds.domain_left_edge[0] + relx * DW[0] yl = self.ds.domain_left_edge[1] + rely * DW[1] xr = xl + dd*DW[0] yr = yl + dd*DW[1] frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256)) cmi, cma = get_color_bounds(self.data['px'], self.data['py'], self.data['pdx'], self.data['pdy'], self.data[self.field], self.ds.domain_left_edge[0], self.ds.domain_right_edge[0], self.ds.domain_left_edge[1], self.ds.domain_right_edge[1], dd*DW[0] / (64*256), dd*DW[0]) if self.ds._get_field_info(self.field).take_log: cmi = np.log10(cmi) cma = np.log10(cma) to_plot = apply_colormap(np.log10(frb[self.field]), color_bounds = (cmi, cma)) else: to_plot = apply_colormap(frb[self.field], color_bounds = (cmi, cma)) rv = write_png_to_string(to_plot) return rv
def write_bitmap(bitmap_array, filename, max_val=None, transpose=False): r"""Write out a bitmapped image directly to a PNG file. This accepts a three- or four-channel `bitmap_array`. If the image is not already uint8, it will be scaled and converted. If it is four channel, only the first three channels will be scaled, while the fourth channel is assumed to be in the range of [0,1]. If it is not four channel, a fourth alpha channel will be added and set to fully opaque. The resultant image will be directly written to `filename` as a PNG with no colormap applied. `max_val` is a value used if the array is passed in as anything other than uint8; it will be the value used for scaling and clipping in the first three channels when the array is converted. Additionally, the minimum is assumed to be zero; this makes it primarily suited for the results of volume rendered images, rather than misaligned projections. Parameters ---------- bitmap_array : array_like Array of shape (N,M,3) or (N,M,4), to be written. If it is not already a uint8 array, it will be scaled and converted to uint8. filename : string Filename to save to. If None, PNG contents will be returned as a string. max_val : float, optional The upper limit to clip values to in the output, if converting to uint8. If `bitmap_array` is already uint8, this will be ignore. transpose : boolean, optional If transpose is False, we assume that the incoming bitmap_array is such that the first element resides in the upper-left corner. If True, the first element will be placed in the lower-left corner. """ if len(bitmap_array.shape) != 3 or bitmap_array.shape[-1] not in (3, 4): raise RuntimeError( "Expecting image array of shape (N,M,3) or " "(N,M,4), received %s" % str(bitmap_array.shape) ) if bitmap_array.dtype != np.uint8: s1, s2 = bitmap_array.shape[:2] if bitmap_array.shape[-1] == 3: alpha_channel = 255 * np.ones((s1, s2, 1), dtype="uint8") else: alpha_channel = (255 * bitmap_array[:, :, 3]).astype("uint8") alpha_channel.shape = s1, s2, 1 if max_val is None: max_val = bitmap_array[:, :, :3].max() bitmap_array = np.clip(bitmap_array[:, :, :3] / max_val, 0.0, 1.0) * 255 bitmap_array = np.concatenate( [bitmap_array.astype("uint8"), alpha_channel], axis=-1 ) if transpose: bitmap_array = bitmap_array.swapaxes(0, 1).copy(order="C") if filename is not None: pw.write_png(bitmap_array, filename) else: return pw.write_png_to_string(bitmap_array.copy()) return bitmap_array
def write_bitmap(bitmap_array, filename, max_val = None, transpose=False): r"""Write out a bitmapped image directly to a PNG file. This accepts a three- or four-channel `bitmap_array`. If the image is not already uint8, it will be scaled and converted. If it is four channel, only the first three channels will be scaled, while the fourth channel is assumed to be in the range of [0,1]. If it is not four channel, a fourth alpha channel will be added and set to fully opaque. The resultant image will be directly written to `filename` as a PNG with no colormap applied. `max_val` is a value used if the array is passed in as anything other than uint8; it will be the value used for scaling and clipping in the first three channels when the array is converted. Additionally, the minimum is assumed to be zero; this makes it primarily suited for the results of volume rendered images, rather than misaligned projections. Parameters ---------- bitmap_array : array_like Array of shape (N,M,3) or (N,M,4), to be written. If it is not already a uint8 array, it will be scaled and converted to uint8. filename : string Filename to save to. If None, PNG contents will be returned as a string. max_val : float, optional The upper limit to clip values to in the output, if converting to uint8. If `bitmap_array` is already uint8, this will be ignore. transpose : boolean, optional If transpose is False, we assume that the incoming bitmap_array is such that the first element resides in the upper-left corner. If True, the first element will be placed in the lower-left corner. """ if len(bitmap_array.shape) != 3 or bitmap_array.shape[-1] not in (3,4): raise RuntimeError if bitmap_array.dtype != np.uint8: s1, s2 = bitmap_array.shape[:2] if bitmap_array.shape[-1] == 3: alpha_channel = 255*np.ones((s1,s2,1), dtype='uint8') else: alpha_channel = (255*bitmap_array[:,:,3]).astype('uint8') alpha_channel.shape = s1, s2, 1 if max_val is None: max_val = bitmap_array[:,:,:3].max() bitmap_array = np.clip(bitmap_array[:,:,:3] / max_val, 0.0, 1.0) * 255 bitmap_array = np.concatenate([bitmap_array.astype('uint8'), alpha_channel], axis=-1) if transpose: bitmap_array = bitmap_array.swapaxes(0,1).copy(order="C") if filename is not None: pw.write_png(bitmap_array, filename) else: return pw.write_png_to_string(bitmap_array.copy()) return bitmap_array
def __call__(self, val): from yt.utilities.png_writer import write_png_to_string from yt.visualization.image_writer import map_to_colors image = np.log10(val) mi = np.nanmin(image[~np.isinf(image)]) ma = np.nanmax(image[~np.isinf(image)]) color_bounds = mi, ma image = (image - color_bounds[0])/(color_bounds[1] - color_bounds[0]) to_plot = map_to_colors(image, "algae") to_plot = np.clip(to_plot, 0, 255) s = write_png_to_string(to_plot) response_body = "data:image/png;base64," + base64.encodestring(s) tf.close() self.transport.append(response_body)
def map(self, field, L, x, y): if "," in field: field = tuple(field.split(",")) cmap = self.cmap dd = 1.0 / (2.0**(int(L))) relx = int(x) * dd rely = int(y) * dd DW = self.ds.domain_right_edge - self.ds.domain_left_edge xl = self.ds.domain_left_edge[0] + relx * DW[0] yl = self.ds.domain_left_edge[1] + rely * DW[1] xr = xl + dd * DW[0] yr = yl + dd * DW[1] try: self.lock() w = 256 # pixels data = self.data[field] frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (w, w)) cmi, cma = get_color_bounds( self.data["px"], self.data["py"], self.data["pdx"], self.data["pdy"], data, self.ds.domain_left_edge[0], self.ds.domain_right_edge[0], self.ds.domain_left_edge[1], self.ds.domain_right_edge[1], dd * DW[0] / (64 * 256), dd * DW[0], ) finally: self.unlock() if self.takelog: cmi = np.log10(cmi) cma = np.log10(cma) to_plot = apply_colormap(np.log10(frb[field]), color_bounds=(cmi, cma), cmap_name=cmap) else: to_plot = apply_colormap(frb[field], color_bounds=(cmi, cma), cmap_name=cmap) rv = write_png_to_string(to_plot) return rv
def map(self, field, L, x, y): if ',' in field: field = tuple(field.split(',')) dd = 1.0 / (2.0**(int(L))) relx = int(x) * dd rely = int(y) * dd DW = (self.ds.domain_right_edge - self.ds.domain_left_edge) xl = self.ds.domain_left_edge[0] + relx * DW[0] yl = self.ds.domain_left_edge[1] + rely * DW[1] xr = xl + dd*DW[0] yr = yl + dd*DW[1] try: self.lock() data = self.data[field] frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256)) cmi, cma = get_color_bounds(self.data['px'], self.data['py'], self.data['pdx'], self.data['pdy'], data, self.ds.domain_left_edge[0], self.ds.domain_right_edge[0], self.ds.domain_left_edge[1], self.ds.domain_right_edge[1], dd*DW[0] / (64*256), dd*DW[0]) finally: self.unlock() if self.takelog: cmi = np.log10(cmi) cma = np.log10(cma) to_plot = apply_colormap(np.log10(frb[field]), color_bounds = (cmi, cma)) else: to_plot = apply_colormap(frb[field], color_bounds = (cmi, cma)) rv = write_png_to_string(to_plot) return rv