Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(DiamondLightSource/dodal#751) Draw crosshairs with better contrast #787

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/dodal/devices/areadetector/plugins/MJPG.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class SnapshotWithBeamCentre(MJPG):
image and saves the image to disk."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could: Would be good to mention the outline in the docstring


CROSSHAIR_LENGTH_PX = 20
CROSSHAIR_COLOUR = "Blue"
CROSSHAIR_OUTLINE_COLOUR = "Black"
CROSSHAIR_FILL_COLOUR = "White"

def post_processing(self, image: Image.Image):
assert (
Expand All @@ -100,15 +101,27 @@ def post_processing(self, image: Image.Image):
beam_x = self.oav_params.beam_centre_i
beam_y = self.oav_params.beam_centre_j

SnapshotWithBeamCentre.draw_crosshair(image, beam_x, beam_y)

self._save_image(image)

@classmethod
def draw_crosshair(cls, image: Image.Image, beam_x: int, beam_y: int):
draw = ImageDraw.Draw(image)
HALF_LEN = self.CROSSHAIR_LENGTH_PX / 2
HALF_LEN = cls.CROSSHAIR_LENGTH_PX / 2
draw.rectangle(
[beam_x - 1, beam_y - HALF_LEN - 1, beam_x + 1, beam_y + HALF_LEN + 1],
fill=cls.CROSSHAIR_OUTLINE_COLOUR,
)
draw.rectangle(
[beam_x - HALF_LEN - 1, beam_y - 1, beam_x + HALF_LEN + 1, beam_y + 1],
fill=cls.CROSSHAIR_OUTLINE_COLOUR,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should: The 1 in here is the width of the outline right? Might be worth pulling into a constant for readability?

draw.line(
((beam_x, beam_y - HALF_LEN), (beam_x, beam_y + HALF_LEN)),
fill=self.CROSSHAIR_COLOUR,
fill=cls.CROSSHAIR_FILL_COLOUR,
)
draw.line(
((beam_x - HALF_LEN, beam_y), (beam_x + HALF_LEN, beam_y)),
fill=self.CROSSHAIR_COLOUR,
fill=cls.CROSSHAIR_FILL_COLOUR,
)

self._save_image(image)
12 changes: 12 additions & 0 deletions tests/devices/unit_tests/areadetector/plugins/test_MJPG.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pathlib import Path
from unittest.mock import MagicMock, patch

from ophyd.sim import instantiate_fake_device
from PIL import Image

from dodal.devices.areadetector.plugins.MJPG import SnapshotWithBeamCentre
from dodal.devices.oav.oav_detector import OAVConfigParams
Expand Down Expand Up @@ -31,3 +33,13 @@ def test_given_snapshot_triggered_then_crosshair_drawn(
status.wait()

assert len(patch_line.mock_calls) == 2


def test_snapshot_draws_expected_crosshair(tmp_path: Path):
image = Image.open("tests/test_data/test_images/oav_snapshot_test.png")
SnapshotWithBeamCentre.draw_crosshair(image, 510, 380)
image.save(tmp_path / "output_image.png")
expected_image = Image.open("tests/test_data/test_images/oav_snapshot_expected.png")
image_bytes = image.tobytes()
expected_bytes = expected_image.tobytes()
assert image_bytes == expected_bytes, "Actual and expected images differ"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading