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

655 remove thumbnail writing #675

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions src/dodal/devices/areadetector/plugins/MJPG.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from PIL import Image, ImageDraw

from dodal.devices.oav.oav_parameters import OAVConfigParams
from dodal.devices.oav.utils import save_thumbnail
from dodal.log import LOGGER


Expand Down Expand Up @@ -50,9 +49,6 @@ def _save_image(self, image: Image.Image):

LOGGER.info(f"Saving image to {path}")
image.save(path)

save_thumbnail(Path(path), image)

self.last_saved_path.put(path)

def trigger(self):
Expand Down
16 changes: 1 addition & 15 deletions src/dodal/devices/oav/utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from enum import IntEnum
from pathlib import Path
from typing import Generator, Tuple

import bluesky.plan_stubs as bps
import numpy as np
from bluesky.utils import Msg
from PIL.Image import Image

from dodal.devices.oav.oav_calculations import camera_coordinates_to_xyz
from dodal.devices.oav.oav_parameters import OAVConfigParams
from dodal.devices.oav.oav_detector import OAVConfigParams
from dodal.devices.oav.pin_image_recognition import PinTipDetection
from dodal.devices.smargon import Smargon
from dodal.log import LOGGER

Pixel = Tuple[int, int]

Expand Down Expand Up @@ -110,14 +107,3 @@ def wait_for_tip_to_be_found(
raise PinNotFoundException(f"No pin found after {timeout} seconds")

return found_tip # type: ignore


def save_thumbnail(full_file_path: Path, full_image: Image, new_height=192):
"""Scales an image down to have the height specified in new_height and saves it
to the same location as the full image with a t appended to the filename"""
thumbnail_path = full_file_path.with_stem(full_file_path.stem + "t")
LOGGER.info(f"Saving thumbnail to {thumbnail_path}")
full_size = full_image.size
new_width = (new_height / full_size[1]) * full_size[0]
full_image.thumbnail((new_width, new_height))
full_image.save(thumbnail_path.as_posix())
7 changes: 1 addition & 6 deletions src/dodal/devices/webcam.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import io
from pathlib import Path

import aiofiles
from aiohttp import ClientSession
from bluesky.protocols import Triggerable
from ophyd_async.core import AsyncStatus, StandardReadable, soft_signal_rw
from PIL import Image

from dodal.devices.oav.utils import save_thumbnail
from dodal.log import LOGGER


Expand All @@ -26,10 +23,8 @@ async def _write_image(self, file_path: str):
async with session.get(self.url) as response:
response.raise_for_status()
LOGGER.info(f"Saving webcam image from {self.url} to {file_path}")
data = await response.read()
async with aiofiles.open(file_path, "wb") as file:
await file.write(data)
save_thumbnail(Path(file_path), Image.open(io.BytesIO(data)))
await file.write((await response.read()))

@AsyncStatus.wrap
async def trigger(self) -> None:
Expand Down
15 changes: 0 additions & 15 deletions tests/devices/unit_tests/oav/test_oav_utils.py

This file was deleted.

7 changes: 1 addition & 6 deletions tests/devices/unit_tests/test_oav.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@ def test_snapshot_trigger_saves_to_correct_file(
st.wait()
expected_calls_to_save = [
call(f"test directory/test filename{addition}.png")
for addition in [
"",
"t",
"_outer_overlay",
"_grid_overlay",
]
for addition in ["", "_outer_overlay", "_grid_overlay"]
]
calls_to_save = mock_save.mock_calls
assert calls_to_save == expected_calls_to_save
Expand Down
14 changes: 4 additions & 10 deletions tests/devices/unit_tests/test_webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ async def test_given_last_saved_path_when_device_read_then_returns_path(webcam:
)
@patch("dodal.devices.webcam.aiofiles", autospec=True)
@patch("dodal.devices.webcam.ClientSession.get", autospec=True)
@patch("dodal.devices.webcam.Image", autospec=True)
async def test_given_filename_and_directory_when_trigger_and_read_then_returns_expected_path(
mock_image,
mock_get: MagicMock,
mock_aiofiles,
directory,
filename,
expected_path,
webcam: Webcam,
):
mock_get.return_value.__aenter__.return_value = (mock_response := AsyncMock())
mock_response.read.return_value = b"TEST"
mock_get.return_value.__aenter__.return_value = AsyncMock()
await webcam.filename.set(filename)
await webcam.directory.set(directory)
await webcam.trigger()
Expand All @@ -49,12 +46,11 @@ async def test_given_filename_and_directory_when_trigger_and_read_then_returns_e

@patch("dodal.devices.webcam.aiofiles", autospec=True)
@patch("dodal.devices.webcam.ClientSession.get", autospec=True)
@patch("dodal.devices.webcam.Image", autospec=True)
async def test_given_data_returned_from_url_when_trigger_then_data_written(
mock_image, mock_get: MagicMock, mock_aiofiles, webcam: Webcam
mock_get: MagicMock, mock_aiofiles, webcam: Webcam
):
mock_get.return_value.__aenter__.return_value = (mock_response := AsyncMock())
mock_response.read.return_value = (test_web_data := b"TEST")
mock_response.read.return_value = (test_web_data := "TEST")
mock_open = mock_aiofiles.open
mock_open.return_value.__aenter__.return_value = (mock_file := AsyncMock())
await webcam.filename.set("file")
Expand All @@ -66,9 +62,8 @@ async def test_given_data_returned_from_url_when_trigger_then_data_written(

@patch("dodal.devices.webcam.aiofiles", autospec=True)
@patch("dodal.devices.webcam.ClientSession.get", autospec=True)
@patch("dodal.devices.webcam.Image", autospec=True)
async def test_given_response_throws_exception_when_trigger_then_exception_rasied(
mock_image, mock_get: MagicMock, mock_aiofiles, webcam: Webcam
mock_get: MagicMock, mock_aiofiles, webcam: Webcam
):
class MyException(Exception):
pass
Expand All @@ -77,7 +72,6 @@ def _raise():
raise MyException()

mock_get.return_value.__aenter__.return_value = (mock_response := AsyncMock())
mock_response.read.return_value = b"TEST"
mock_response.raise_for_status = _raise
await webcam.filename.set("file")
await webcam.directory.set("/tmp")
Expand Down
Loading