Пример #1
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)
Пример #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")
Пример #3
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)