Advertisement
Pella86

Untitled

Aug 3rd, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. class BaseArray:
  2.    
  3.     def __init__(self, path):
  4.         if type(path) == list:
  5.             self.paths = path
  6.         elif type(path) == str:
  7.             self.generate_paths(path)
  8.         elif type(path) == tuple:
  9.             self.generate_paths_from_names(path[0], path[1], path[2])
  10.         self.i = 0
  11.         self.n = len(self.paths)
  12.  
  13.     def generate_paths_from_names(self, path, basename, i):
  14.         npath, name, ext = get_pathname(basename)
  15.        
  16.         self.paths = []
  17.         for n in range(i):
  18.             respath = join(path, name + "_" + str(n) + ext)
  19.             self.paths.append(respath)
  20.    
  21.     def __iter__(self):
  22.         return self
  23.  
  24.     def __next__(self):
  25.         if self.i < self.n:
  26.             img = self.get_image(self.i)
  27.             self.i += 1
  28.             return img
  29.         else:
  30.             self.i = 0
  31.             raise StopIteration()  
  32.    
  33.     def __getitem__(self, i):
  34.         return self.get_image(i)
  35.  
  36.  
  37.  
  38. class ImageArray(BaseArray):
  39.  
  40.     def __init__(self, path):
  41.         super(ImageArray, self).__init__(path)
  42.    
  43.     def generate_paths(self, path):
  44.         # gather files in folder
  45.         names = [get_pathname(join(path, f)) for f in listdir(path) if isfile(join(path, f))]
  46.        
  47.         self.paths = []
  48.         for n in names:
  49.             if n[2] in ['.jpg', '.png']:
  50.                 self.paths.append(join(n[0], n[1] + n[2]))
  51.  
  52.     def get_path_to_img(self, i):
  53.          # check if there is an image already
  54.          path, name, ext = get_pathname(self.paths[i])
  55.          imgpath = join(path, name + ".png")
  56.          
  57.          if isfile(imgpath):
  58.              return imgpath
  59.          else:
  60.              img = self.get_image(i)
  61.              img.save(imgpath)
  62.              return imgpath
  63.  
  64.     def get_image(self, i):
  65.         path = self.paths[i]
  66.         image = MyImage(path)
  67.         return image
  68.    
  69.     def set_image(self, i):
  70.         print("ERROR cant save with this class")
  71.  
  72.  
  73.  
  74. class NpyImageArray(BaseArray):
  75.    
  76.     def __init__(self, path):
  77.         super(NpyImageArray, self).__init__(path)
  78.    
  79.        
  80.     def generate_paths(self, path):
  81.         # gather files in folder
  82.         names = [get_pathname(join(path, f)) for f in listdir(path) if isfile(join(path, f))]
  83.        
  84.         self.paths = []
  85.         for n in names:
  86.             if n[2] in ['.pickle']:
  87.                 self.paths.append(join(n[0], n[1] + n[2]))
  88.    
  89.     def get_path_to_img(self, i):
  90.          # check if there is an image already
  91.          path, name, ext = get_pathname(self.paths[i])
  92.          imgpath = join(path, name + ".png")
  93.          
  94.          if isfile(imgpath):
  95.              return imgpath
  96.          else:
  97.              img = self.get_image(i)
  98.              img.save(imgpath)
  99.              return imgpath
  100.  
  101.     def get_image(self, i):
  102.         with open(self.paths[i], 'rb') as f:
  103.             data = np.load(f)
  104.         return MyImage(data)
  105.    
  106.     def set_image(self, i, image):
  107.         with open(self.paths[i], 'wb') as f:
  108.             np.save(f, image.data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement