Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Created July 3, 2020 22:22
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 Caffe1neAdd1ct/9c62fab8b850ddd455891159224b8155 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/9c62fab8b850ddd455891159224b8155 to your computer and use it in GitHub Desktop.
Counts background pixels in an image
#!/usr/bin/env python
from PIL import Image
image = Image.open("image.jpg")
bg = image.getpixel((0,0))
width, height = image.size
bg_count = next(n for n,c in image.getcolors(width*height) if c==bg)
img_count = width*height - bg_count
img_percent = img_count*100.0/width/height
print(img_percent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment