def add_image_info_cb(self, viewer, channel, info): save_thumb = self.settings.get('cache_thumbs', False) # Do we already have this thumb loaded? chname = channel.name thumbkey = self.get_thumb_key(chname, info.name, info.path) thumbpath = self.get_thumbpath(info.path) with self.thmblock: try: bnch = self.thumb_dict[thumbkey] # if these are not equal then the mtime must have # changed on the file, better reload and regenerate if bnch.thumbpath == thumbpath: return except KeyError: pass # Is there a cached thumbnail image on disk we can use? thmb_image = RGBImage.RGBImage() loaded = False if (thumbpath is not None) and os.path.exists(thumbpath): #save_thumb = False try: # try to load the thumbnail image thmb_image.load_file(thumbpath) # make sure name is consistent thmb_image.set(name=info.name) loaded = True except Exception as e: self.logger.warning("Error loading thumbnail: %s" % (str(e))) if not loaded: # no luck loading thumbnail, try to load the full image try: thmb_image = channel.get_loaded_image(info.name) except KeyError: self.logger.info("image not in memory; using placeholder") # load a plcaeholder image tmp_path = os.path.join(icondir, 'fits.png') thmb_image = RGBImage.RGBImage() thmb_image.load_file(tmp_path) # make sure name is consistent thmb_image.set(name=info.name, path=None) self.fv.gui_do(self._make_thumb, chname, thmb_image, info.name, info.path, thumbkey, info.image_future, save_thumb=save_thumb, thumbpath=thumbpath)
def __init__(self, fv, fitsimage): # superclass defines some variables for us, like logger super(Overlays, self).__init__(fv, fitsimage) self.layertag = 'overlays-canvas' self.dc = fv.get_draw_classes() canvas = self.dc.DrawingCanvas() canvas.enable_draw(False) canvas.set_surface(self.fitsimage) self.canvas = canvas self.colornames = colors.get_colors() # TODO: there is some problem with basic "red", at least on Linux #self.hi_color = 'red' self.hi_color = 'palevioletred' self.hi_value = None self.lo_color = 'blue' self.lo_value = None self.opacity = 0.5 self.arrsize = None self.rgbarr = np.zeros((1, 1, 4), dtype=np.uint8) self.rgbobj = RGBImage.RGBImage(logger=self.logger, data_np=self.rgbarr) self.canvas_img = None
def load_image(self, filepath): # Create an image. Assume type to be an AstroImage unless # the MIME association says it is something different. try: typ, subtyp = self.guess_filetype(filepath) except Exception: # Can't determine file type: assume and attempt FITS typ, subtyp = 'image', 'fits' if (typ == 'image') and (subtyp != 'fits'): image = RGBImage.RGBImage(logger=self.logger) else: image = AstroImage.AstroImage(logger=self.logger) try: self.logger.info("Loading image from %s" % (filepath)) image.load_file(filepath) #self.gui_do(chinfo.fitsimage.onscreen_message, "") except Exception as e: errmsg = "Failed to load file '%s': %s" % (filepath, str(e)) self.logger.error(errmsg) try: (type, value, tb) = sys.exc_info() tb_str = "\n".join(traceback.format_tb(tb)) except Exception as e: tb_str = "Traceback information unavailable." self.gui_do(self.show_error, errmsg + '\n' + tb_str) #chinfo.fitsimage.onscreen_message("Failed to load file", delay=1.0) raise ControlError(errmsg) self.logger.debug("Successfully loaded file into image object.") return image
def load_idx(self, idx, **kwargs): if idx is None: idx = self.find_first_good_idx() typ = self.get_idx_type(idx) if typ == 'image': from ginga import AstroImage, RGBImage header = self.get_header(idx) data_np = np.copy(self._f[idx].value) if 'PALETTE' in header: p_idx = header['PALETTE'] p_data = self._f[p_idx].value data_np = p_data[data_np] image = RGBImage.RGBImage(logger=self.logger) else: image = AstroImage.AstroImage(logger=self.logger) image.update_keywords(header) image.set_data(data_np) name = iohelper.name_image_from_path(self._path, idx=idx) image.set(path=self._path, name=name, idx=idx, image_loader=load_file) return image raise ValueError("I don't know how to read dataset '{}'".format(idx))
def _regen_thumb_image(self, image, viewer): self.logger.debug("generating new thumbnail") if not isinstance(image, BaseImage.BaseImage): # this is not a regular image type image = RGBImage.RGBImage() tmp_path = os.path.join(icondir, 'fits.png') image.load_file(tmp_path) self.thumb_generator.set_image(image) if viewer is not None: v_img = viewer.get_image() if v_img is not None: viewer.copy_attributes(self.thumb_generator, self.transfer_attrs) rgb_img = self.thumb_generator.get_image_as_array() thmb_image = RGBImage.RGBImage(rgb_img) thmb_image.set(placeholder=False) return thmb_image
def _get_thumb_image(self, channel, info, image): # Get any previously stored thumb information in the image info thumb_extra = info.setdefault('thumb_extras', Bunch.Bunch()) # Choice [A]: is there a thumb image attached to the image info? if 'rgbimg' in thumb_extra: # yes return thumb_extra.rgbimg thumbpath = self.get_thumbpath(info.path) # Choice [B]: is the full image available to make a thumbnail? if image is None: try: image = channel.get_loaded_image(info.name) except KeyError: pass if image is not None: try: thmb_image = self._regen_thumb_image(image, None) thumb_extra.rgbimg = thmb_image thumb_extra.time_update = time.time() return thmb_image except Exception as e: self.logger.warning("Error generating thumbnail: %s" % (str(e))) thmb_image = RGBImage.RGBImage() thmb_image.set(name=info.name, placeholder=False) # Choice [C]: is there a cached thumbnail image on disk we can use? if (thumbpath is not None) and os.path.exists(thumbpath): try: # try to load the thumbnail image thmb_image.load_file(thumbpath) thmb_image = self._regen_thumb_image(thmb_image, None) thumb_extra.rgbimg = thmb_image return thmb_image except Exception as e: self.logger.warning("Error loading thumbnail: %s" % (str(e))) # Choice [D]: load a placeholder image tmp_path = os.path.join(icondir, 'fits.png') thmb_image.load_file(tmp_path) thmb_image.set(path=None, placeholder=True) return thmb_image
def _regen_thumb_image(self, image, viewer): self.logger.debug("generating new thumbnail") self.thumb_generator.set_image(image) if viewer is not None: v_img = viewer.get_image() if v_img is not None: viewer.copy_attributes(self.thumb_generator, self.transfer_attrs) rgb_img = self.thumb_generator.get_image_as_array() thmb_image = RGBImage.RGBImage(rgb_img) thmb_image.set(placeholder=False) return thmb_image
def __init__(self, fv, fitsimage): # superclass defines some variables for us, like logger super(Overlays, self).__init__(fv, fitsimage) self.layertag = 'overlays-canvas' self.dc = fv.getDrawClasses() canvas = self.dc.DrawingCanvas() canvas.enable_draw(False) canvas.setSurface(self.fitsimage) self.canvas = canvas self.colornames = colors.get_colors() self.hi_color = 'red' self.hi_value = None self.lo_color = 'blue' self.lo_value = None self.opacity = 0.5 self.arrsize = None self.rgbarr = numpy.zeros((1, 1, 4), dtype=numpy.uint8) self.rgbobj = RGBImage.RGBImage(self.rgbarr, logger=self.logger) self.canvas_img = None
def __init__(self, fv): # superclass defines some variables for us, like logger super(ColorMapPicker, self).__init__(fv) # read preferences for this plugin prefs = self.fv.get_preferences() self.settings = prefs.createCategory('plugin_ColorMapPicker') self.settings.addDefaults(cbar_ht=20, cbar_wd=300, cbar_sep=10, cbar_pan_accel=1.0) self.settings.load(onError='silent') self._cmht = self.settings.get('cbar_ht', 20) self._cmwd = self.settings.get('cbar_wd', 300) self._cmsep = self.settings.get('cbar_sep', 10) self._cmxoff = 20 self._wd = 300 self._ht = 400 self._max_y = 0 # create a PIL viewer that we use to construct an RGB image # containing all the possible color bars and their labels self.p_view = CanvasView(logger=self.logger) p_v = self.p_view p_v.configure_surface(self._wd, self._ht) p_v.enable_autozoom('off') p_v.enable_autocuts('off') p_v.set_scale_limits(1.0, 1.0) p_v.set_pan(0, 0) p_v.zoom_to(1) p_v.cut_levels(0, 255) p_v.set_bg(0.4, 0.4, 0.4) # this will hold the resulting RGB image self.r_image = RGBImage.RGBImage(logger=self.logger) self.c_view = None self.cm_names = []
def __init__(self, logger, ev_quit, options): super(GingaVision, self).__init__() self.logger = logger self.ev_quit = ev_quit from ginga.gw import Widgets, Viewers, GwMain self.card = 'default' # playback rate; changed when we know the actual rate self.fps = options.fps self.playback_rate = 1.0 / 30.0 self.pimage = RGBImage.RGBImage() self.pdata = None self.app = Widgets.Application(logger=logger) self.app.add_callback('shutdown', self.quit) self.top = self.app.make_window("Ginga example2") self.top.add_callback('close', lambda *args: self.quit()) thread_pool = Task.ThreadPool(2, logger, ev_quit=ev_quit) thread_pool.startall() self.main = GwMain.GwMain(logger=logger, ev_quit=ev_quit, app=self.app, thread_pool=thread_pool) vbox = Widgets.VBox() vbox.set_border_width(2) vbox.set_spacing(1) fi = Viewers.CanvasView(logger=logger) fi.set_autocut_params('histogram') fi.enable_autozoom('off') fi.enable_autocenter('once') fi.enable_autocuts('off') fi.cut_levels(0, 255) fi.scale_to(1, 1) fi.set_bg(0.2, 0.2, 0.2) fi.ui_set_active(True) self.viewer = fi if options.optimize: # Some optimizations to smooth playback at decent FPS # PassThruRGBMapper is the most efficient mapper rgbmap = RGBMap.PassThruRGBMapper(self.logger) fi.set_rgbmap(rgbmap) # Clip cuts assumes data does not need to be scaled in cut levels-- # only clipped fi.set_autocuts(AutoCuts.Clip(logger=self.logger)) bd = fi.get_bindings() bd.enable_all(True) fi.set_desired_size(512, 512) iw = Viewers.GingaViewerWidget(viewer=fi) vbox.add_widget(iw, stretch=1) hbox = Widgets.HBox() hbox.set_margins(4, 2, 4, 2) wopen = Widgets.Button("Open File") #wopen.clicked.connect(self.open_file) wquit = Widgets.Button("Quit") wquit.add_callback('activated', lambda *args: self.quit()) for w in (wopen, wquit): hbox.add_widget(w, stretch=0) hbox.add_widget(Widgets.Label(''), stretch=1) vbox.add_widget(hbox, stretch=0) self.top.set_widget(vbox) self.top.set_title("Video Example Viewer")
def make_reduced_image(self, image): wd, ht = image.get_size()[:2] res = image.get_scaled_cutout_basic(0, 0, wd, ht, self.pct_reduce, self.pct_reduce) sm_img = RGBImage.RGBImage(data_np=res.data, order=image.order) return sm_img