Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.