Skip to content

Commit

Permalink
Merge pull request #380 from girder/openjpeg-source
Browse files Browse the repository at this point in the history
Add an openjpeg source using the glymur library.
  • Loading branch information
manthey authored Oct 30, 2019
2 parents 4e228d5 + 484cc87 commit 137372f
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 41 deletions.
7 changes: 5 additions & 2 deletions .circleci/release_pypi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ twine upload --verbose dist/*
cd "$ROOTPATH/sources/mapnik"
python setup.py sdist
twine upload --verbose dist/*
cd "$ROOTPATH/sources/openslide"
cd "$ROOTPATH/sources/ometiff"
python setup.py sdist
twine upload --verbose dist/*
cd "$ROOTPATH/sources/ometiff"
cd "$ROOTPATH/sources/openjpeg"
python setup.py sdist
twine upload --verbose dist/*
cd "$ROOTPATH/sources/openslide"
python setup.py sdist
twine upload --verbose dist/*
cd "$ROOTPATH/sources/pil"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var LeafletImageViewerWidget = ImageViewerWidget.extend({
},

render: function () {
var errmsg;
// If script or metadata isn't loaded, then abort
if (!window.L || !this.tileWidth || !this.tileHeight || this.deleted) {
return this;
Expand All @@ -36,7 +37,15 @@ var LeafletImageViewerWidget = ImageViewerWidget.extend({
}

if (this.tileWidth !== this.tileHeight) {
console.error('The Leaflet viewer only supports square tiles.');
errmsg = 'The Leaflet viewer only supports square tiles.';
}
if (this.tileWidth > 256) {
errmsg = 'The Leaflet viewer does not support tiles wider than 256 pixels.';
}
if (errmsg) {
this.viewer = $('<div/>').text(errmsg);
this.$el.append(this.viewer);
console.error(errmsg);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var SlideAtlasImageViewerWidget = ImageViewerWidget.extend({
},

render: function () {
var errmsg;
// If script or metadata isn't loaded, then abort
if (!window.SA || !this.tileWidth || !this.tileHeight || this.deleted) {
return this;
Expand All @@ -36,7 +37,15 @@ var SlideAtlasImageViewerWidget = ImageViewerWidget.extend({
}

if (this.tileWidth !== this.tileHeight) {
console.error('The SlideAtlas viewer only supports square tiles.');
errmsg = 'The SlideAtlas viewer only supports square tiles.';
}
if (this.tileWidth > 256) {
errmsg = 'The SlideAtlas viewer does not support tiles wider than 256 pixels.';
}
if (errmsg) {
this.viewer = $('<div/>').text(errmsg);
this.$el.append(this.viewer);
console.error(errmsg);
return this;
}

Expand Down
5 changes: 3 additions & 2 deletions large_image/tilesource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pkg_resources import iter_entry_points

from .base import TileSource, FileTileSource, TileOutputMimeTypes, \
TILE_FORMAT_IMAGE, TILE_FORMAT_PIL, TILE_FORMAT_NUMPY, nearPowerOfTwo
TILE_FORMAT_IMAGE, TILE_FORMAT_PIL, TILE_FORMAT_NUMPY, nearPowerOfTwo, \
etreeToDict
from ..exceptions import TileGeneralException, TileSourceException, TileSourceAssetstoreException
from .. import config
from ..constants import SourcePriority
Expand Down Expand Up @@ -82,5 +83,5 @@ def getTileSource(*args, **kwargs):
'TileSource', 'FileTileSource',
'exceptions', 'TileGeneralException', 'TileSourceException', 'TileSourceAssetstoreException',
'TileOutputMimeTypes', 'TILE_FORMAT_IMAGE', 'TILE_FORMAT_PIL', 'TILE_FORMAT_NUMPY',
'AvailableTileSources', 'getTileSource', 'nearPowerOfTwo',
'AvailableTileSources', 'getTileSource', 'nearPowerOfTwo', 'etreeToDict',
]
35 changes: 33 additions & 2 deletions large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import PIL.ImageColor
import PIL.ImageDraw
import six
from collections import defaultdict
from six import BytesIO

from ..cache_util import getTileCache, strhash, methodcache
Expand Down Expand Up @@ -95,6 +96,37 @@ def _letterboxImage(image, width, height, fill):
return result


def etreeToDict(t):
"""
Convert an xml etree to a nested dictionary without schema names in the
keys.
@param t: an etree.
@returns: a python dictionary with the results.
"""
# Remove schema
tag = t.tag.split('}', 1)[1] if t.tag.startswith('{') else t.tag
d = {tag: {}}
children = list(t)
if children:
entries = defaultdict(list)
for entry in map(etreeToDict, children):
for k, v in six.iteritems(entry):
entries[k].append(v)
d = {tag: {k: v[0] if len(v) == 1 else v
for k, v in six.iteritems(entries)}}

if t.attrib:
d[tag].update({(k.split('}', 1)[1] if k.startswith('{') else k): v
for k, v in six.iteritems(t.attrib)})
text = (t.text or '').strip()
if text and len(d[tag]):
d[tag]['text'] = text
elif text:
d[tag] = text
return d


def nearPowerOfTwo(val1, val2, tolerance=0.02):
"""
Check if two values are different by nearly a power of two.
Expand Down Expand Up @@ -979,8 +1011,7 @@ def _pilFormatMatches(self, image, match=True, **kwargs):
# compatibility could be an issue.
return False

def _outputTile(self, tile, tileEncoding, x, y, z, pilImageAllowed=False,
**kwargs):
def _outputTile(self, tile, tileEncoding, x, y, z, pilImageAllowed=False, **kwargs):
"""
Convert a tile from a PIL image or image in memory to the desired
encoding.
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ girder>=3.0.3
girder-jobs>=3.0.3
-e sources/dummy
-e sources/mapnik
-e sources/openjpeg
-e sources/openslide
-e sources/pil
-e sources/test
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'dummy': ['large-image-source-dummy'],
'mapnik': ['large-image-source-mapnik'],
'ometiff': ['large-image-source-ometiff'],
'openjpeg': ['large-image-source-openjpeg'],
'openslide': ['large-image-source-openslide'],
'pil': ['large-image-source-pil'],
'tiff': ['large-image-source-tiff'],
Expand Down
Loading

0 comments on commit 137372f

Please sign in to comment.