Pillow (Python Imaging Library Fork)

repository·main·Indexed 11 days ago

https://github.com/python-pillow/pillow

A powerful fork of the Python Imaging Library (PIL) that provides extensive image processing capabilities and file format support for Python applications. It features an efficient internal representation of image data and is optimized for fast access to basic pixel formats.

Tokens
92.6K
Snippets
330
Records
597
Agent score
96%

What's inside Pillow

  1. Overview of Pillow

    main
    Pillow is a friendly fork of the Python Imaging Library (PIL) that adds image processing capabilities to the Python interpreter. It provides extensive file format support, an efficient internal representation of image data, and powerful image processing capabilities. The core library is optimized for fast access to data stored in basic pixel formats, serving as a foundation for general image processing tasks.
  2. Overview of Pillow capabilities

    main

    Pillow (the Python Imaging Library fork) provides image processing capabilities for Python. It is designed for fast access to data in basic pixel formats and supports:

    • Image Archival & Batch Processing: Creating thumbnails, converting between file formats, and printing images.
    • Image Display: Integration with GUI toolkits (like Tk via PIL.ImageTk) and a built-in method for quick debugging.
    • Image Processing: Point operations, convolution filtering, color space conversions, resizing, rotation, and affine transforms.
    • Statistical Analysis: Extracting image statistics via histograms for tasks like automatic contrast enhancement.
  3. What is Raqm?

    main

    Raqm is a library designed to encapsulate complex text layout logic. It provides a convenient API for handling:

    • Bidirectional text support: Using FriBiDi or SheenBidi.
    • Shaping: Using HarfBuzz.
    • Script itemization: Enabling support for most writing systems covered by Unicode.

    It is used as a dependency in several major projects, including Pillow, ImageMagick, and LibGD.

  4. Use the PIL.ImageCms module for color profile management

    main
    The PIL.ImageCms module provides color profile management support using the LittleCMS2 color management engine. It allows you to manage color profiles, perform color transformations between different profiles, and handle color intent and direction during transformations.
  5. Evaluate image expressions with PIL.ImageMath

    main

    The PIL.ImageMath module allows you to evaluate mathematical expressions that take one or more images as input and produce a result.

    Important Constraints:

    • Single-layer only: ImageMath only supports single-layer images. To process multi-band (e.g., RGB) images, you must first use Image.Image.split() to separate the bands and then use Image.merge() to combine the results.
    • Data Types: Operations are performed using 32-bit integers or 32-bit floating-point values. For example, adding two 8-bit images results in a 32-bit integer image.
    from PIL import Image, ImageMath
    
    with Image.open("image1.jpg") as im1:
        with Image.open("image2.jpg") as im2:
            # Example using lambda_eval
            out = ImageMath.lambda_eval(
              lambda args: args["convert"](args["min"](args["a"], args["b"]), 'L'),
              a=im1,
              b=im2
            )
            # Example using unsafe_eval
            out = ImageMath.unsafe_eval(
              "convert(min(a, b), 'L')",
              a=im1,
              b=im2
            )
  6. New features and performance improvements in 12.2.0

    main

    New Capabilities

    • JPEG2000 CMYK Support: You can now read JPEG2000 images that use CMYK palettes.
    • EXIF FrameRate: The FrameRate EXIF tag has been added.

    Performance and Thread Safety

    • Lazy Plugin Loading: Pillow now lazily loads only the required plugin based on file extension. This significantly improves performance: open is 2.3-15.6x faster and save is 2.2-9x faster for common formats.
    • Free-threaded Python Support: Critical sections are used to protect FreeType font objects, improving thread safety for the free-threaded build of Python.
  7. Use the PIL.ImageChops module for channel operations

    main

    The PIL.ImageChops module provides arithmetical image operations, known as "channel operations" or "chops". These are used for special effects, image compositions, and algorithmic painting.

    Key constraints:

    • Most operations are currently only implemented for 8-bit images (e.g., modes "L" and "RGB").
    • Results are typically clipped to the range 0 to MAX (where MAX is 255 for supported modes).
    • Most functions take one or two images as arguments and return a new image.
  8. Performance improvements in Pillow 12.3.0

    main

    Pillow 12.3.0 introduces optimizations for C-based image manipulation. Performance for the following operations can be improved by up to 5.6 times:

    • Image.alpha_composite()
    • Image.fill()
    • Image.filter()
    • Image.getchannel()
    • Image.linear_gradient()
    • Image.matrix()
    • Image.merge()
    • Image.negative()
    • Image.putalpha()
    • Image.quantize()
    • Image.radial_gradient()
    • Image.resample()
    • Image.split()
    • ImageChops operations
  9. Use the ImageDraw module for 2D graphics

    main

    The PIL.ImageDraw module provides simple 2D graphics capabilities for PIL.Image.Image objects. It allows you to create new images, annotate or retouch existing images, and generate graphics on the fly. Note that drawing operations modify the image in place.

    from PIL import Image, ImageDraw
    
    with Image.open("example.jpg") as im:
        draw = ImageDraw.Draw(im)
        draw.line((0, 0, im.size[0], im.size[1]), fill=128)
  10. Explore available image format plugins

    main

    Pillow uses a plugin architecture to support various image file formats. Each format is handled by a specific module within the PIL package. You can access specialized functionality for specific formats by importing their respective plugin modules.

    Commonly used format plugins include:

    • PIL.JpegImagePlugin for JPEG files
    • PIL.PngImagePlugin for PNG files
    • PIL.GifImagePlugin for GIF files
    • PIL.WebPImagePlugin for WebP files
    • PIL.TiffImagePlugin for TIFF files
    • PIL.BmpImagePlugin for BMP files
    • PIL.PsdImagePlugin for Photoshop files

    Note that many of these plugins are automatically loaded when you use PIL.Image.open(), but you can import them directly if you need to access format-specific classes or constants.