def multi_image_composite(fn, red_channel, blue_channel, green_channel = None, alpha_channel = None): r"""Write an image with different color channels corresponding to different quantities. Accepts at least a red and a blue array, of shape (N,N) each, that are optionally scaled and composited into a final image, written into `fn`. Can also accept green and alpha. Parameters ---------- fn : string Filename to save red_channel : array_like or tuple of image info Array, of shape (N,N), to be written into the red channel of the output image. If not already uint8, will be converted (and scaled) into uint8. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale). blue_channel : array_like or tuple of image info Array, of shape (N,N), to be written into the blue channel of the output image. If not already uint8, will be converted (and scaled) into uint8. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale). green_channel : array_like or tuple of image info, optional Array, of shape (N,N), to be written into the green channel of the output image. If not already uint8, will be converted (and scaled) into uint8. If not supplied, will be left empty. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale). alpha_channel : array_like or tuple of image info, optional Array, of shape (N,N), to be written into the alpha channel of the output image. If not already uint8, will be converted (and scaled) into uint8. If not supplied, will be made fully opaque. Optionally, you can also specify a tuple that includes scaling information, in the form of (array_to_plot, min_value_to_scale, max_value_to_scale). Examples -------- >>> red_channel = np.log10(frb["Temperature"]) >>> blue_channel = np.log10(frb["Density"]) >>> multi_image_composite("multi_channel1.png", red_channel, blue_channel) """ red_channel = scale_image(red_channel) blue_channel = scale_image(blue_channel) if green_channel is None: green_channel = np.zeros(red_channel.shape, dtype='uint8') else: green_channel = scale_image(green_channel) if alpha_channel is None: alpha_channel = np.zeros(red_channel.shape, dtype='uint8') + 255 else: alpha_channel = scale_image(alpha_channel) image = np.array([red_channel, green_channel, blue_channel, alpha_channel]) image = image.transpose().copy() # Have to make sure it's contiguous pw.write_png(image, fn)
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 write_image(image, filename, color_bounds=None, cmap_name=None, func=lambda x: x): r"""Write out a floating point array directly to a PNG file, scaling it and applying a colormap. This function will scale an image and directly call libpng to write out a colormapped version of that image. It is designed for rapid-fire saving of image buffers generated using `yt.visualization.api.FixedResolutionBuffers` and the likes. Parameters ---------- image : array_like This is an (unscaled) array of floating point values, shape (N,N,) to save in a PNG file. filename : string Filename to save as. color_bounds : tuple of floats, optional The min and max to scale between. Outlying values will be clipped. cmap_name : string, optional An acceptable colormap. See either yt.visualization.color_maps or https://scipy-cookbook.readthedocs.io/items/Matplotlib_Show_colormaps.html . func : function, optional A function to transform the buffer before applying a colormap. Returns ------- scaled_image : uint8 image that has been saved Examples -------- >>> sl = ds.slice(0, 0.5, "Density") >>> frb1 = FixedResolutionBuffer(sl, (0.2, 0.3, 0.4, 0.5), (1024, 1024)) >>> write_image(frb1["Density"], "saved.png") """ if cmap_name is None: cmap_name = ytcfg.get("yt", "default_colormap") if len(image.shape) == 3: mylog.info("Using only channel 1 of supplied image") image = image[:, :, 0] to_plot = apply_colormap(image, color_bounds=color_bounds, cmap_name=cmap_name) pw.write_png(to_plot, filename) return to_plot
def write_image(image, filename, color_bounds = None, cmap_name = "algae", func = lambda x: x): r"""Write out a floating point array directly to a PNG file, scaling it and applying a colormap. This function will scale an image and directly call libpng to write out a colormapped version of that image. It is designed for rapid-fire saving of image buffers generated using `yt.visualization.api.FixedResolutionBuffers` and the like. Parameters ---------- image : array_like This is an (unscaled) array of floating point values, shape (N,N,) to save in a PNG file. filename : string Filename to save as. color_bounds : tuple of floats, optional The min and max to scale between. Outlying values will be clipped. cmap_name : string, optional An acceptable colormap. See either yt.visualization.color_maps or http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps . func : function, optional A function to transform the buffer before applying a colormap. Returns ------- scaled_image : uint8 image that has been saved Examples -------- >>> sl = ds.slice(0, 0.5, "Density") >>> frb1 = FixedResolutionBuffer(sl, (0.2, 0.3, 0.4, 0.5), (1024, 1024)) >>> write_image(frb1["Density"], "saved.png") """ if len(image.shape) == 3: mylog.info("Using only channel 1 of supplied image") image = image[:,:,0] to_plot = apply_colormap(image, color_bounds = color_bounds, cmap_name = cmap_name) pw.write_png(to_plot, filename) return to_plot