Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BaseArray:
- def __init__(self, path):
- if type(path) == list:
- self.paths = path
- elif type(path) == str:
- self.generate_paths(path)
- elif type(path) == tuple:
- self.generate_paths_from_names(path[0], path[1], path[2])
- self.i = 0
- self.n = len(self.paths)
- def generate_paths_from_names(self, path, basename, i):
- npath, name, ext = get_pathname(basename)
- self.paths = []
- for n in range(i):
- respath = join(path, name + "_" + str(n) + ext)
- self.paths.append(respath)
- def __iter__(self):
- return self
- def __next__(self):
- if self.i < self.n:
- img = self.get_image(self.i)
- self.i += 1
- return img
- else:
- self.i = 0
- raise StopIteration()
- def __getitem__(self, i):
- return self.get_image(i)
- class ImageArray(BaseArray):
- def __init__(self, path):
- super(ImageArray, self).__init__(path)
- def generate_paths(self, path):
- # gather files in folder
- names = [get_pathname(join(path, f)) for f in listdir(path) if isfile(join(path, f))]
- self.paths = []
- for n in names:
- if n[2] in ['.jpg', '.png']:
- self.paths.append(join(n[0], n[1] + n[2]))
- def get_path_to_img(self, i):
- # check if there is an image already
- path, name, ext = get_pathname(self.paths[i])
- imgpath = join(path, name + ".png")
- if isfile(imgpath):
- return imgpath
- else:
- img = self.get_image(i)
- img.save(imgpath)
- return imgpath
- def get_image(self, i):
- path = self.paths[i]
- image = MyImage(path)
- return image
- def set_image(self, i):
- print("ERROR cant save with this class")
- class NpyImageArray(BaseArray):
- def __init__(self, path):
- super(NpyImageArray, self).__init__(path)
- def generate_paths(self, path):
- # gather files in folder
- names = [get_pathname(join(path, f)) for f in listdir(path) if isfile(join(path, f))]
- self.paths = []
- for n in names:
- if n[2] in ['.pickle']:
- self.paths.append(join(n[0], n[1] + n[2]))
- def get_path_to_img(self, i):
- # check if there is an image already
- path, name, ext = get_pathname(self.paths[i])
- imgpath = join(path, name + ".png")
- if isfile(imgpath):
- return imgpath
- else:
- img = self.get_image(i)
- img.save(imgpath)
- return imgpath
- def get_image(self, i):
- with open(self.paths[i], 'rb') as f:
- data = np.load(f)
- return MyImage(data)
- def set_image(self, i, image):
- with open(self.paths[i], 'wb') as f:
- np.save(f, image.data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement