Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

ImageMath module

TheImageMath module can be used to evaluate “image expressions”, thatcan take a number of images and generate a result.

ImageMath only supports single-layer images. To process multi-bandimages, use thesplit() method ormerge()function.

Example: Using theImageMath module

fromPILimportImage,ImageMathwithImage.open("image1.jpg")asim1:withImage.open("image2.jpg")asim2:out=ImageMath.lambda_eval(lambdaargs:args["convert"](args["min"](args["a"],args["b"]),'L'),a=im1,b=im2)out=ImageMath.unsafe_eval("convert(min(a, b), 'L')",a=im1,b=im2)
PIL.ImageMath.lambda_eval(expression,options,**kw)[source]

Returns the result of an image function.

Parameters:
  • expression – A function that receives a dictionary.

  • options – Values to add to the function’s dictionary. Note that the namesmust be valid Python identifiers. Deprecated.You can instead use one or more keyword arguments, asshown in the above example.

  • **kw – Values to add to the function’s dictionary, mapping image names toImage instances.

Returns:

An image, an integer value, a floating point value,or a pixel tuple, depending on the expression.

PIL.ImageMath.unsafe_eval(expression,options,**kw)[source]

Evaluates an image expression.

Danger

This uses Python’seval() function to process the expression string,and carries the security risks of doing so. It is notrecommended to process expressions without considering this.lambda_eval() is a more secure alternative.

ImageMath only supports single-layer images. To process multi-bandimages, use thesplit() method ormerge() function.

Parameters:
  • expression – A string which uses the standard Python expressionsyntax. In addition to the standard operators, you canalso use the functions described below.

  • options – Values to add to the evaluation context. Note that the names mustbe valid Python identifiers. Deprecated.You can instead use one or more keyword arguments, asshown in the above example.

  • **kw – Values to add to the evaluation context, mapping image names to Imageinstances.

Returns:

An image, an integer value, a floating point value,or a pixel tuple, depending on the expression.

Expression syntax

  • lambda_eval() expressions are functions that receive a dictionarycontaining images and operators.

  • unsafe_eval() expressions are standard Python expressions,but they’re evaluated in a non-standard environment.

Danger

unsafe_eval() uses Python’seval() function to process theexpression string, and carries the security risks of doing so.It is not recommended to process expressions without considering this.lambda_eval() is a more secure alternative.

Standard operators

You can use standard arithmetical operators for addition (+), subtraction (-),multiplication (*), and division (/).

The module also supports unary minus (-), modulo (%), and power (**) operators.

Note that all operations are done with 32-bit integers or 32-bit floatingpoint values, as necessary. For example, if you add two 8-bit images, theresult will be a 32-bit integer image. If you add a floating point constant toan 8-bit image, the result will be a 32-bit floating point image.

You can force conversion using theconvert(),float(), andint()functions described below.

Bitwise operators

The module also provides operations that operate on individual bits. Thisincludes and (&), or (|), and exclusive or (^). You can also invert (~) allpixel bits.

Note that the operands are converted to 32-bit signed integers before thebitwise operation is applied. This means that you’ll get negative values ifyou invert an ordinary grayscale image. You can use the and (&) operator tomask off unwanted bits.

Bitwise operators don’t work on floating point images.

Logical operators

Logical operators likeand,or, andnot workon entire images, rather than individual pixels.

An empty image (all pixels zero) is treated as false. All other images aretreated as true.

Note thatand andor return the last evaluated operand,while not always returns a boolean value.

Built-in functions

These functions are applied to each individual pixel.

abs(image)

Absolute value.

convert(image,mode)

Convert image to the given mode. The mode must be given as a stringconstant.

float(image)

Convert image to 32-bit floating point. This is equivalent toconvert(image, “F”).

int(image)

Convert image to 32-bit integer. This is equivalent to convert(image, “I”).

Note that 1-bit and 8-bit images are automatically converted to 32-bitintegers if necessary to get a correct result.

max(image1,image2)

Maximum value.

min(image1,image2)

Minimum value.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp