Exemple #1
0
def saveWithOptions(img, output, extension, extraInfo=None):
    """
    @summary: Gets a dictionary with options for extension.
    @param img: PIL.Image to save.
    @param output: outfile where store img
    @param extension: Extension of the output file.   
    """
    try:
        if (extension == "JPEG"):
            img.save(output, extension,
                     quality=jpegOptions["quality"])
            if (extraInfo != None):
                __log__.debug("It is a JPEG image. It will set EXIF information...")
                ImageUtils.setJpegInfo(output, extraInfo)
        else:
            img.save(output, extension)
    except Exception, e:
        __log__.error("It could not save %s. %s" (output, e))
Exemple #2
0
    def __calculateSize__ (self, srcScale, size):
        """
        @summary: Calculate size of the dictionary.
        @param srcScale: Scale of the size.
        @param size: Size of a image.
        """
        __log__.debug("Scale: %s" % srcScale)
        if (srcScale == ImageUtils.PERCENT):
            self.__lastSizes__[ImageUtils.PERCENT] = size
            self.__lastSizes__[ImageUtils.PIXEL] = (
                self.__srcSize__[0] * (float(size[0]) / 100),
                self.__srcSize__[1] * (float(size[1]) / 100))
            self.__lastSizes__[ImageUtils.CM] = ImageUtils.pixelToCm(
                self.__lastSizes__[ImageUtils.PIXEL],
                self.__srcDpi__)
        elif (srcScale == ImageUtils.PIXEL):
            self.__lastSizes__[ImageUtils.PIXEL] = size
            self.__lastSizes__[ImageUtils.CM] = ImageUtils.pixelToCm(
                size,
                self.__srcDpi__)

            if (self.__srcSize__[0] != 0):
                self.__lastSizes__[ImageUtils.PERCENT] = (
                    (float(size[0]) / self.__srcSize__[0]) * 100,
                    (float(size[1]) / self.__srcSize__[1]) * 100)
            else:
                __log__.debug("Source size is 0. It can not possible calculate other scales.")
                self.__lastSizes__[ImageUtils.PERCENT] = (0, 0)

        elif (srcScale == ImageUtils.CM):
            self.__lastSizes__[ImageUtils.CM] = size
            self.__lastSizes__[ImageUtils.PIXEL] = ImageUtils.cmToPixel(
                size,
                self.__srcDpi__)

            if (self.__srcSize__[0] != 0):
                self.__lastSizes__[ImageUtils.PERCENT] = (
                    (float(size[0]) / self.__srcSize__[0]) * 100,
                    (float(size[1]) / self.__srcSize__[1]) * 100)
            else:
                __log__.debug("Source size is 0. It can not possible calculate other scales.")
                self.__lastSizes__[ImageUtils.PERCENT] = (0, 0)
        else:
            __log__.error("Scale unknown")
Exemple #3
0
    def setData(self, width, height, unit, srcSize=None):
        """
        @summary: Set data to dialog.
        @param with: With of a image.
        @param height: Height of a image.
        @param unit: Sacale of the width and height.
        @param srcSize: Tuple with width and height in pixels.
        @raise TypeError: Raise when height, width or unit are not int or float. 
        """
        if (not isinstance(width, float) and not isinstance(width, int)):
            sMessage = "Width must be float or integer number"
            __log__.error(sMessage)
            raise TypeError(sMessage)
        if (not isinstance(height, float) and not isinstance(height, int)):
            sMessage = "Height must be float or integer number"
            __log__.error(sMessage)
            raise TypeError(sMessage)
        if (not isinstance(unit, int)):
            sMessage = "Unit must be PIXEL(0) or CM(1)or PERCENT(2)"
            __log__.error(sMessage)
            raise TypeError(sMessage)
        elif ((unit < ImageUtils.PIXEL) or (unit > ImageUtils.PERCENT)) :
            sMessage = "Unit must be PIXEL(0), CM(1) or PERCENT(2)"
            __log__.error(sMessage)
            raise TypeError(sMessage)

        radio = None
        if (unit == ImageUtils.PIXEL):
            radio = self.__rbPixel__
            if (srcSize != None):
                self.__srcSize__ = srcSize
            else:
                self.__srcSize__ = (float(width), float(height))
            self.__setActiveRadios__(True)
        elif (unit == ImageUtils.CM):
            radio = self.__rbCM__
            if (srcSize != None):
                self.__srcSize__ = srcSize
            else:
                self.__srcSize__ = ImageUtils.cmToPixel(
                    (float(width), float(height)),
                    self.__srcDpi__)
            self.__setActiveRadios__(True)
        elif (unit == ImageUtils.PERCENT):
            
            radio = self.__rbPercentage__
            self.__setActiveRadios__(True)
        else:
            __log__.error("Scale unknown")

        if (radio != None):
            radio.set_active(True)
        
        self.__printSize__((float(width), float(height)))
        self.__calculateSize__(unit, (float(width), float(height)))
        self.__setConditionsSpins__(unit)
Exemple #4
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:
Exemple #5
0
def openWithExtraInfo(path, extension):
    """
    @summary: Gets a pointer and extra info from path.
    @param path: Path of image.
    @param extension: Extension of image.
    @return: Tuple within (handler of image, extraInfo)  
    """
    extraInfo = None
    try:
        img2do = Image.open(path)
    except IOError:
        __log__.error("An error has occurred when it was trying to open %s. Skip operation." % fileop)
        img2do = None
        
    if (extension == "JPEG"):
        extraInfo = ImageUtils.getJpegInfo(path)
        
    return (img2do, extraInfo)
Exemple #6
0
    def do(self, imgobj, path=None):
        """
        @summary: Do a resizing.
        @param imgobj: File on path will be used as input file.
        @param path: Path of imgobj.
        @return: PIL.Image object as result of operation.
        @raise Exception: Raise when it can not resize image. 
        """            
        scale = self.__args__["scale"]
        __log__.debug("Get scale: %d" % scale)
        
        if (scale == ImageUtils.CM):
            size = ImageUtils.cmToPixel((self.__args__["width"],
                                         self.__args__["height"]), DEFAULT_DPI)
        elif (scale == ImageUtils.PIXEL):
            size = (int(self.__args__["width"]), int(self.__args__["height"]))
        elif (scale == ImageUtils.PERCENT):
            srcSize = imgobj.size
            size = (int(srcSize[0] * (float(self.__args__["width"]) / 100)),
                    int(srcSize[1] * (float(self.__args__["height"]) / 100)))
        else:
            __log__.error("Scale has non-valid value. %d" % scale)
            raise ValueError("Scale has non-valid value. %d" % scale)

        __log__.debug("Get new size: %d x %d" % size)

        try:
            imgobj = imgobj.resize(size, self.__args__["filter"])
            if (path != None):
                __log__.info("%s Resized." % path)
            else:
                __log__.debug("Resized")
        except Exception, e:
            if (path != None):
                __log__.error("An error has occurred when it was resizing %s. %s" % (path, e))
            else:
                __log__.error("An error has occurred when it was resizing. %s", e)