Pillow (Python Imaging Library Fork)
repository·main·Indexed 11 days ago
https://github.com/python-pillow/pillowA 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.
What's inside Pillow
- 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.
Overview of Pillow capabilities
mainPillow (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.
What is Raqm?
mainRaqm 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.
Use the PIL.ImageOps module for ready-made operations
mainThePIL.ImageOpsmodule provides a collection of 'ready-made' image processing operations. Note that this module is considered somewhat experimental, and most operators are primarily designed to work onL(grayscale) andRGBimages.Use the PIL.ImageCms module for color profile management
mainThePIL.ImageCmsmodule 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.Evaluate image expressions with PIL.ImageMath
mainThe
PIL.ImageMathmodule allows you to evaluate mathematical expressions that take one or more images as input and produce a result.Important Constraints:
- Single-layer only:
ImageMathonly supports single-layer images. To process multi-band (e.g., RGB) images, you must first useImage.Image.split()to separate the bands and then useImage.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 )- Single-layer only:
New features and performance improvements in 12.2.0
mainNew Capabilities
- JPEG2000 CMYK Support: You can now read JPEG2000 images that use CMYK palettes.
- EXIF FrameRate: The
FrameRateEXIF 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:
openis 2.3-15.6x faster andsaveis 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.
Use the PIL.ImageChops module for channel operations
mainThe
PIL.ImageChopsmodule 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
0toMAX(whereMAXis255for supported modes). - Most functions take one or two images as arguments and return a new image.
Use the PIL.PSDraw module for PostScript printing
mainThePIL.PSDrawmodule provides simple print support for PostScript printers. It allows you to output text, graphics, and images directly to a PostScript device. You can interact with this functionality through thePIL.PSDraw.PSDrawclass.Performance improvements in Pillow 12.3.0
mainPillow 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()ImageChopsoperations
Use the ImageDraw module for 2D graphics
mainThe
PIL.ImageDrawmodule provides simple 2D graphics capabilities forPIL.Image.Imageobjects. 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)Explore available image format plugins
mainPillow uses a plugin architecture to support various image file formats. Each format is handled by a specific module within the
PILpackage. You can access specialized functionality for specific formats by importing their respective plugin modules.Commonly used format plugins include:
PIL.JpegImagePluginfor JPEG filesPIL.PngImagePluginfor PNG filesPIL.GifImagePluginfor GIF filesPIL.WebPImagePluginfor WebP filesPIL.TiffImagePluginfor TIFF filesPIL.BmpImagePluginfor BMP filesPIL.PsdImagePluginfor 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.