Skip to content

Instantly share code, notes, and snippets.

@mgautam98
Created July 8, 2020 08:37
Show Gist options
  • Save mgautam98/d8813c2976d693d2c8501be574086ba0 to your computer and use it in GitHub Desktop.
Save mgautam98/d8813c2976d693d2c8501be574086ba0 to your computer and use it in GitHub Desktop.
# https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering
function dither(img::AbstractArray{<:Colorant})
n,m = size(img)
for i in 2:n-1
for j in 2:m-1
old_pixel = img[i,j]
new_pixel = round(old_pixel*256)/256
img[i,j] = new_pixel
err = old_pixel - new_pixel
img[i+1, j] += err*(7.0/16)
img[i-1, j+1] += err*(3.0/16)
img[i, j+1] += err*(5.0/16)
img[i+1, j+1] += err*(1.0/16)
end
end
return img
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment