Skip to content

Commit 53b0569

Browse files
committed
Replace can_open() method with detect_format()
OpenSlide calls it detect_vendor(), but "format" makes more sense for ImageSlide.
1 parent 97607cf commit 53b0569

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
OpenSlide Python requires:
22

33
* Python 2 >= 2.6 or Python 3 >= 3.3
4-
* OpenSlide >= 3.3.0
4+
* OpenSlide >= 3.4.0
55
* Python Imaging Library or Pillow
66

77
To install:

openslide/__init__.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5656
return False
5757

5858
@classmethod
59-
def can_open(cls, filename):
60-
"""Return True if the specified file can be read."""
59+
def detect_format(cls, filename):
60+
"""Return a string describing the format of the specified file.
61+
62+
If the file format is not recognized, return None."""
6163
raise NotImplementedError
6264

6365
def close(self):
@@ -150,9 +152,11 @@ def __init__(self, filename):
150152
self._osr = lowlevel.open(filename)
151153

152154
@classmethod
153-
def can_open(cls, filename):
154-
"""Return True if OpenSlide can read the specified file."""
155-
return lowlevel.can_open(filename)
155+
def detect_format(cls, filename):
156+
"""Return a string describing the format vendor of the specified file.
157+
158+
If the file format is not recognized, return None."""
159+
return lowlevel.detect_vendor(filename)
156160

157161
def close(self):
158162
"""Close the OpenSlide object."""
@@ -267,13 +271,14 @@ def __init__(self, file):
267271
self._image = Image.open(file)
268272

269273
@classmethod
270-
def can_open(cls, filename):
271-
"""Return True if PIL can read the specified file."""
274+
def detect_format(cls, filename):
275+
"""Return a string describing the format of the specified file.
276+
277+
If the file format is not recognized, return None."""
272278
try:
273-
Image.open(filename)
274-
return True
279+
return Image.open(filename).format
275280
except IOError:
276-
return False
281+
return None
277282

278283
def close(self):
279284
"""Close the slide object."""
@@ -360,8 +365,8 @@ def open_slide(filename):
360365

361366
if __name__ == '__main__':
362367
import sys
363-
print("OpenSlide can open:", OpenSlide.can_open(sys.argv[1]))
364-
print("PIL can open:", ImageSlide.can_open(sys.argv[1]))
368+
print("OpenSlide vendor:", OpenSlide.detect_format(sys.argv[1]))
369+
print("PIL format:", ImageSlide.detect_format(sys.argv[1]))
365370
with open_slide(sys.argv[1]) as _slide:
366371
print("Dimensions:", _slide.dimensions)
367372
print("Levels:", _slide.level_count)

openslide/lowlevel.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ def _load_image(buf, size):
171171
return PIL.Image.frombuffer('RGBA', size, buf, 'raw', 'RGBa', 0, 1)
172172

173173
try:
174-
get_version = _func('openslide_get_version', c_char_p, [], _check_string)
174+
detect_vendor = _func('openslide_detect_vendor', c_char_p, [_utf8_p],
175+
_check_string)
175176
except AttributeError:
176-
raise OpenSlideError('OpenSlide >= 3.3.0 required')
177-
178-
can_open = _func('openslide_can_open', c_bool, [_utf8_p], None)
177+
raise OpenSlideError('OpenSlide >= 3.4.0 required')
179178

180179
open = _func('openslide_open', c_void_p, [_utf8_p], _check_open)
181180

@@ -240,3 +239,5 @@ def read_associated_image(slide, name):
240239
buf = (w * h * c_uint32)()
241240
_read_associated_image(slide, name, buf)
242241
return _load_image(buf, (w, h))
242+
243+
get_version = _func('openslide_get_version', c_char_p, [], _check_string)

0 commit comments

Comments
 (0)