Skip to content

Instantly share code, notes, and snippets.

@matcool
Created January 16, 2018 21:06
Show Gist options
  • Save matcool/e35a1d2131e480adc4ce866292be3a61 to your computer and use it in GitHub Desktop.
Save matcool/e35a1d2131e480adc4ce866292be3a61 to your computer and use it in GitHub Desktop.
#Based off coding train:
#https://github.com/CodingTrain/Rainbow-Code/blob/master/CodingChallenges/CC_90_dithering
from PIL import Image,ImageDraw
img = Image.open("kitten.jpg").convert("L").convert("RGB")
d = ImageDraw.Draw(img)
for y in range(img.height-1):
for x in range(1,img.width-1):
oldC = img.getpixel((x,y))
factor = 1
newC = []
for i in oldC:
newC.append(int(round(factor*i/255)*(255/factor)))
newC = tuple(newC)
d.point((x,y),fill=newC)
errC = []
for i in range(3):
errC.append(oldC[i]-newC[i])
errC = tuple(errC)
indexes = [(x+1,y),(x-1,y+1),(x,y+1),(x+1,y+1)]
numbers = (7,5,3,1)
for i in range(3):
try:
c = img.getpixel(indexes[i])
except IndexError:
print(indexes[i])
color = []
for sex in range(3):
color.append(int(c[sex] + errC[sex] * numbers[i]/16))
color = tuple(color)
d.point(indexes[i],fill=color)
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment