Beispiel #1
0
        def convert_image(url, data, sizes, grayscale, removetrans, imgtype="jpg", background="#ffffff"):
            export = False
            img = Image.open(StringIO(data))

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

            if normalize_format_name(img.format) != imgtype:
                if img.mode == "P":
                    # convert pallete gifs to RGB so jpg save doesn't fail.
                    img = img.convert("RGB")
                export = True

            if removetrans and img.mode == "RGBA":
                background = Image.new("RGBA", img.size, background)
                # Paste the image on top of the background
                background.paste(img, img)
                img = background.convert("RGB")
                export = True

            if grayscale and img.mode != "L":
                img = img.convert("L")
                export = True

            if export:
                outsio = StringIO()
                img.save(outsio, convtype[imgtype])
                return (outsio.getvalue(), imgtype, imagetypes[imgtype])
            else:
                logger.debug("image used unchanged")
                return (data, imgtype, imagetypes[imgtype])
Beispiel #2
0
        def convert_image(url,
                          data,
                          sizes,
                          grayscale,
                          removetrans,
                          imgtype="jpg",
                          background='#ffffff'):
            export = False
            img = Image.open(StringIO(data))

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

            if normalize_format_name(img.format) != imgtype:
                if img.mode == "P":
                    # convert pallete gifs to RGB so jpg save doesn't fail.
                    img = img.convert("RGB")
                export = True

            if removetrans and img.mode == "RGBA":
                background = Image.new('RGBA', img.size, background)
                # Paste the image on top of the background
                background.paste(img, img)
                img = background.convert('RGB')
                export = True

            if grayscale and img.mode != "L":
                img = img.convert("L")
                export = True

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