ImageOps Module

Note

All source images in this document are derived from https://www.pexels.com (CC0 License).

Functions

autocontrast

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.autocontrast, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.autocontrast

from PIL import Image, ImageOps

#
for bn in ("srcimg20", "srcimg21", "srcimg22", "srcimg23",):
    img = Image.open("data/" + bn + ".jpg")
    for i, cutoff in enumerate((
            0,
            0.2,  # 20%
            0.4   # 40%
            )):
        dimg = ImageOps.autocontrast(img, cutoff=cutoff)
        dimg.save(
            "result/ImageOps_autocontrast_%s_0%d.jpg" % (bn, i + 1))

src srcimg20_100

cutoff=0 ImageOps_autocontrast.res201

cutoff=0.2 ImageOps_autocontrast.res202

cutoff=0.4 ImageOps_autocontrast.res203

src srcimg21_100

cutoff=0 ImageOps_autocontrast.res211

cutoff=0.2 ImageOps_autocontrast.res212

cutoff=0.4 ImageOps_autocontrast.res213

src srcimg22_100

cutoff=0 ImageOps_autocontrast.res221

cutoff=0.2 ImageOps_autocontrast.res222

cutoff=0.4 ImageOps_autocontrast.res223

src srcimg23_100

cutoff=0 ImageOps_autocontrast.res231

cutoff=0.2 ImageOps_autocontrast.res232

cutoff=0.4 ImageOps_autocontrast.res233

colorize

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.colorize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.colorize

srcimg23.jpg srcimg23

from PIL import Image, ImageOps

img = Image.open("data/srcimg23.jpg")  # grayscale ("L")

# 
dimg = ImageOps.colorize(
    img,
    black="midnightblue",
    white="white")
dimg.save("result/ImageOps_colorize_01.jpg")

ImageOps.colorize.res01

crop

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.crop, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.crop

srcimg11.png srcimg11

from PIL import Image, ImageOps

img = Image.open('data/srcimg11.png')
ImageOps.crop(img, img.size[1] // 4).save(
    "result/ImageOps_crop_01.jpg")

ImageOps.crop.res01

deform

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.deform, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.deform

Deform the image using the given deformer object. The deformer should provide a getmesh method, which returns a MESH list suitable for the Image transform method. See the transform method for details.

srcimg17.jpg srcimg17

from PIL import Image, ImageOps

