%pylab inline
from PIL import Image,ImageDraw,ImageFont
img = Image.open('work-image/lena.jpg')
pl_img = np.array(img) ; plt.imshow( pl_img ) # 表示
Populating the interactive namespace from numpy and matplotlib
<matplotlib.image.AxesImage at 0xaa34694c>
# グレースケールに変換
gray_img = img.convert("L")
ちなみに、保存したファイルは正常にグレースケールに変換されていました。
pl_img = np.array(gray_img) ; plt.imshow( pl_img ) # 表示
<matplotlib.image.AxesImage at 0xa5b90c4c>
# ファイルの情報表示
print('size : ', gray_img.size)
print('format : ', gray_img.format)
print('mode : ', gray_img.mode)
print('palette : ', gray_img.palette)
print('info : ', gray_img.info)
size : (512, 512) format : None mode : L palette : None info : {'jfif_density': (1, 1), 'jfif_version': (1, 1), 'jfif_unit': 0, 'jfif': 257}
# こうしなければならんらしい
import matplotlib.cm as cm
plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa8643f6c>
from PIL import ImageFilter, ImageOps
# ぼかし
pl_img = np.array(gray_img.filter(ImageFilter.BLUR)); plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa7490dcc>
# 輪郭抽出
pl_img = np.array(gray_img.filter(ImageFilter.CONTOUR)); plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa74444cc>
# エンボス加工
pl_img = np.array(gray_img.filter(ImageFilter.EMBOSS)); plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa6ab832c>
# 最小値フィルタ
pl_img = np.array(gray_img.filter(ImageFilter.MinFilter(5))); plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa6ae7c6c>
理論的なことは後で書く(本当に書くのか?)
# 縦方向のエッジを検出
flist = [1, 1, 1,
0, 0, 0,
-1, -1, -1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa669ceec>
# 横方向のエッジを検出
flist = [1, 0, -1,
1, 0, -1,
1, 0, -1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa644bdcc>
# 4傍近接ラプラシアン
flist = [0, 1, 0,
1, -4, 1,
0, 1, 0]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa61ffa2c>
# 8傍近接ラプラシアン
flist = [1, 1, 1,
1, -8, 1,
1, 1, 1]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa5fb07ac>
# 8傍近接ラプラシアンの利用例
flist = [0.4, 0.4, 0.4,
0.4, -2.2, 0.4,
0.4, 0.4, 0.4]
flt = ImageFilter.Kernel((3, 3), flist, scale=1)
filter_img = gray_img.filter(flt)
pl_img = np.array(filter_img) ; plt.imshow(pl_img, cmap = cm.Greys_r)
<matplotlib.image.AxesImage at 0xa5fe446c>