Basic Operations
因为我有点儿懒。。。所以本篇后续就基本是英文了(输入法切换好累0.0),本教程配套有Jupyter Notebook,请在此处下载。
Python3 based PIL(python image library) tutorials from Documents
from PIL import ImageFilter
from PIL import Image
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
im = Image.open("test.jpg")
print('format: ', im.format)
print('size: ', im.size)
print('mode: ', im.mode)
im.show() # show the figure in photo viewer
format: JPEG
size: (625, 625)
mode: RGB
Cut A Certain Region
box = (100, 100, 400, 400)
region = im.crop(box)
# region.show()
plt.imshow(region)
plt.show()
Rotate
box = (100, 100, 400, 400)
region = im.crop(box)
region = region.transpose(Image.ROTATE_180)
im.paste(region, box)
# im.show()
plt.imshow(im)
plt.show()
Splitting and Merging Bands
im = Image.open("test.jpg")
r, g, b = im.split()
# r.show()
# g.show()
# b.show()
f, ax = plt.subplots(2, 2)
ax[0, 0].imshow(r)
ax[0, 0].set_title("R")
ax[0, 1].imshow(g)
ax[0, 1].set_title("G")
ax[1, 0].imshow(b)
ax[1, 0].set_title("B")
im = Image.merge("RGB", (g, b, r))
ax[1, 1].imshow(im)
ax[1, 1].set_title("Mixed Image")
plt.show()
Geometrical Transforms
im = Image.open("test.jpg")
out = im.resize((