class _Deformer(object):

    # The deformer should provide a getmesh method,
    # which returns a MESH list suitable for the Image
    # transform method. See the transform method for details.
    def getmesh(self, img):

        (w, h) = img.size

        # data is a list of target rectangles
        # and corresponding source quadrilaterals.
        return [(
                # target rectangle (1)
                (0, 0, w // 2, h // 2),
                # corresponding source quadrilateral (1)
                # (NW, SW, SE, and NE. see method=QUAD)
                (0, 0, 0, h, w, h, w - 100, 0)
               ),
              (
                # target rectangle (2)
                (w // 2, h // 2, w, h),
                # corresponding source quadrilateral (2)
                # (NW, SW, SE, and NE. see method=QUAD)
                (0, 0, 0, h, w, h, w - 100, 0)
                ),
              ]

#
simg = Image.open('data/srcimg17.jpg')
dimg = ImageOps.deform(simg, _Deformer())
dimg.save(
    "result/ImageOps_deform_01.jpg")

ImageOps_deform.res1

equalize

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.equalize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.equalize

srcimg07.jpg srcimg07

from PIL import Image, ImageOps
#
fn = 'data/srcimg07.jpg'
simg = Image.open(fn)
simg_gs = simg.convert("L")  # grayscale
simg_gs.save("result/ImageOps_equalize_sgs.jpg")

# Equalize the image histogram.
dimg = ImageOps.equalize(simg)
dimg.save("result/ImageOps_equalize_d.jpg")
dimg_gs = dimg.convert("L")  # grayscale
dimg_gs.save("result/ImageOps_equalize_dgs.jpg")

#
# visualize histogram
import matplotlib.pyplot as plt  # https://matplotlib.org
bins = list(range(256))
plt.plot(bins, simg_gs.histogram(), 'r')
plt.plot(bins, dimg_gs.histogram(), 'g')
plt.xlabel('Pixel value')
plt.ylabel('Frequency')
plt.title(fn)
plt.legend(
    ('src (grayscale)', 'dest (grayscale)'),
     shadow=True, loc=(0.01, 0.75))
plt.grid(True)
plt.savefig("result/ImageOps_equalize_hist_01.jpg")

convert(“L”) to orig ImageOps_equalize.res1

equalize to orig ImageOps_equalize.res2

convert(“L”) to equalized ImageOps_equalize.res3

histogram ImageOps_equalize.res4

expand

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.expand, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.expand

srcimg05.jpg srcimg05

from PIL import Image, ImageOps

img = Image.open('data/srcimg05.jpg')
dimg = ImageOps.expand(img, border=100)
dimg.save("result/ImageOps_expand_01.jpg")
dimg = ImageOps.expand(img, border=100, fill="burlywood")
dimg.save("result/ImageOps_expand_02.jpg")

border=100, fill=0 ImageOps.expand.res01

border=100, fill=”burlywood” ImageOps.expand.res02

from PIL import Image, ImageOps

img = Image.open('data/srcimg05.jpg')
# left, top = right, bottom = (100, 50)
dimg = ImageOps.expand(img, border=(100, 50))
dimg.save("result/ImageOps_expand_03.jpg")

# left, top, right, bottom = (20, 30, 40, 50)
dimg = ImageOps.expand(img, border=(20, 30, 40, 50))
dimg.save("result/ImageOps_expand_04.jpg")

border=(100, 50) ImageOps.expand.res03

border=(20, 30, 40, 50) ImageOps.expand.res04

from PIL import Image, ImageOps

def _resize_to_square(img, fill=0):
    if img.width > img.height:
        border = (0, (img.width - img.height) // 2)
    elif img.width < img.height:
        border = ((img.height - img.width) // 2, 0)
    else:
        return img.copy()
    return ImageOps.expand(img, border, fill)

img = Image.open('data/srcimg05.jpg')
dimg = _resize_to_square(img)
dimg.save("result/ImageOps_expand_05.jpg")
dimg = _resize_to_square(img.rotate(90, expand=True))
dimg.save("result/ImageOps_expand_06.jpg")

ImageOps.expand.res05

ImageOps.expand.res06

fit

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.fit, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.fit

srcimg11.png srcimg11

from PIL import Image, ImageOps

simg = Image.open('data/srcimg11.png')
#
dimg = ImageOps.fit(simg,
    size=(simg.size[0], simg.size[1] // 3),
    centering=(0.5, 0.0))  # 50%, 0%
dimg.save("result/ImageOps_fit_01.jpg")
#
dimg = ImageOps.fit(simg,
    size=(simg.size[0], simg.size[1] // 3),
    centering=(0.5, 0.5))  # 50%, 50% <= default
dimg.save("result/ImageOps_fit_02.jpg")
#
dimg = ImageOps.fit(simg,
    size=(simg.size[0], simg.size[1] // 3),
    centering=(0.5, 1.0))  # 50%, 100%
dimg.save("result/ImageOps_fit_03.jpg")
#
dimg = ImageOps.fit(simg,
    size=(simg.size[0] // 3, simg.size[1]),
    centering=(0.0, 0.5))  # 0%, 50%
dimg.save("result/ImageOps_fit_04.jpg")

#
dimg = ImageOps.fit(simg,
    size=(simg.size[0] // 3, simg.size[1]),
    centering=(0.5, 0.5))  # 50%, 50% <= default
dimg.save("result/ImageOps_fit_05.jpg")
#
dimg = ImageOps.fit(simg,
    size=(simg.size[0] // 3, simg.size[1]),
    centering=(1.0, 0.5))  # 100%, 50%
dimg.save("result/ImageOps_fit_06.jpg")
ImageOps.fit.res01 height=1/3, centering=0
ImageOps.fit.res04 width=1/3, centering=0
ImageOps.fit.res02 height=1/3, centering=0.5
ImageOps.fit.res05 width=1/3, centering=0.5
ImageOps.fit.res03 height=1/3, centering=1
ImageOps.fit.res06 width=1/3, centering=1

flip

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.flip, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.flip

PIL.ImageOps.flip(image) is identical to image.transpose(PIL.Image.FLIP_TOP_BOTTOM).

invert

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.invert, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.invert

srcimg18.jpg srcimg18

from PIL import Image, ImageOps

img1 = Image.open("data/srcimg18.jpg")

# ImageOps.invert is maybe identical to ImageChops.invert.
dimg = ImageOps.invert(img1)
dimg.save("result/ImageOps_invert_01.jpg")

ImageOps.invert.res01

mirror

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.mirror, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.mirror

PIL.ImageOps.mirror(image) is identical to image.transpose(PIL.Image.FLIP_LEFT_RIGHT).

posterize

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.posterize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.posterize

srcimg06.jpg srcimg06

from PIL import Image, ImageOps

simg = Image.open('data/srcimg06.jpg')
#
for i in (1, 2, 3, 4, 5, 6, 7, 8):
    dimg = ImageOps.posterize(simg, bits=i)
    dimg.save("result/ImageOps_posterize_b%d.jpg" % i)

bits=1 ImageOps.posterize.res01

bits=2 ImageOps.posterize.res02

bits=3 ImageOps.posterize.res03

bits=4 ImageOps.posterize.res04

bits=5 ImageOps.posterize.res05

bits=6 ImageOps.posterize.res06

bits=7 ImageOps.posterize.res07

bits=8 ImageOps.posterize.res08

solarize

doc

https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.solarize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.solarize

srcimg12.jpg srcimg12

from PIL import Image, ImageOps

simg = Image.open("data/srcimg12.jpg")

dimg = ImageOps.solarize(simg, threshold=128)  # default
dimg.save("result/ImageOps_solarize_01.jpg")

dimg = ImageOps.solarize(simg, threshold=50)
dimg.save("result/ImageOps_solarize_02.jpg")

dimg = ImageOps.solarize(simg, threshold=0)  # identical to ImageOps.invert
dimg.save("result/ImageOps_solarize_03.jpg")

threshold=128 ImageOps.solarize.res01

threshold=50 ImageOps.solarize.res02

threshold=0 ImageOps.solarize.res03