Skip to content

Instantly share code, notes, and snippets.

@radupotop
Created May 23, 2023 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save radupotop/ea41c108de0cb987eacff6dae2a2c3ed to your computer and use it in GitHub Desktop.
Save radupotop/ea41c108de0cb987eacff6dae2a2c3ed to your computer and use it in GitHub Desktop.
Atkinson dithering
#!/usr/bin/python2
import sys, PIL.Image
img = PIL.Image.open(sys.argv[-1]).convert('L')
threshold = 128*[0] + 128*[255]
for y in range(img.size[1]):
for x in range(img.size[0]):
old = img.getpixel((x, y))
new = threshold[old]
err = (old - new) >> 3 # divide by 8
img.putpixel((x, y), new)
for nxy in [(x+1, y), (x+2, y), (x-1, y+1), (x, y+1), (x+1, y+1), (x, y+2)]:
try:
img.putpixel(nxy, img.getpixel(nxy) + err)
except IndexError:
pass
img.show(command="/usr/bin/gimp")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment