Skip to content

Instantly share code, notes, and snippets.

@aginor
Created June 25, 2018 09:46
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 aginor/50688599ce55047813439d96a57b7f47 to your computer and use it in GitHub Desktop.
Save aginor/50688599ce55047813439d96a57b7f47 to your computer and use it in GitHub Desktop.
from PIL import Image
im = Image.open("indata.png")
pix = im.load()
pixel_values = list(im.getdata())
counts = {}
(xmax,ymax) = im.size
for val in pixel_values:
if val not in counts.keys():
counts[val] = 1
else:
counts[val] = counts[val] + 1
setvalues = set(pixel_values)
setvalues = sorted(setvalues)
print(setvalues)
print(len(setvalues))
for key in counts.keys():
if counts[key] > 100:
print(key, counts[key])
for key in counts.keys():
if counts[key] > 100:
im2 = im.copy()
pix = im2.load()
for x in range(0,xmax):
for y in range(0,ymax):
data = pix[x,y]
if data == key :
pix[x,y] = (0,0,0,255)
else:
pix[x, y] = (255, 255, 255, 255)
im2.save('out/test{}.png'.format(key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment