File.py 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""
  Nome: File.py
  Autor: Alessandro dos Santos Ferreira ( santosferreira.alessandro@gmail.com )

  Descricão: TODO
"""

import cv2
#import numpy as np
import os
from skimage.util import img_as_float
from scipy import ndimage

class File(object):

    @staticmethod
    def get_filename(filepath):
        path, name = os.path.split(filepath)
        
        return name
    
    @staticmethod
    def get_path():
        path, name = os.path.split(filepath)
        
        return path    
        
    @staticmethod
    def open_image(filepath):
33
        image = cv2.imread(filepath)
34
            
35 36 37 38 39 40 41 42
        #image = np.flipud(image)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        
        #return img_as_float(image)
        return image

    @staticmethod
    def save_image(image, dataset, directory, filename, idx, ext = '.tif'):
43
        
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        for root, dirs, files in os.walk(dataset):
            for d in dirs:
                filepath = dataset + '/' + d + '/' + filename + '_%05d' % idx + ext
                if(os.path.isfile(filepath)):
                    os.remove(filepath)

        dir_path = dataset + '/' + directory
        filepath = dir_path + '/' + filename + '_%05d' % idx + ext
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        cv2.imwrite(filepath, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))

        return filepath

58
    @staticmethod
59 60 61
    def list_dirs(dataset):
        return [name for name in os.listdir(dataset) 
                    if os.path.isdir(os.path.join(dataset, name))]