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  | cutoff=0  | cutoff=0.2  | cutoff=0.4  | 
| src  | cutoff=0  | cutoff=0.2  | cutoff=0.4  | 
| src  | cutoff=0  | cutoff=0.2  | cutoff=0.4  | 
| src  | cutoff=0  | cutoff=0.2  | cutoff=0.4  | 
colorize¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.colorize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.colorize 
| 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")
 | 
 | 
crop¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.crop, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.crop 
| from PIL import Image, ImageOps
img = Image.open('data/srcimg11.png')
ImageOps.crop(img, img.size[1] // 4).save(
    "result/ImageOps_crop_01.jpg")
 | 
 | 
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.
| 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")
 | 
 | 
equalize¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.equalize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.equalize 
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  | equalize to orig  | convert(“L”) to equalized  | histogram  | 
expand¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.expand, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.expand 
| 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  | border=100, fill=”burlywood”  | 
| 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)  | border=(20, 30, 40, 50)  | 
| 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")
 | 
 | 
 | 
fit¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.fit, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.fit 
| 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")
 | 
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).
grayscale¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.grayscale, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.grayscale 
PIL.ImageOps.grayscale(image) is identical to image.convert("L").
invert¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.invert, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.invert 
| 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")
 | 
 | 
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 
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  | bits=2  | bits=3  | bits=4  | 
| bits=5  | bits=6  | bits=7  | bits=8  | 
solarize¶
- doc
- https://pillow.readthedocs.io/en/latest/reference/ImageOps.html#PIL.ImageOps.solarize, http://effbot.org/imagingbook/imageops.htm#tag-ImageOps.solarize 
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  | threshold=50  | threshold=0  | 





















































