HOME/Articles/

pil example pil images (snippet)

Article Outline

Python pil example 'pil images'

python pil images

Python pil example: pil images

from weasyprint(import images as weasyprint_images)

# Only monkey-patch if GDK failed to import
if not hasattr(weasyprint_images, 'PixbufLoader'):
    from io import BytesIO
    try:
        from PIL import Image
    except ImportError:
        # Yeah, PIL packaging.
        import Image

    def pil_loader(file_obj, string):
        if string is None:
            # PIL wants to seek, but file_obj could be eg. a socket.
            string = file_obj.read()
        file_obj = BytesIO(string)
        png_file = BytesIO()
        Image.open(file_obj).save(png_file, 'PNG')
        png_file.seek(0)
        return weasyprint_images.cairo_png_loader(png_file, None)

    weasyprint_images.gdkpixbuf_loader = pil_loader