Last active
August 29, 2015 14:18
-
-
Save jindrichmynarz/0d5bb7cac0b0a01d2911 to your computer and use it in GitHub Desktop.
Is an image mostly red or green?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os, sys | |
from PIL import Image | |
def get_red_green_balance(filename): | |
im = Image.open(filename) | |
r, g = reduce(lambda c1, c2: [c1[0] + c2[0], c1[1] + c2[1]], im.getdata(), [0, 0]) | |
percent = (r + g) / 100.0 | |
return round(r / percent, 2), round(g / percent, 2) | |
def main(path): | |
if not os.path.exists(path): | |
print "Path {} doesn't exist!".format(path) | |
sys.exit() | |
table = "{:<31}{:<8}{:<8}" | |
print table.format("File", "Red %", "Green %") | |
print table.format("-" * 30, "-" * 7, "-" * 7) | |
if os.path.isdir(path): | |
for fname in [os.path.join(path, fn) for fn in next(os.walk(path))[2]]: | |
r, g = get_red_green_balance(fname) | |
print table.format(fname[:27] + "..." if len(fname) > 30 else fname, r, g) | |
else: | |
r, g = get_red_green_balance(path) | |
print table.format(path[:30], r, g) | |
if __name__ == "__main__": | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment