Skip to content

Instantly share code, notes, and snippets.

@villares
Last active November 14, 2020 20:07
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 villares/8cdf9ff7b4b939848dce1bff271b00ef to your computer and use it in GitHub Desktop.
Save villares/8cdf9ff7b4b939848dce1bff271b00ef to your computer and use it in GitHub Desktop.
Repeatedly Sharpen and Blur an image - Add a /data/ folder to your sketch with a `img.jpg` file
# based on https://processing.org/tutorials/pixels/
w = 80
# It's possible to perform a convolution
# the image with different matrices
sharpen_kernel = [[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]]
blur_kernel = [[1.0 / 9] * 3] * 3
def settings():
size(200, 200)
def setup():
global img
img = loadImage("img.jpg")
# this.surface.setResizable(True)
this.surface.setSize(img.width, img.height)
def draw():
# We're only going to process a portion of the image
# so let's set the whole image as the background first
image(img, 0, 0)
# Where is the small rectangle we will process
# xstart = constrain(mouseX-w/2,0,img.width)
# ystart = constrain(mouseY-w/2,0,img.height)
# xend = constrain(mouseX+w/2,0,img.width)
# yend = constrain(mouseY+w/2,0,img.height)
xstart = 0 # width / 2
ystart = 0
xend = img.width
yend = img.height
kernel_size = 3
img.loadPixels()
# Begin our loop for every pixel
for x in xrange(xstart, xend):
for y in xrange(ystart, yend):
# Each pixel location (x,y) gets passed into a function called convolution()
# which returns a new color value to be displayed.
c = convolution(x, y, sharpen_kernel, kernel_size, img)
loc = x + y * img.width
img.pixels[loc] = c
for x in xrange(xstart, xend):
for y in xrange(ystart, yend):
# Each pixel location (x,y) gets passed into a function called convolution()
# which returns a new color value to be displayed.
c = convolution(x, y, blur_kernel, kernel_size, img)
loc = x + y * img.width
img.pixels[loc] = c
img.updatePixels()
# stroke(0)
# noFill()
# rect(xstart,ystart,w,w)
def convolution(x, y, kernel, kernel_size, img):
rtotal = 0.0
gtotal = 0.0
btotal = 0.0
offset = kernel_size / 2
# Loop through convolution kernel
for i in xrange(kernel_size):
for j in xrange(kernel_size):
# What pixel are we testing
xloc = x + i - offset
yloc = y + j - offset
loc = xloc + img.width * yloc
# Make sure we have not walked off the edge of the pixel array
loc = constrain(loc, 0, len(img.pixels) - 1)
# Calculate the convolution
# We sum all the neighboring pixels multiplied by the convolution
# kernel values.
rtotal += (red(img.pixels[loc]) * kernel[i][j])
gtotal += (green(img.pixels[loc]) * kernel[i][j])
btotal += (blue(img.pixels[loc]) * kernel[i][j])
# Make sure RGB is within range
rtotal = constrain(rtotal, 0, 255)
gtotal = constrain(gtotal, 0, 255)
btotal = constrain(btotal, 0, 255)
# Return the resulting color
return color(rtotal, gtotal, btotal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment