Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pillow ImageOps Module

Pillow ImageOps Module

ImageOps is a utility module in the Python Pillow image processing library that provides a series of predefined image processing operations. These operations are usually wrappers and simplifications of PIL.Image module functions, allowing developers to implement common image processing needs with more concise code.


Main features of the ImageOps module

  1. Simplified operations: Encapsulate complex image processing workflows into a single function call
  2. Standardization: Provide standardized image processing methods
  3. Utility functions: Include a variety of practical image processing functions
  4. Complementary to PIL.Image: Complements the functions of the PIL.Image module

Common methods of the ImageOps module

The following are the most commonly used methods in the ImageOps module and their functional descriptions:

Image adjustment methods

Method Name Parameters Return Value Description
autocontrast(image, cutoff=0, ignore=None) image: Input image
 cutoff: 裁剪百分比(0-100)       

 ignore: 忽略的背景色 | 新图像对象 | 自动调整图像对比度,使图像中最暗的像素变为黑色,最亮的像素变为白色 |

| colorize(image, black, white) | image: Grayscale image
black: Black replacement color
white: White replacement color | New image object | Colorize a grayscale image, specifying which colors black and white map to respectively | | equalize(image, mask=None) | image: Input image
mask: Optional mask | New image object | Perform histogram equalization on the image to enhance contrast | | mirror(image) | image: Input image | New image object | Flip image horizontally (mirror effect) |

Image transformation methods

Method Name Parameters Return Value Description
expand(image, border, fill=0) image: Input image
 border: 边框宽度       

 fill: 填充颜色 | 新图像对象 | 为图像添加边框 |

| fit(image, size, method=0, bleed=0.0, centering=(0.5, 0.5)) | image: Input image
size: Target size
method: Resizing method
bleed: Crop ratio
centering: Center point | New image object | Resize image to specified size while maintaining aspect ratio | | flip(image) | image: Input image | New image object | Flip image vertically | | scale(image, factor, resample=3) | image: Input image
factor: Scale factor
resample: Resampling method | New image object | Scale image by ratio |

Image special effect methods

Method Name Parameters Return Value Description
posterize(image, bits) image: Input image
bits: Number of bits to keep (1-8) New image object Reduce the number of bits per color channel in the image, producing a posterization effect
solarize(image, threshold=128) image: Input image
threshold: Threshold (0-255) New image object Invert all pixel values above the threshold, producing an overexposure effect
invert(image) image: Input image New image object Invert image colors (negative effect)

Usage examples

Basic Usage Example

Example

from

PIL

import

Image

,

ImageOps

Open the image file

image

=

Image.

open

(

"example.jpg"

)

Automatically adjust contrast

adjusted_image

=

ImageOps.

autocontrast

(

image

)

Save the processed image

adjusted_image.

save

(

"adjusted_example.jpg"

)

More practical examples

Example

from

PIL

import

Image

,

ImageOps

Create a negative of the image

image

=

Image.

open

(

"photo.jpg"

)

inverted_image

=

ImageOps.

invert

(

image

)

inverted_image.

save

(

"inverted_photo.jpg"

)

Add a border to the image

bordered_image

=

ImageOps.

expand

(

image

,

border

=

50

,

fill

=

"white"

)

bordered_image.

save

(

"bordered_photo.jpg"

)

Image colorization (first convert image to grayscale)

gray_image

=

ImageOps.

grayscale

(

image

)

colored_image

=

ImageOps.

colorize

(

gray_image

,

black

=

"blue"

,

white

=

"yellow"

)

colored_image.

save

(

"colored_photo.jpg"

)


Notes

  1. Image mode: Some ImageOps methods have specific requirements for image mode, such as colorize() requires a grayscale image
  2. Performance considerations: When processing large images, some operations may consume a lot of memory
  3. Parameter range: Pay attention to the valid range of parameters, such as posterize() the bits parameter of must be between 1 and 8
  4. Original protection: ImageOps methods usually return a new image object; the original image is not modified

Summary

The ImageOps module is a very practical toolset in the Pillow library that simplifies many common image processing tasks. By mastering these methods, developers can quickly implement various image processing effects without writing complex code. For scenarios requiring more advanced image processing, other features of the PIL.Image module can be used in combination.