Skip to content

Instantly share code, notes, and snippets.

@RECS-Tsukuba
Last active December 23, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RECS-Tsukuba/6648180 to your computer and use it in GitHub Desktop.
Save RECS-Tsukuba/6648180 to your computer and use it in GitHub Desktop.
x軸方向のsobelフィルタのサンプル(Python)
# coding: UTF-8
from PIL import Image
# 画像を取得
image = Image.open('input.png', 'r').convert('L')
# サイズを取得
width, height = image.size
# 出力画像を生成
filtered = image.copy()
for x in range(1, image.size[0] - 1):
for y in range(1, image.size[1] - 1):
# ============この部分を書き換える===============
# ここでは例としてx軸方向のsobelフィルタの処理を記述する
# x軸方向のsobelフィルタを適用
x_sobel = (image.getpixel((x + 1, y - 1)) \
+ 2 * image.getpixel((x + 1, y)) \
# (x + 1, y + 1)の画素を取得
+ image.getpixel((x + 1, y + 1))) \
- (image.getpixel((x - 1, y - 1)) \
+ 2 * image.getpixel((x - 1, y)) \
+ image.getpixel((x - 1, y + 1)))
# 画素を設定
filtered.putpixel((x, y), abs(x_sobel / 4))
# ===============================================
# 画像を出力
filtered.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment