def snap_and_get_bleach_location(exposure, cutoff):
    p_exposure = projector_device.get_exposure()
    c_exposure = mmc.get_exposure()
    test_img = mm.live().snap(True).get(0)
    test_np_img = np.reshape(test_img.get_raw_pixels(), newshape=[test_img.get_height(), test_img.get_width()])
    location = central_pixel_without_cells(test_np_img)
    if location:
        auto_shutter = mm.shutter().get_auto_shutter()
        mm.shutter().set_auto_shutter(False)
        projector.set_exposure(projector_device, exposure)
        mmc.set_exposure(exposure)
        projector.enable_point_and_shoot_mode(True)
        pre_img = mm.live().snap(True).get(0)
        pre_np_img = np.reshape(pre_img.get_raw_pixels(), newshape=[pre_img.get_height(), pre_img.get_width()])
        projector.add_point_to_point_and_shoot_queue(int(location[1]), int(location[0]))
        post_img = mm.live().snap(True).get(0)
        post_np_img = np.reshape(post_img.get_raw_pixels(), newshape=[post_img.get_height(), post_img.get_width()])
        measured_location = bleach_location(pre_np_img, post_np_img, location, [100, 100])
        offset = (measured_location[0] - location[0], measured_location[1] - location[1])
        print(offset)
        if offset[0] * offset[0] + offset[1] * offset[1] > cutoff:
            projector.calibrate(True)
        projector.set_exposure(projector_device, p_exposure)
        mmc.set_exposure(c_exposure)
        mm.shutter().set_auto_shutter(auto_shutter)
        return offset[0] * offset[0] + offset[1] * offset[1]
    return -1
def snap_and_get_bleach_location(exposure, cutoff):
    """
    Takes an image with the current settings.  Finds a location close to the center where there are
    no objects (as defined in function central_picel_without_cells).  If no such location is found, returns -1.
    Targets bleacher to this location, exposes and takes an image of that exposure.  Finds the center of the
    actual bleach spot.  When the square of the distance between the intended target and the actual bleach spot is
    greater than provided offset, will execute a full calibration.
    :param exposure: exposure time to use for bleaching
    :param cutoff: square of distance.  When offset is higher than this code should execute a calibration
    :return: tuple with first Boolean indicating if a calibration took place, second variable the square of the offset distance
    """
    p_exposure = projector_device.get_exposure()
    c_exposure = mmc.get_exposure()
    test_img = mm.live().snap(True).get(0)
    test_np_img = np.reshape(
        test_img.get_raw_pixels(),
        newshape=[test_img.get_height(),
                  test_img.get_width()])
    location = central_pixel_without_cells(test_np_img)
    if location:
        auto_shutter = mm.shutter().get_auto_shutter()
        mm.shutter().set_auto_shutter(False)
        projector.set_exposure(projector_device, exposure)
        mmc.set_exposure(exposure)
        projector.enable_point_and_shoot_mode(True)
        pre_img = mm.live().snap(True).get(0)
        pre_np_img = np.reshape(
            pre_img.get_raw_pixels(),
            newshape=[pre_img.get_height(),
                      pre_img.get_width()])
        projector.add_point_to_point_and_shoot_queue(int(location[1]),
                                                     int(location[0]))
        post_img = mm.live().snap(True).get(0)
        post_np_img = np.reshape(
            post_img.get_raw_pixels(),
            newshape=[post_img.get_height(),
                      post_img.get_width()])
        measured_location = bleach_location(pre_np_img, post_np_img, location,
                                            [100, 100])
        offset = (measured_location[0] - location[0],
                  measured_location[1] - location[1])
        print(offset)
        cal = False
        if offset[0] * offset[0] + offset[1] * offset[1] > cutoff:
            projector.calibrate(True)
            cal = True
            print("Calibrated")
        projector.set_exposure(projector_device, p_exposure)
        mmc.set_exposure(c_exposure)
        mm.shutter().set_auto_shutter(auto_shutter)
        return cal, offset[0] * offset[0] + offset[1] * offset[1]
    return False, -1
half_roi_size = (100, 100)

pre = "C://Users//NicoLocal//Images//Jess//20201217//Pre"
post = "C://Users//NicoLocal//Images//Jess//20201217//Post"

pre_data = mm.data().load_data(pre, False)
post_data = mm.data().load_data(post, False)

cb = mm.data().get_coords_builder()
cb.t(0).p(0).c(0).z(0)

pre_img = pre_data.get_image(cb.build())
post_img = post_data.get_image(cb.build())

pre_pixels = np.reshape(pre_img.get_raw_pixels(),
                        newshape=[pre_img.get_height(),
                                  pre_img.get_width()])
post_pixels = np.reshape(
    post_img.get_raw_pixels(),
    newshape=[post_img.get_height(),
              post_img.get_width()])
bleach_location = bleach_location(pre_pixels, post_pixels, expected_position,
                                  half_roi_size)

print(bleach_location)
offset = [
    bleach_location[0] - expected_position[0],
    bleach_location[1] - expected_position[1]
]
print(offset)
if location:
    auto_shutter = mm.shutter().get_auto_shutter()
    mm.shutter().set_auto_shutter(False)
    projector.set_exposure(projector_device, exposure)
    mmc.set_exposure(exposure)
    projector.enable_point_and_shoot_mode(True)
    pre_img = mm.live().snap(True).get(0)
    pre_np_img = np.reshape(
        pre_img.get_raw_pixels(),
        newshape=[pre_img.get_height(),
                  pre_img.get_width()])
    projector.add_point_to_point_and_shoot_queue(int(location[1]),
                                                 int(location[0]))
    post_img = mm.live().snap(True).get(0)
    post_np_img = np.reshape(
        post_img.get_raw_pixels(),
        newshape=[post_img.get_height(),
                  post_img.get_width()])
    measured_location = bleach_location(pre_np_img, post_np_img, location,
                                        [100, 100])
    offset = (measured_location[0] - location[0],
              measured_location[1] - location[1])
    projector.set_exposure(projector_device, p_exposure)
    mmc.set_exposure(c_exposure)
    mm.shutter().set_auto_shutter(auto_shutter)
    print(location)
    print(measured_location)
    print(offset)
else:
    print("No location found to project bleach spot")