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

Use "Rotate Image" operation in the Live Viewer #2016

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 18 additions & 0 deletions mantidimaging/gui/windows/live_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from pathlib import Path
from typing import TYPE_CHECKING
from logging import getLogger
import numpy as np

from imagecodecs._deflate import DeflateError
from tifffile import tifffile, TiffFileError
from astropy.io import fits

from mantidimaging.gui.mvp_base import BasePresenter
from mantidimaging.gui.windows.live_viewer.model import LiveViewerWindowModel, Image_Data
from mantidimaging.core.operations.rotate_stack import RotateFilter
from mantidimaging.core.data import ImageStack

if TYPE_CHECKING:
from mantidimaging.gui.windows.live_viewer.view import LiveViewerWindowView # pragma: no cover
Expand Down Expand Up @@ -84,6 +87,8 @@ def load_and_display_image(self, image_path: Path):
elif image_path.suffix.lower() == ".fits":
with fits.open(image_path.__str__()) as fit:
image_data = fit[0].data

image_data = self.rotate_image(image_data)
except (IOError, KeyError, ValueError, TiffFileError, DeflateError) as error:
message = f"{type(error).__name__} reading image: {image_path}: {error}"
logger.error(message)
Expand All @@ -106,3 +111,16 @@ def update_image_modified(self, image_path: Path):
"""
if self.selected_image and image_path == self.selected_image.image_path:
self.load_and_display_image(image_path)

def rotate_image(self, image_data):
ang = self.view.image_rotation_angle
image_data_shape = image_data.shape
image_data_temp = np.zeros(shape=(1, image_data_shape[0], image_data_shape[1]))
image_data_temp[0] = image_data
image_stack_temp = ImageStack(image_data_temp)
rotated_imaged = RotateFilter().filter_func(image_stack_temp, angle=ang)
return rotated_imaged.data[0].astype(int)

def update_image_operation(self):
""" Reload the current image if an operation has been performed on the current image"""
self.load_and_display_image(self.selected_image.image_path)
18 changes: 18 additions & 0 deletions mantidimaging/gui/windows/live_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from PyQt5.QtCore import QSignalBlocker
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.Qt import QAction, QActionGroup

from mantidimaging.gui.mvp_base import BaseMainWindowView
from .live_view_widget import LiveViewWidget
Expand Down Expand Up @@ -34,6 +35,18 @@ def __init__(self, main_window: 'MainWindowView', live_dir_path: Path) -> None:
self.live_viewer = LiveViewWidget()
self.imageLayout.addWidget(self.live_viewer)
self.live_viewer.z_slider.valueChanged.connect(self.presenter.select_image)
self.image_rotation_angle = 0
self.right_click_menu = self.live_viewer.image.vb.menu
rotate_menu = self.right_click_menu.addMenu("Rotate Image")
self.rotate_angles_group = QActionGroup(self)
allowed_angles = [0, 90, 180, 270]
for angle in allowed_angles:
action = QAction(str(angle) + "°", self.rotate_angles_group)
action.setCheckable(True)
rotate_menu.addAction(action)
action.triggered.connect(self.set_image_rotation_angle)
if angle == 0:
action.setChecked(True)

def show(self) -> None:
"""Show the window"""
Expand Down Expand Up @@ -74,3 +87,8 @@ def closeEvent(self, e) -> None:
self.live_viewer.handle_deleted()
super().closeEvent(e)
self.presenter = None # type: ignore # View instance to be destroyed -type can be inconsistent

def set_image_rotation_angle(self):
"""Set the image rotation angle which will be read in by the presenter"""
self.image_rotation_angle = int(self.rotate_angles_group.checkedAction().text().replace('°', ''))
self.presenter.update_image_operation()
Loading