def __init__(self, data_size): super(BaseRenderer, self).__init__(data_size) self.colorbar = False self.colormap = 'gray' self.histogram = False self.histogram_bins = 32 self.interpolation = 'nearest' self.grid = False self.axes = False self.title = None self.axes_titles = Coord(None, None) self.axes_offsets = Coord(0.0, 0.0) self.axes_scales = Coord(1.0, 1.0) self.resize = 1.0
def image_size(self): "Returns the size of the image in pixels after cropping and resizing" if isinstance(self.resize, Coord): return self.resize else: return Coord( self.resize * (self.data_size.x - self.crop.left - self.crop.right), self.resize * (self.data_size.y - self.crop.top - self.crop.bottom), )
def canvas_pan_motion(self, event): "Handler for mouse movement in pan mode" inverse = self.image_axes.transData.inverted() start_x, start_y = inverse.transform_point(self._drag_start) end_x, end_y = inverse.transform_point((event.x, event.y)) delta = Coord(int(start_x - end_x), int(start_y - end_y)) if (self._pan_crop.left + delta.x >= 0) and (self._pan_crop.right - delta.x >= 0): self.ui.crop_left_spinbox.setValue(self._pan_crop.left + delta.x) self.ui.crop_right_spinbox.setValue(self._pan_crop.right - delta.x) if (self._pan_crop.top + delta.y >= 0) and (self._pan_crop.bottom - delta.y >= 0): self.ui.crop_top_spinbox.setValue(self._pan_crop.top + delta.y) self.ui.crop_bottom_spinbox.setValue(self._pan_crop.bottom - delta.y)
def parse_axes_offset_option(self, options): "Checks the validity of the --offset option" if options.axes_offset: s = options.axes_offset if ',' in s: x, y = s.split(',', 1) elif '-' in s: x, y = s.split('-', 1) else: x, y = s, s try: result = Coord(float(x), float(y)) except ValueError: self.parser.error('{} is not a valid --offset'.format( options.offset)) return result
def parse_axes_scale_option(self, options): "Checks the validity of the --scale option" if options.axes_scale: s = options.axes_scale if ',' in s: x, y = s.split(',', 1) elif '-' in s: x, y = s.split('-', 1) else: x, y = s, s try: result = Coord(float(x), float(y)) except ValueError: self.parser.error('{} is not a valid --scale'.format( options.scale)) if result.x == 0.0 or result.y == 0.0: self.parser.error('--scale multiplier cannot be 0.0') return result
def canvas_press(self, event): "Handler for mouse press on graph canvas" if event.button != 1: return if event.inaxes != self.image_axes: return self._drag_start = Coord(event.x, event.y) if self.window().ui.zoom_mode_action.isChecked(): self._zoom_id = self.canvas.mpl_connect('motion_notify_event', self.canvas_zoom_motion) elif self.window().ui.pan_mode_action.isChecked(): self._pan_id = self.canvas.mpl_connect('motion_notify_event', self.canvas_pan_motion) self._pan_crop = Crop(top=self.ui.crop_top_spinbox.value(), left=self.ui.crop_left_spinbox.value(), bottom=self.ui.crop_bottom_spinbox.value(), right=self.ui.crop_right_spinbox.value()) self.redraw_timer.setInterval(REDRAW_TIMEOUT_PAN)
def parse_resize_option(self, options): "Checks the validity of the --offset option" if options.resize: s = options.resize try: if not (',' in s or 'x' in s): result = float(s) if result == 0.0: self.parser.error('--resize multiplier cannot be 0.0') else: if ',' in s: x, y = s.split(',', 1) else: x, y = s.split('x', 1) result = Coord(float(x), float(y)) except ValueError: self.parser.error('{} is not a valid --resize value'.format( options.resize)) return result
def margin(self): "Returns the size of the margins when drawing" return Coord(0.75, 0.25) if self.margins_visible else Coord(0.0, 0.0)
def main(self, options, args): if options.list_colormaps: self.list_colormaps() sys.stdout.write('The following colormaps are available:\n\n') sys.stdout.write('\n'.join(name for name in self.list_colormaps() if not name.endswith('_r'))) sys.stdout.write('\n\n') sys.stdout.write( 'Append _r to any colormap name to reverse it. Previews at:\n') sys.stdout.write( 'http://matplotlib.sourceforge.net/examples/pylab_examples/' 'show_colormaps.html\n\n') return 0 if options.list_interpolations: sys.stdout.write( 'The following image interpolation algorithms are ' 'available:\n\n') sys.stdout.write('\n'.join(self.list_interpolations())) sys.stdout.write('\n\n') return 0 if options.list_formats: sys.stdout.write('The following input formats are supported:\n\n') sys.stdout.write('\n'.join( '{0:<8} - {1}'.format(ext, desc) for (ext, desc) in self.list_input_formats())) sys.stdout.write('\n\n') sys.stdout.write('The following output formats are supported:\n\n') sys.stdout.write('\n'.join( '{0:<8} - {1}'.format(ext, desc) for (ext, desc) in self.list_output_formats())) sys.stdout.write('\n\n') return 0 # Configure the renderer from the command line options data_file = self.parse_files(options, args) options.layers = self.parse_layers(options, data_file) if options.layers: if options.show_colorbar: self.parser.error('you may not use --color-bar with --layers') renderer = LayeredRenderer((data_file.x_size, data_file.y_size)) else: renderer = ChannelRenderer((data_file.x_size, data_file.y_size)) renderer.colormap = self.parse_colormap_option(options) renderer.crop = self.parse_crop_option(options) renderer.clip = self.parse_range_options(options) renderer.resize = self.parse_resize_option(options) renderer.colorbar = options.show_colorbar renderer.histogram = options.show_histogram renderer.histogram_bins = options.bins renderer.title = options.title renderer.axes = options.show_axes or options.title_x or options.title_y renderer.axes_offsets = self.parse_axes_offset_option(options) renderer.axes_scales = self.parse_axes_scale_option(options) renderer.axes_titles = Coord(options.title_x, options.title_y) renderer.grid = options.show_grid renderer.empty = options.empty (canvas_class, canvas_method, multi_class, default_interpolation) = self.parse_output_options(options) renderer.interpolation = self.parse_interpolation_option( options, default_interpolation) # Extract the specified channels logging.info( 'File contains %d channels, extracting channels %s', len(data_file.channels), ','.join( str(channel.index) for channel in data_file.channels if channel.enabled)) if options.layers: filename = options.output.format(**data_file.format_dict( **renderer.format_dict())) logging.warning('Writing all layers to %s', filename) figure = renderer.draw(*options.layers) canvas = canvas_class(figure) canvas_method(canvas, filename) elif options.multi: filename = options.output.format(**data_file.format_dict( **renderer.format_dict())) logging.warning('Writing all channels to %s', filename) output = multi_class(filename) try: for channel in data_file.channels: if channel.enabled: logging.warning( 'Writing channel %d (%s) to new page/layer', channel.index, channel.name) figure = renderer.draw(channel) if figure is not None: canvas = canvas_class(figure) output.savefig( figure, title='{channel} - {channel_name}'.format( **channel.format_dict())) finally: output.close() else: for channel in data_file.channels: if channel.enabled: filename = options.output.format(**channel.format_dict( **renderer.format_dict())) logging.warning('Writing channel %d (%s) to %s', channel.index, channel.name, filename) figure = renderer.draw(channel) if figure is not None: # Finally, dump the figure to disk as whatever format # the user requested canvas = canvas_class(figure) canvas_method(canvas, filename)
def __init__(self, data_size): self.data_size = Coord(*data_size) self.crop = Crop(0, 0, 0, 0) self.clip = None self.empty = False