Skip to content

Commit

Permalink
Improve color averaging and update colors.txt again
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 9, 2019
1 parent cd0d1ad commit 0fd3dc1
Show file tree
Hide file tree
Showing 2 changed files with 355 additions and 357 deletions.
18 changes: 8 additions & 10 deletions autogenerating-colors.txt
Expand Up @@ -65,11 +65,9 @@ minetest.register_chatcommand("dumpnodes", {
==FILE== avgcolor.py
#!/usr/bin/env python
import sys
from math import sqrt
from PIL import Image

def tadd(a, b):
return tuple(sum(e) for e in zip(a, b))

if len(sys.argv) < 2:
print("Prints average color (RGB) of input image")
print("Usage: %s <input>" % sys.argv[0])
Expand All @@ -78,20 +76,20 @@ if len(sys.argv) < 2:
inp = Image.open(sys.argv[1]).convert('RGBA')
ind = inp.load()

cl = (0, 0, 0)
counted = 0
cl = ([], [], [])
for x in range(inp.size[0]):
for y in range(inp.size[1]):
px = ind[x, y]
if px[3] < 128: continue # alpha
cl = tadd(cl, px[:3])
counted += 1
cl[0].append(px[0]**2)
cl[1].append(px[1]**2)
cl[2].append(px[2]**2)

if counted == 0:
sys.stderr.write("did not find avg color for %s\n" % sys.argv[1])
if len(cl[0]) == 0:
print("Didn't find average color for %s" % sys.argv[1], file=sys.stderr)
print("0 0 0")
else:
cl = tuple(int(n / counted) for n in cl)
cl = tuple(sqrt(sum(x)/len(x)) for x in cl)
print("%d %d %d" % cl)
==SCRIPT==
#!/bin/bash -e
Expand Down

0 comments on commit 0fd3dc1

Please sign in to comment.