Skip to content

Commit

Permalink
examples/deepzoom: load OpenSlide DLL from OPENSLIDE_PATH on Windows
Browse files Browse the repository at this point in the history
We can't read the path from a command-line option because WSGI apps
aren't necessarily invoked as __main__.
  • Loading branch information
bgilbert committed Apr 3, 2021
1 parent bb4c9b6 commit 57cca2d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Install Python tools
run: |
python -m pip install --upgrade pip
pip install pytest wheel
pip install flask pytest wheel
- name: Install OpenSlide
run: |
case "${{ matrix.python-arch }}" in
Expand All @@ -87,6 +87,12 @@ jobs:
- name: Run tests
# Reads OPENSLIDE_PATH
run: pytest -v
- name: Test library loading in examples
# Reads OPENSLIDE_PATH
run: |
python examples/deepzoom/deepzoom_multiserver.py -h >/dev/null
python examples/deepzoom/deepzoom_server.py -h >/dev/null
python examples/deepzoom/deepzoom_tile.py -h >/dev/null
- name: Archive wheel
uses: actions/upload-artifact@v2
with:
Expand Down
21 changes: 18 additions & 3 deletions examples/deepzoom/deepzoom_multiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,28 @@
from collections import OrderedDict
from flask import Flask, abort, make_response, render_template, url_for
from io import BytesIO
import openslide
from openslide import OpenSlide, OpenSlideError
from openslide.deepzoom import DeepZoomGenerator
import os
from optparse import OptionParser
from threading import Lock

if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
if hasattr(os, 'add_dll_directory'):
# Python >= 3.8
with os.add_dll_directory(_dll_path):
import openslide
else:
# Python < 3.8
_orig_path = os.environ.get('PATH', '')
os.environ['PATH'] = _orig_path + ';' + _dll_path
import openslide
os.environ['PATH'] = _orig_path
else:
import openslide
from openslide import OpenSlide, OpenSlideError
from openslide.deepzoom import DeepZoomGenerator

SLIDE_DIR = '.'
SLIDE_CACHE_SIZE = 10
DEEPZOOM_FORMAT = 'jpeg'
Expand Down
22 changes: 19 additions & 3 deletions examples/deepzoom/deepzoom_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@

from flask import Flask, abort, make_response, render_template, url_for
from io import BytesIO
import openslide
from openslide import ImageSlide, open_slide
from openslide.deepzoom import DeepZoomGenerator
from optparse import OptionParser
import os
import re
from unicodedata import normalize

if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
if hasattr(os, 'add_dll_directory'):
# Python >= 3.8
with os.add_dll_directory(_dll_path):
import openslide
else:
# Python < 3.8
_orig_path = os.environ.get('PATH', '')
os.environ['PATH'] = _orig_path + ';' + _dll_path
import openslide
os.environ['PATH'] = _orig_path
else:
import openslide
from openslide import ImageSlide, open_slide
from openslide.deepzoom import DeepZoomGenerator

DEEPZOOM_SLIDE = None
DEEPZOOM_FORMAT = 'jpeg'
DEEPZOOM_TILE_SIZE = 254
Expand Down
21 changes: 18 additions & 3 deletions examples/deepzoom/deepzoom_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@

import json
from multiprocessing import Process, JoinableQueue
import openslide
from openslide import open_slide, ImageSlide
from openslide.deepzoom import DeepZoomGenerator
from optparse import OptionParser
import os
import re
import shutil
import sys
from unicodedata import normalize

if os.name == 'nt':
_dll_path = os.getenv('OPENSLIDE_PATH')
if _dll_path is not None:
if hasattr(os, 'add_dll_directory'):
# Python >= 3.8
with os.add_dll_directory(_dll_path):
import openslide
else:
# Python < 3.8
_orig_path = os.environ.get('PATH', '')
os.environ['PATH'] = _orig_path + ';' + _dll_path
import openslide
os.environ['PATH'] = _orig_path
else:
import openslide
from openslide import open_slide, ImageSlide
from openslide.deepzoom import DeepZoomGenerator

VIEWER_SLIDE_NAME = 'slide'

class TileWorker(Process):
Expand Down

0 comments on commit 57cca2d

Please sign in to comment.