@@ -56,8 +56,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):
56
56
return False
57
57
58
58
@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."""
61
63
raise NotImplementedError
62
64
63
65
def close (self ):
@@ -150,9 +152,11 @@ def __init__(self, filename):
150
152
self ._osr = lowlevel .open (filename )
151
153
152
154
@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 )
156
160
157
161
def close (self ):
158
162
"""Close the OpenSlide object."""
@@ -267,13 +271,14 @@ def __init__(self, file):
267
271
self ._image = Image .open (file )
268
272
269
273
@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."""
272
278
try :
273
- Image .open (filename )
274
- return True
279
+ return Image .open (filename ).format
275
280
except IOError :
276
- return False
281
+ return None
277
282
278
283
def close (self ):
279
284
"""Close the slide object."""
@@ -360,8 +365,8 @@ def open_slide(filename):
360
365
361
366
if __name__ == '__main__' :
362
367
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 ]))
365
370
with open_slide (sys .argv [1 ]) as _slide :
366
371
print ("Dimensions:" , _slide .dimensions )
367
372
print ("Levels:" , _slide .level_count )
0 commit comments