diff --git a/simple_picameraGUI.py b/simple_picameraGUI.py index bd627d6..746cfad 100644 --- a/simple_picameraGUI.py +++ b/simple_picameraGUI.py @@ -24,11 +24,12 @@ def __init__(self, **kw) -> None: self.minsize(410, 300) self.title("sPiCameraGUI") + # For scalebar self.lens_zoom = StringVar(self, "5X") self.scale_unit = StringVar(self, "um") self.fixed_scalebar_len = 50 self.scalebar_len = self.fixed_scalebar_len - self.physical_len = DoubleVar(self, 100.0) + self.physical_len = DoubleVar(self, 100.0) # um self.scale_bar_font_size = 10 self.save_dir = os.path.join(homedir, "PiCamCapture", "") @@ -43,22 +44,7 @@ def __init__(self, **kw) -> None: self._camera_init() self._set_camera_preview_size() - # default bars_per_um_per_unit_zoom calculate for initialization - scale_unit_um = 1 - current_zoom = int(self.lens_zoom.get()[:-1]) - self.bars_per_um_per_unit_zoom = self.scalebar_len / ( - self.physical_len.get() * scale_unit_um * current_zoom - ) # physical len in um - - # try loading calib data from calib.json else create initial file - try: - with open(self.save_dir + "calib.json", "r") as f: - self.bars_per_um_per_unit_zoom = json.load(f) - except: - self.calib_data = self.bars_per_um_per_unit_zoom - with open(self.save_dir + "calib.json", "w") as f: - json.dump(self.calib_data, f, indent=2) - + self._load_calib_data() self._update_fixed_scalebar() # self.bind("", self._hide_input_window) @@ -74,6 +60,25 @@ def _camera_init(self): self.quit() exit() + def _load_calib_data(self): + # try loading calib data from calib.json else create initial file + try: + with open(self.save_dir + "calib.json", "r") as f: + self.bars_per_um_per_unit_zoom = json.load(f) + print("calibration data loaded.") + except FileNotFoundError: + messagebox.showwarning( + "Calibration error", "Scalebar calibration data not found please recalibrate." + ) + print( + "calibration file 'calib.json' not found in save dir.\nprocceding with default calibration.." + ) + # default bars_per_um_per_unit_zoom calculate for initialization + self.bars_per_um_per_unit_zoom = self.scalebar_len / ( + self.physical_len.get() * int(self.lens_zoom.get()[:-1]) + ) # physical len in um + self.calib_data = self.bars_per_um_per_unit_zoom + def create_frames(self): self.window = Frame(self.master) self.screen_width, self.screen_height = ( @@ -211,17 +216,21 @@ def _calibration_frame(self): self.btn_OK.grid(row=0, column=9, padx=5) def _recalculate_scale(self, *event): - self.calib_data = self.bars_per_um_per_unit_zoom - with open(self.save_dir + "calib.json", "w") as f: - json.dump(self.calib_data, f, indent=2) - scale_unit_um = 1 if self.scale_unit.get() == "mm": scale_unit_um = 1000 # 1mm = 1000um + current_zoom = int(self.lens_zoom.get()[:-1]) self.bars_per_um_per_unit_zoom = self.scalebar_len / ( self.physical_len.get() * scale_unit_um * current_zoom ) # physical len in um + + # Save new calibration data to calib.json + self.calib_data = self.bars_per_um_per_unit_zoom + with open(self.save_dir + "calib.json", "w") as f: + json.dump(self.calib_data, f, indent=2) + print("calibration data saved!", self.calib_data) + self._update_fixed_scalebar() def _update_fixed_scalebar(self):