Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Created September 20, 2017 01:41
Show Gist options
  • Save bgschiller/74a118f68a0a4f696bd80cf6f7f6aa76 to your computer and use it in GitHub Desktop.
Save bgschiller/74a118f68a0a4f696bd80cf6f7f6aa76 to your computer and use it in GitHub Desktop.
a (needlessly complex) solution to http://coding101.devetry.com/Studios/blurring.html
import image
import sys
import random
def randGauss():
"""
it would be better to use random.normalvariate(), but it looks like the
textbook version of python doesn't include that function :(
"""
return (
random.random() + random.random() +
random.random() + random.random() +
random.random() + random.random() - 3) / 3
img = image.Image("luther.jpg")
new_img = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin(img.getWidth(), img.getHeight())
width = img.getWidth()
height = img.getHeight()
for i in range(0, width):
for j in range(0, height):
# Randomly choose the coordinates of a neighboring pixel
x = i + int(randGauss() * 3)
y = j + int(randGauss() * 20)
# if it's smaller than 0, choose 0. If it's larger than width - 1, choose width - 1.
x = min(width - 1, max(0, x))
y = min(height - 1, max(0, y))
nearby_pixel = img.getPixel(x, y)
new_img.setPixel(i, j, nearby_pixel)
new_img.draw(win)
win.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment