Skip to content

Instantly share code, notes, and snippets.

@franktoffel
Last active November 2, 2016 16:40
Show Gist options
  • Save franktoffel/ac694ddb82dc7860bdbf4c0d2c3ea1a9 to your computer and use it in GitHub Desktop.
Save franktoffel/ac694ddb82dc7860bdbf4c0d2c3ea1a9 to your computer and use it in GitHub Desktop.
Different digital bokeh shapes with Python (scikit image)
import numpy as np
from skimage.morphology import diamond, disk, square
from skimage.filters.rank import mean
import matplotlib.pyplot as plt
n = 20
l = 256
im = np.zeros((l, l))
points = l * np.random.random((2, n ** 2))
# Original image
im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
fig, axes = plt.subplots(nrows=1, ncols=4, figsize=(12, 10))
axes[3].imshow(im, cmap=plt.cm.viridis)
axes[3].set_title('original image', fontsize=10)
axes[3].set_axis_off()
element_list = [diamond, disk, square]
for i, element in enumerate(element_list):
selection_element = element(5) # matrix of n pixels with a disk shape
im_mean = mean(im, selection_element)
axes[i].imshow(im_mean, cmap=plt.cm.viridis)
axes[i].set_title(element.__name__, fontsize=10)
axes[i].set_axis_off()
plt.show()
@franktoffel
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment