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

ImageOps module

TheImageOps module contains a number of ‘ready-made’ imageprocessing operations. This module is somewhat experimental, and most operatorsonly work on L and RGB images.

Added in version 1.1.3.

PIL.ImageOps.autocontrast(image:Image,cutoff:float|tuple[float,float]=0,ignore:int|Sequence[int]|None=None,mask:Image|None=None,preserve_tone:bool=False)Image[source]

Maximize (normalize) image contrast. This function calculates ahistogram of the input image (or mask region), removescutoff percent of thelightest and darkest pixels from the histogram, and remaps the imageso that the darkest pixel becomes black (0), and the lightestbecomes white (255).

Parameters:
  • image – The image to process.

  • cutoff – The percent to cut off from the histogram on the low andhigh ends. Either a tuple of (low, high), or a singlenumber for both.

  • ignore – The background pixel value (use None for no background).

  • mask – Histogram used in contrast operation is computed using pixelswithin the mask. If no mask is given the entire image is usedfor histogram computation.

  • preserve_tone

    Preserve image tone in Photoshop-like style autocontrast.

    Added in version 8.2.0.

Returns:

An image.

PIL.ImageOps.colorize(image:Image,black:str|tuple[int,...],white:str|tuple[int,...],mid:str|int|tuple[int,...]|None=None,blackpoint:int=0,whitepoint:int=255,midpoint:int=127)Image[source]

Colorize grayscale image.This function calculates a color wedge which maps all black pixels inthe source image to the first color and all white pixels to thesecond color. Ifmid is specified, it uses three-color mapping.Theblack andwhite arguments should be RGB tuples or color names;optionally you can use three-color mapping by also specifyingmid.Mapping positions for any of the colors can be specified(e.g.blackpoint), where these parameters are the integervalue corresponding to where the corresponding color should be mapped.These parameters must have logical order, such thatblackpoint<=midpoint<=whitepoint (ifmid is specified).

Parameters:
  • image – The image to colorize.

  • black – The color to use for black input pixels.

  • white – The color to use for white input pixels.

  • mid – The color to use for midtone input pixels.

  • blackpoint – an int value [0, 255] for the black mapping.

  • whitepoint – an int value [0, 255] for the white mapping.

  • midpoint – an int value [0, 255] for the midtone mapping.

Returns:

An image.

PIL.ImageOps.crop(image:Image,border:int=0)Image[source]

Remove border from image. The same amount of pixels are removedfrom all four sides. This function works on all image modes.

See also

crop()

Parameters:
  • image – The image to crop.

  • border – The number of pixels to remove.

Returns:

An image.

PIL.ImageOps.scale(image:Image,factor:float,resample:int=Resampling.BICUBIC)Image[source]

Returns a rescaled image by a specific factor given in parameter.A factor greater than 1 expands the image, between 0 and 1 contracts theimage.

Parameters:
  • image – The image to rescale.

  • factor – The expansion factor, as a float.

  • resample – Resampling method to use. Default isBICUBIC.SeeFilters.

Returns:

AnImage object.

classPIL.ImageOps.SupportsGetMesh(*args,**kwargs)[source]

Bases:Protocol

An object that supports thegetmesh method, taking an image as anargument, and returning a list of tuples. Each tuple contains two tuples,the source box as a tuple of 4 integers, and a tuple of 8 integers for thefinal quadrilateral, in order of top left, bottom left, bottom right, topright.

PIL.ImageOps.deform(image:Image,deformer:SupportsGetMesh,resample:int=Resampling.BILINEAR)Image[source]

Deform the image.

Parameters:
  • image – The image to deform.

  • deformer – A deformer object. Any object that implements agetmesh method can be used.

  • resample – An optional resampling filter. Same values possible asin the PIL.Image.transform function.

Returns:

An image.

PIL.ImageOps.equalize(image:Image,mask:Image|None=None)Image[source]

Equalize the image histogram. This function applies a non-linearmapping to the input image, in order to create a uniformdistribution of grayscale values in the output image.

Parameters:
  • image – The image to equalize.

  • mask – An optional mask. If given, only the pixels selected bythe mask are included in the analysis.

Returns:

An image.

PIL.ImageOps.expand(image:Image,border:int|tuple[int,...]=0,fill:str|int|tuple[int,...]=0)Image[source]

Add border to the image

Parameters:
  • image – The image to expand.

  • border – Border width, in pixels.

  • fill – Pixel fill value (a color value). Default is 0 (black).

Returns:

An image.

PIL.ImageOps.flip(image:Image)Image[source]

Flip the image vertically (top to bottom).

Parameters:

image – The image to flip.

Returns:

An image.

PIL.ImageOps.grayscale(image:Image)Image[source]

Convert the image to grayscale.

Parameters:

image – The image to convert.

Returns:

An image.

PIL.ImageOps.invert(image:Image)Image[source]

Invert (negate) the image.

Parameters:

image – The image to invert.

Returns:

An image.

PIL.ImageOps.mirror(image:Image)Image[source]

Flip image horizontally (left to right).

Parameters:

image – The image to mirror.

Returns:

An image.

PIL.ImageOps.posterize(image:Image,bits:int)Image[source]

Reduce the number of bits for each color channel.

Parameters:
  • image – The image to posterize.

  • bits – The number of bits to keep for each channel (1-8).

Returns:

An image.

PIL.ImageOps.solarize(image:Image,threshold:int=128)Image[source]

Invert all pixel values above a threshold.

Parameters:
  • image – The image to solarize.

  • threshold – All pixels above this grayscale level are inverted.

Returns:

An image.

PIL.ImageOps.exif_transpose(image:Image,*,in_place:Literal[True])None[source]
PIL.ImageOps.exif_transpose(image:Image,*,in_place:Literal[False]=False)Image

If an image has an EXIF Orientation tag, other than 1, transpose the imageaccordingly, and remove the orientation data.

Parameters:
  • image – The image to transpose.

  • in_place – Boolean. Keyword-only argument.IfTrue, the original image is modified in-place, andNone is returned.IfFalse (default), a newImage object is returnedwith the transposition applied. If there is no transposition, a copy of theimage will be returned.

Resize relative to a given size

fromPILimportImage,ImageOpssize=(100,150)withImage.open("Tests/images/hopper.webp")asim:ImageOps.contain(im,size).save("imageops_contain.webp")ImageOps.cover(im,size).save("imageops_cover.webp")ImageOps.fit(im,size).save("imageops_fit.webp")ImageOps.pad(im,size,color="#f00").save("imageops_pad.webp")# thumbnail() can also be used,# but will modify the image object in placeim.thumbnail(size)im.save("image_thumbnail.webp")

thumbnail()

contain()

cover()

fit()

pad()

Given size

(100,150)

(100,150)

(100,150)

(100,150)

(100,150)

Resulting image

../_images/image_thumbnail.webp../_images/imageops_contain.webp../_images/imageops_cover.webp../_images/imageops_fit.webp../_images/imageops_pad.webp

Resulting size

100×100

100×100

150×150

100×150

100×150

PIL.ImageOps.contain(image:Image,size:tuple[int,int],method:int=Resampling.BICUBIC)Image[source]

Returns a resized version of the image, set to the maximum width and heightwithin the requested size, while maintaining the original aspect ratio.

Parameters:
  • image – The image to resize.

  • size – The requested output size in pixels, given as a(width, height) tuple.

  • method – Resampling method to use. Default isBICUBIC.SeeFilters.

Returns:

An image.

PIL.ImageOps.cover(image:Image,size:tuple[int,int],method:int=Resampling.BICUBIC)Image[source]

Returns a resized version of the image, so that the requested size iscovered, while maintaining the original aspect ratio.

Parameters:
  • image – The image to resize.

  • size – The requested output size in pixels, given as a(width, height) tuple.

  • method – Resampling method to use. Default isBICUBIC.SeeFilters.

Returns:

An image.

PIL.ImageOps.fit(image:Image,size:tuple[int,int],method:int=Resampling.BICUBIC,bleed:float=0.0,centering:tuple[float,float]=(0.5,0.5))Image[source]

Returns a resized and cropped version of the image, cropped to therequested aspect ratio and size.

This function was contributed by Kevin Cazabon.

Parameters:
  • image – The image to resize and crop.

  • size – The requested output size in pixels, given as a(width, height) tuple.

  • method – Resampling method to use. Default isBICUBIC.SeeFilters.

  • bleed – Remove a border around the outside of the image from allfour edges. The value is a decimal percentage (use 0.01 forone percent). The default value is 0 (no border).Cannot be greater than or equal to 0.5.

  • centering – Control the cropping position. Use (0.5, 0.5) forcenter cropping (e.g. if cropping the width, take 50% offof the left side, and therefore 50% off the right side).(0.0, 0.0) will crop from the top left corner (i.e. ifcropping the width, take all of the crop off of the rightside, and if cropping the height, take all of it off thebottom). (1.0, 0.0) will crop from the bottom leftcorner, etc. (i.e. if cropping the width, take all of thecrop off the left side, and if cropping the height takenone from the top, and therefore all off the bottom).

Returns:

An image.

PIL.ImageOps.pad(image:Image,size:tuple[int,int],method:int=Resampling.BICUBIC,color:str|int|tuple[int,...]|None=None,centering:tuple[float,float]=(0.5,0.5))Image[source]

Returns a resized and padded version of the image, expanded to fill therequested aspect ratio and size.

Parameters:
  • image – The image to resize and crop.

  • size – The requested output size in pixels, given as a(width, height) tuple.

  • method – Resampling method to use. Default isBICUBIC.SeeFilters.

  • color – The background color of the padded image.

  • centering

    Control the position of the original image within thepadded version.

    (0.5, 0.5) will keep the image centered(0, 0) will keep the image aligned to the top left(1, 1) will keep the image aligned to the bottomright

Returns:

An image.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp