Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created November 10, 2016 10:08
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 MartinThoma/f51a1044c4abc6c7b81915ef96b7cfbd to your computer and use it in GitHub Desktop.
Save MartinThoma/f51a1044c4abc6c7b81915ef96b7cfbd to your computer and use it in GitHub Desktop.
Playing with image filters.
#!/usr/bin/env python
"""Playing with image filters."""
import scipy.misc
import scipy.ndimage
import numpy as np
# Load an example image
# Use scipy.ndimage.imread(file_name, mode='L') if you have your own
img = scipy.misc.face()
# Convert to grayscale
R = img[:, :, 0]
G = img[:, :, 1]
B = img[:, :, 2]
img_gray = R * 299. / 1000 + G * 587. / 1000 + B * 114. / 1000
img = img_gray
# Show and save original image
scipy.misc.imshow(img)
scipy.misc.imsave("original.png", img)
# Convolve
k = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
new1 = scipy.ndimage.convolve(img, k, mode='constant', cval=0.0)
# Show and save convolved image
scipy.misc.imshow(new1)
scipy.misc.imsave("new1.png", new1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment