22
22
'''
23
23
24
24
from __future__ import print_function , division , absolute_import , unicode_literals
25
- from pathlib import Path
25
+ from glob import glob
26
26
27
27
import base64
28
28
import copy
@@ -1219,7 +1219,8 @@ class Image(CASDataMsgHandler):
1219
1219
behavior should be similar to that of the image.loadImages_ CAS action for loading
1220
1220
server-side images:
1221
1221
1222
- .. _image.loadImages: https://go.documentation.sas.com/doc/en/pgmsascdc/v_028/casactml/casactml_image_details22.htm # noqa: E501
1222
+ .. _image.loadImages: https://go.documentation.sas.com/doc/en/pgmsascdc/v_028/casactml/casactml_image_details22
1223
+ .htm # noqa: E501
1223
1224
1224
1225
Although images will be stored in binary format to a CAS table column
1225
1226
labeled "_image_", the CAS table metadata will not indicate that this column should
@@ -1237,8 +1238,12 @@ class Image(CASDataMsgHandler):
1237
1238
"""
1238
1239
1239
1240
def __init__ (self , data , nrecs = 1000 , subdirs = True ):
1240
- if isinstance (data , (str , Path )):
1241
- path = Path (data )
1241
+
1242
+ # To maintain Py2.7 compatibility, use strings instead of Paths.
1243
+ if type (data ).__module__ == 'pathlib' :
1244
+ data = str (data )
1245
+
1246
+ if isinstance (data , str ):
1242
1247
files = []
1243
1248
1244
1249
# Search for all images in the directory and (optionally) in subdirectories
@@ -1247,9 +1252,11 @@ def __init__(self, data, nrecs=1000, subdirs=True):
1247
1252
'tif' , 'tiff' , 'webp' ):
1248
1253
1249
1254
if subdirs :
1250
- files . extend ( path .glob ( f '**/*. { extension } ' ) )
1255
+ pattern = os . path .join ( data , '**' , '*.%s' % extension )
1251
1256
else :
1252
- files .extend (path .glob (f'*.{ extension } ' ))
1257
+ pattern = os .path .join (data , '*.%s' % extension )
1258
+
1259
+ files .extend (glob (pattern , recursive = subdirs ))
1253
1260
self ._data = files
1254
1261
else :
1255
1262
self ._data = list (data )
@@ -1286,14 +1293,18 @@ def getrow(self, row):
1286
1293
1287
1294
record = self ._data [row ]
1288
1295
1296
+ # Convert Path instances to str for Py2.7 compatibility.
1297
+ if type (record ).__module__ == 'pathlib' :
1298
+ record = str (record )
1299
+
1289
1300
# Default value. Will be overridden if disk location is known.
1290
1301
path = 'Image_%d.png' % (row + 1 )
1291
1302
1292
1303
# Input is path to an image on disk. Can just read bytes directly.
1293
- if isinstance (record , ( str , Path ) ):
1304
+ if isinstance (record , str ):
1294
1305
with open (record , 'rb' ) as f :
1295
1306
image = f .read ()
1296
- path = str ( record )
1307
+ path = record
1297
1308
else :
1298
1309
# Otherwise, PIL package is required to format data as an image.
1299
1310
if PIL is None :
0 commit comments