import os.path import jarray from org.das2.graph import DasColorBar from java import io from java.awt import image as awtimage from javax import imageio import javax.imageio.metadata import javax.imageio.stream ARGB = imageio.ImageTypeSpecifier.createFromBufferedImageType(awtimage.BufferedImage.TYPE_INT_ARGB) COMMON_GIF_ATTRIBUTES = { 'disposalMethod': 'none', 'userInputFlag': 'FALSE', 'transparentColorFlag': 'FALSE', 'transparentColorIndex': '0', } LOOP_NODE_ATTRIBUTES = { 'applicationID': 'NETSCAPE', 'authenticationCode': '2.0', } def set_node_attr(node, attributes_dict): for key, value in attributes_dict.items(): node.setAttribute(key, value) def write_gif(images, path, delay_tenths_sec=5, loop=True): if not images: return gif_writer = imageio.ImageIO.getImageWritersBySuffix('gif').next() write_param = gif_writer.getDefaultWriteParam() metadata = gif_writer.getDefaultImageMetadata(ARGB, write_param) metadata_format = metadata.getNativeMetadataFormatName() print metadata_format root = metadata.getAsTree(metadata_format) graphics_control_extension_node = imageio.metadata.IIOMetadataNode('GraphicControlExtension') root.appendChild(graphics_control_extension_node) graphics_control_extension_node.setAttribute('delayTime', '%d' % delay_tenths_sec) set_node_attr(graphics_control_extension_node, COMMON_GIF_ATTRIBUTES) if loop: app_extensions_node = imageio.metadata.IIOMetadataNode('ApplicationExtensions') root.appendChild(app_extensions_node) app_extension_node = imageio.metadata.IIOMetadataNode('ApplicationExtension') app_extensions_node.appendChild(app_extension_node) set_node_attr(app_extension_node, LOOP_NODE_ATTRIBUTES) app_extension_node.setUserObject(jarray.array([1, 0, 0], 'b')) metadata.setFromTree(metadata_format, root) out = imageio.stream.FileImageOutputStream(io.File(path)) gif_writer.setOutput(out) gif_writer.prepareWriteSequence(None) for image in images: gif_writer.writeToSequence(imageio.IIOImage(image, None, metadata), write_param) gif_writer.endWriteSequence() out.close() def plot_to_gif(plot_func, path, keyword_params=[], delay_tenths_sec=10, loop=True): images = [] for params in keyword_params: plot_func(**params) images.append(writeToBufferedImage()) write_gif(images, path, delay_tenths_sec=delay_tenths_sec, loop=loop) def xor_mod_plot(n=256, modulus=9): x = outerProduct(linspace(0, n - 1, n), ones(n)) plot(gt(bitwiseXor(x, transpose(x)) % modulus, zeros(n, n)), renderType='nnSpectrogram>rebin=noInterpolate') dom.plots[0].setColortable(DasColorBar.Type.GRAYSCALE) annotation(0, text='m = %d' % modulus, borderType='Rounded_Rectangle') plot_to_gif(xor_mod_plot, os.path.expanduser('~/autoplot/xor_mod.gif'), keyword_params=[dict(modulus=m) for m in range(2, 255)], delay_tenths_sec=50)
An occasionally updated blog, mostly related to programming. (Views are my own, not my employer's.)
Saturday, April 10, 2021
Animation of x ^ y % m for various m
Thanks to some example code and other generous help from Jeremy, I'm excited to have been able to create an animated GIF of x XOR y MOD m for various m in Autoplot.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment