def _image3_animated_gif(imp, scale_factor=1): """ Function to actually create the animated gif. Args: imp: tuple of tag and a list of image tensors scale_factor: amount to multiply values by. if the image data is between 0 and 1, using 255 for this value will scale it to displayable range """ (tag, ims) = imp ims = [(np.asarray((ims[:, :, i])) * scale_factor).astype(np.uint8) for i in range(ims.shape[2])] ims = [GifImage.fromarray(im) for im in ims] img_str = b'' for b_data in PIL.GifImagePlugin.getheader(ims[0])[0]: img_str += b_data img_str += b'\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50' \ b'\x45\x32\x2E\x30\x03\x01\x00\x00\x00' for i in ims: for b_data in PIL.GifImagePlugin.getdata(i): img_str += b_data img_str += b'\x3B' summary_image_str = summary_pb2.Summary.Image(height=10, width=10, colorspace=1, encoded_image_string=img_str) image_summary = summary_pb2.Summary.Value(tag=tag, image=summary_image_str) return summary_pb2.Summary(value=[image_summary])
def _image3_animated_gif(tag: str, image: Union[np.array, torch.Tensor], scale_factor: float = 1): """Function to actually create the animated gif. Args: tag (str): Data identifier image (np.array or Tensor): 3D image tensors expected to be in `HWD` format scale_factor (float): amount to multiply values by. if the image data is between 0 and 1, using 255 for this value will scale it to displayable range """ assert len( image.shape ) == 3, "3D image tensors expected to be in `HWD` format, len(image.shape) != 3" ims = [(np.asarray((image[:, :, i])) * scale_factor).astype(np.uint8) for i in range(image.shape[2])] ims = [GifImage.fromarray(im) for im in ims] img_str = b"" for b_data in PIL.GifImagePlugin.getheader(ims[0])[0]: img_str += b_data img_str += b"\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50" b"\x45\x32\x2E\x30\x03\x01\x00\x00\x00" for i in ims: for b_data in PIL.GifImagePlugin.getdata(i): img_str += b_data img_str += b"\x3B" summary_image_str = summary_pb2.Summary.Image(height=10, width=10, colorspace=1, encoded_image_string=img_str) image_summary = summary_pb2.Summary.Value(tag=tag, image=summary_image_str) return summary_pb2.Summary(value=[image_summary])
def _image3_animated_gif(tag, ims): check_module('PIL') import PIL from PIL.GifImagePlugin import Image as GIF # x=numpy.random.randint(0,256,[10,10,10],numpy.uint8) ims = [np.asarray((ims[i, :, :]).astype(np.uint8)) for i in range(ims.shape[0])] ims = [GIF.fromarray(im) for im in ims] s = b'' for b in PIL.GifImagePlugin.getheader(ims[0])[0]: s += b s += b'\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50' \ b'\x45\x32\x2E\x30\x03\x01\x00\x00\x00' for i in ims: for b in PIL.GifImagePlugin.getdata(i): s += b s += b'\x3B' if IS_PYTHON2: s = str(s) summary_image_str = summary_pb2.Summary.Image( height=10, width=10, colorspace=1, encoded_image_string=s) image_summary = summary_pb2.Summary.Value( tag=tag, image=summary_image_str) return [summary_pb2.Summary(value=[image_summary]).SerializeToString()]
def _image3_animated_gif(tag, ims): PIL = require_module('PIL') from PIL.GifImagePlugin import Image as GIF # x=numpy.random.randint(0,256,[10,10,10],numpy.uint8) ims = [np.asarray((ims[i, :, :]).astype(np.uint8)) for i in range(ims.shape[0])] ims = [GIF.fromarray(im) for im in ims] s = b'' for b in PIL.GifImagePlugin.getheader(ims[0])[0]: s += b s += b'\x21\xFF\x0B\x4E\x45\x54\x53\x43\x41\x50' \ b'\x45\x32\x2E\x30\x03\x01\x00\x00\x00' for i in ims: for b in PIL.GifImagePlugin.getdata(i): s += b s += b'\x3B' if IS_PYTHON2: s = str(s) summary_image_str = summary_pb2.Summary.Image( height=10, width=10, colorspace=1, encoded_image_string=s) image_summary = summary_pb2.Summary.Value( tag=tag, image=summary_image_str) return [summary_pb2.Summary(value=[image_summary]).SerializeToString()]