Ejemplo n.º 1
0
    def convert_image(url, data, sizes, grayscale, removetrans, imgtype="jpg", background="#ffffff"):
        export = False
        img = Image()
        img.load(data)

        owidth, oheight = img.size
        nwidth, nheight = sizes
        scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)

        if normalize_format_name(img.format) == "gif" and GifInfo(StringIO(data), CHECK_IS_ANIMATED).frameCount > 1:
            raise exceptions.RejectImage("Animated gifs come out purely--not going to use it.")

        if scaled:
            img.size = (nwidth, nheight)
            export = True

        if normalize_format_name(img.format) != imgtype:
            export = True

        if removetrans and img.has_transparent_pixels():
            canvas = Image()
            canvas.create_canvas(int(img.size[0]), int(img.size[1]), unicode(background))
            canvas.compose(img)
            img = canvas
            export = True

        if grayscale and img.type != "GrayscaleType":
            img.type = "GrayscaleType"
            export = True

        if export:
            return (img.export(convtype[imgtype]), imgtype, imagetypes[imgtype])
        else:
            logger.debug("image used unchanged")
            return (data, imgtype, imagetypes[imgtype])
Ejemplo n.º 2
0
    def convert_image(url,data,sizes,grayscale,
                      removetrans,imgtype="jpg",background='#ffffff'):
        export = False
        img = Image()
        img.load(data)

        owidth, oheight = img.size
        nwidth, nheight = sizes
        scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)
        if scaled:
            img.size = (nwidth, nheight)
            export = True

        if normalize_format_name(img.format) != imgtype:
            export = True

        if removetrans and img.has_transparent_pixels():
            canvas = Image()
            canvas.create_canvas(int(img.size[0]), int(img.size[1]), str(background))
            canvas.compose(img)
            img = canvas
            export = True

        if grayscale and img.type != "GrayscaleType":
            img.type = "GrayscaleType"
            export = True

        if export:
            return (img.export(convtype[imgtype]),imgtype,imagetypes[imgtype])
        else:
            logger.debug("image used unchanged")
            return (data,imgtype,imagetypes[imgtype])
Ejemplo n.º 3
0
    def convert_image(url,
                      data,
                      sizes,
                      grayscale,
                      removetrans,
                      imgtype="jpg",
                      background='#ffffff'):
        export = False
        img = Image()
        img.load(data)

        owidth, oheight = img.size
        nwidth, nheight = sizes
        scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)

        if normalize_format_name(img.format) == "gif" and GifInfo(
                StringIO(data), CHECK_IS_ANIMATED).frameCount > 1:
            raise exceptions.RejectImage(
                "Animated gifs come out purely--not going to use it.")

        if scaled:
            img.size = (nwidth, nheight)
            export = True

        if normalize_format_name(img.format) != imgtype:
            export = True

        if removetrans and img.has_transparent_pixels():
            canvas = Image()
            canvas.create_canvas(int(img.size[0]), int(img.size[1]),
                                 unicode(background))
            canvas.compose(img)
            img = canvas
            export = True

        if grayscale and img.type != "GrayscaleType":
            img.type = "GrayscaleType"
            export = True

        if export:
            return (img.export(convtype[imgtype]), imgtype,
                    imagetypes[imgtype])
        else:
            logger.debug("image used unchanged")
            return (data, imgtype, imagetypes[imgtype])