Example #1
0
    def doOperations(self, projectType, action=ACTION_COPY, tempFolder=None):
        """
        @summary: Do operations on image.
        @param projectType: Instance of IProyectType.
        @param action: Action that it will do with input file. Copy=ACTION_COPY or Move=ACTION_MOVE
        @param tempFolder: Folder where actions will do.
        @note: If tempFolder is not set, Operations will do on input file.
        @see: pycamimg.core.projectType.IProjectType    
        """
        meta = None
        __log__.info("Doing operations on %s" % self.__path__)
        fileop = self.__path__
        if tempFolder != None:
            __log__.debug("Temporal folder is specified. %s" % tempFolder)
            head, tail = os.path.split(self.__path__)
            tempFile = os.path.join(tempFolder, tail)
            fileop = tempFile

            shutil.copy2(self.__path__, tempFile)
            __log__.debug("Copy file from %s to %s" % (self.__path__, tempFile))

            if not projectType.PreDoItem(fileop):
                __log__.error("An error was occurred when it was doing PreDoItem on %s" % fileop)
                os.remove(fileop)
                return None

        if self.__operations__ != None:
            if len(self.__operations__) > 0:
                ext = string.lower(os.path.splitext(fileop)[1])
                ext = Image.EXTENSION[ext]

                # Open image to do operations over image object
                img2do = None
                extraInfo = None
                try:
                    img2do, extraInfo = CamFormatOptions.openWithExtraInfo(fileop, ext)
                except Exception, ex:
                    __log__.error(
                        "An error has occurred when it was trying to open %s. Skip operation. %s" % (fileop, ex)
                    )
                    img2do = None
                    extraInfo = None

                if img2do != None:
                    for key, op in self.__operations__.iteritems():
                        img2do = op.do(img2do, path=fileop)

                    try:
                        CamFormatOptions.saveWithOptions(img2do, fileop, ext)
                    except IOError, ioe:
                        __log__.error("It could not save %s. Please check your permissions. %s" % (fileop, ioe))
                        raise IOError("It could not save %s. Please check your permissions. %s" % (fileop, ioe))
                    except Exception, ex:
                        __log__.error("It could not save %s. Please check your permissions. %s" % (fileop, ex))
                    finally:
Example #2
0
    def doOnPath(self, path):
        """
        @summary: Do operation on path.
        @param path: File on path will be used as input file.
        @note: Generic operation will always do nothing. 
               Its child classes should overwrite this method.  
        """
        
        
        # Open image to do operations over image object
        try:
            img2do = Image.open(path)
        except IOError:
            __log__.error("An error has occurred when it was trying to open %s. Skip operation." % path)
            img2do = None

        if (img2do != None):
            # HACKME: Try to remove dependency with jpeg
            infoExif = None
            infoExif2 = None
            infoComments = None
            
            infoExif, infoExif2, infoComments = ImageUtils.getJpegInfo(path)
            
            self.do(img2do)
            
            try:
                ext = string.lower(os.path.splitext(path)[1])
                ext = Image.EXTENSION[ext]
                CamFormatOptions.saveWithOptions(img2do, path, ext)
                
                if (ext == "JPEG"):
                    __log__.debug("It is a JPEG image. It will set EXIF information...")
                    ImageUtils.setJpegInfo(path, (infoExif, infoExif2, infoComments))
            except IOError, ioe:
                __log__.error("It could not save %s. Please check your permissions. %s" % (path, ioe))
                raise IOError("It could not save %s. Please check your permissions. %s" % (path, ioe))
            finally: