Skip to content

Commit

Permalink
Use local import of pyvips in case it isn't set up
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsmkn committed Jan 23, 2025
1 parent 6bfd2c2 commit 73de7ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Install vips and openslide
run: sudo apt-get update && sudo apt-get install -yq libvips-dev
run: sudo apt-get update && sudo apt-get install -yq libvips-dev libopenslide-dev
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
18 changes: 5 additions & 13 deletions panimg/image_builders/tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
from panimg.image_builders.dicom import get_dicom_headers_by_study
from panimg.models import MAXIMUM_SEGMENTS_LENGTH, ColorSpace, TIFFImage

try:
import pyvips
except OSError:
pyvips = False


DICOM_WSI_STORAGE_ID = "1.2.840.10008.5.1.4.1.1.77.1.6"


Expand Down Expand Up @@ -253,10 +247,13 @@ def _load_with_tiff(
def _load_with_openslide(
*, gc_file: GrandChallengeTiffFile
) -> GrandChallengeTiffFile:
import pyvips

open_slide_file = pyvips.Image.openslideload(str(gc_file.path.absolute()))
gc_file = _extract_openslide_properties(
gc_file=gc_file, image=open_slide_file
)

return gc_file


Expand Down Expand Up @@ -345,6 +342,8 @@ def _convert(


def _convert_to_tiff(*, path: Path, pk: UUID, output_directory: Path) -> Path:
import pyvips

new_file_name = output_directory / path.name / f"{pk}.tif"
new_file_name.parent.mkdir()

Expand Down Expand Up @@ -510,13 +509,6 @@ def image_builder_tiff( # noqa: C901
*, files: set[Path]
) -> Iterator[TIFFImage]:

if pyvips is False:
raise ImportError(
f"Could not import pyvips, which is required for the "
f"{__name__} image builder. Either ensure that libvips-dev "
f"is installed or remove {__name__} from your list of builders."
)

file_errors: DefaultDict[Path, list[str]] = defaultdict(list)

with TemporaryDirectory() as output_directory:
Expand Down
15 changes: 2 additions & 13 deletions panimg/post_processors/tiff_to_dzi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@
from panimg.models import ImageType, PanImgFile, PostProcessorResult
from panimg.settings import DZI_TILE_SIZE

try:
import pyvips
except OSError:
pyvips = False

logger = logging.getLogger(__name__)


def tiff_to_dzi(*, image_files: set[PanImgFile]) -> PostProcessorResult:
if pyvips is False:
raise ImportError(
f"Could not import pyvips, which is required for the "
f"{__name__} post processor. Either ensure that libvips-dev "
f"is installed or remove {__name__} from your list of post "
f"processors."
)

new_image_files: set[PanImgFile] = set()

for file in image_files:
Expand All @@ -36,6 +23,8 @@ def tiff_to_dzi(*, image_files: set[PanImgFile]) -> PostProcessorResult:


def _create_dzi_image(*, tiff_file: PanImgFile) -> PostProcessorResult:
import pyvips

# Creates a dzi file and corresponding tiles in directory {pk}_files
dzi_output = tiff_file.file.parent / str(tiff_file.image_id)

Expand Down

0 comments on commit 73de7ca

Please sign in to comment.