2323from tifffile import imsave as saveImage
2424import subprocess
2525import numpy as np
26-
26+
27+
2728def openImage (im ):
2829 ext = os .path .splitext (im )[1 ].lower ()
2930 try :
@@ -56,6 +57,7 @@ def openImage(im):
5657 rv = None
5758 return rv
5859
60+
5961class LoadImage (object ):
6062 '''
6163 provide methods to filter files and load images
@@ -77,9 +79,9 @@ def __init__(self, p):
7779 def flipImage (self , pic ):
7880 '''
7981 flip image if configured in config
80-
82+
8183 :param pic: 2d array, image array
82-
84+
8385 :return: 2d array, flipped image array
8486 '''
8587 if self .fliphorizontal :
@@ -92,9 +94,9 @@ def loadImage(self, filename):
9294 '''
9395 load image file, if failed (for example loading an incomplete file),
9496 then it will keep trying loading file for 5s
95-
97+
9698 :param filename: str, image file name
97-
99+
98100 :return: 2d ndarray, 2d image array (flipped)
99101 '''
100102 if os .path .exists (filename ):
@@ -106,7 +108,10 @@ def loadImage(self, filename):
106108 i = 0
107109 while i < 10 :
108110 try :
109- image = openImage (filenamefull )
111+ if os .path .splitext (filenamefull )[- 1 ] == '.npy' :
112+ image = np .load (filenamefull )
113+ else :
114+ image = openImage (filenamefull )
110115 i = 10
111116 except :
112117 i = i + 1
@@ -118,33 +123,34 @@ def loadImage(self, filename):
118123 def genFileList (self , filenames = None , opendir = None , includepattern = None , excludepattern = None , fullpath = False ):
119124 '''
120125 generate the list of file in opendir according to include/exclude pattern
121-
126+
122127 :param filenames: list of str, list of file name patterns, all files match ANY pattern in this list will be included
123128 :param opendir: str, the directory to get files
124129 :param includepattern: list of str, list of wildcard of files that will be loaded,
125130 all files match ALL patterns in this list will be included
126131 :param excludepattern: list of str, list of wildcard of files that will be blocked,
127132 any files match ANY patterns in this list will be blocked
128133 :param fullpath: bool, if true, return the full path of each file
129-
134+
130135 :return: list of str, a list of filenames
131136 '''
132-
133- fileset = self .genFileSet (filenames , opendir , includepattern , excludepattern , fullpath )
137+
138+ fileset = self .genFileSet (
139+ filenames , opendir , includepattern , excludepattern , fullpath )
134140 return sorted (list (fileset ))
135141
136142 def genFileSet (self , filenames = None , opendir = None , includepattern = None , excludepattern = None , fullpath = False ):
137143 '''
138144 generate the list of file in opendir according to include/exclude pattern
139-
145+
140146 :param filenames: list of str, list of file name patterns, all files match ANY pattern in this list will be included
141147 :param opendir: str, the directory to get files
142148 :param includepattern: list of str, list of wildcard of files that will be loaded,
143149 all files match ALL patterns in this list will be included
144150 :param excludepattern: list of str, list of wildcard of files that will be blocked,
145151 any files match ANY patterns in this list will be blocked
146152 :param fullpath: bool, if true, return the full path of each file
147-
153+
148154 :return: set of str, a list of filenames
149155 '''
150156 filenames = self .filenames if filenames == None else filenames
@@ -165,6 +171,7 @@ def genFileSet(self, filenames=None, opendir=None, includepattern=None, excludep
165171 fileset1 |= set (fnmatch .filter (fileset , filename ))
166172 fileset = fileset1
167173 if fullpath :
168- filelist = map (lambda x : os .path .abspath (os .path .join (opendir , x )), fileset )
174+ filelist = map (
175+ lambda x : os .path .abspath (os .path .join (opendir , x )), fileset )
169176 fileset = set (filelist )
170177 return fileset
0 commit comments