Ported hexometer for Bash to Python3

This commit is contained in:
Elvish_Hunter 2016-03-17 11:19:04 +01:00
parent bc6effd0de
commit 256dd1e3da
3 changed files with 136 additions and 118 deletions

136
data/tools/hexometer.py Executable file
View file

@ -0,0 +1,136 @@
#! /usr/bin/env python3
import os, sys, argparse, re, base64
from io import BytesIO
try:
from PIL import Image
except ImportError:
print("""Please install the Python Pillow Library to run this script.
You can download it from https://pypi.python.org/pypi/Pillow
On Debian and Ubuntu you can also type in a Terminal
sudo apt-get install python-pil""", file=sys.stderr)
sys.exit(1)
suffix_re = re.compile(".*-(n|s|w|e|ne|nw|sw|se|[0-9]+)([-.]).*")
anim_re = re.compile(".*-(attack|defend|melee|ranged|magic|idle|die|dying|death|flying|leading|healing).*")
# the default mask is a png RGBA file encoded in base64
default_mask = b"""
iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAA/UlEQVR42u3cWwoCMRBFQfe/6Tg/
gsg8b4bWxhLOAixEnSSdxxjjlpbX+KXuel8NcPIANQAKcGoDVAOU43QIUAFQhNMpQAVAAU6vABUA
BTi9AlQAFOD0ClABUIDTK0AFQAFOrwAVAAU4vQI0CzSP0z9AE0B/j/MKUAgE5y1AARCcjwBdBIKz
EqALQHA2AnQSCM5OgE4AwdnPp+cgX9C+gybyM+9/UJ5HDc9ieZY7rAflWXK1Jp1n28e+WJ6tZ3vz
eY6/OB+U5wieM4p5jgE7J51nFMGsRp5xKPNieUYyzazmGQs3N5/nagp3d+S5Hsf9QXmu6HKH2XeB
XBN40BNK3ENj+y1ozgAAAABJRU5ErkJggg=="""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description="Search png images not fitting in a hex",
epilog="""return numbers of pixels out of the hex for each filename
(-1 if the image is not a standard 72x72 image and -f was not used)"""
)
parser.add_argument("-m", "--mask",
action="store",
metavar="file",
help="""choose which image use as mask
(default is a mask embedded in the script)"""
)
parser.add_argument("-a", "--anim",
action="store_true",
help="""skip most animations images (containing:
-attack -defend -melee -ranged -magic -idle
-die -dying -death -healing -flying -leading)"""
)
parser.add_argument("-s", "--suffix",
action="store_true",
help="skip images with directional or numerical suffix"
)
parser.add_argument("-r", "--regex",
action="store",
metavar="REG",
help="""skip images matching the case-insensitive
regular expression REG (Python)"""
)
parser.add_argument("-f", "--format",
action="store_true",
help="skip images which are not in 72x72 format"
)
parser.add_argument("-q", "--quiet",
action="store_true",
help="only display results"
)
parser.add_argument("dirs",
action="store",
nargs="*",
help="directories to check",
default=os.getcwd()
)
args=parser.parse_args()
# get all the PNG images
images = []
for folder in args.dirs:
for root, dirs, files in os.walk(folder):
for filename in files:
if filename.lower().endswith(".png"):
images.append(os.path.join(root, filename))
# remove files matching the regexs
if args.anim:
images = [elem for elem in images if not anim_re.match(elem)]
if args.suffix:
images = [elem for elem in images if not suffix_re.match(elem)]
if args.regex:
images = [elem for elem in images if not re.match(args.regex, elem)]
images.sort()
if not args.quiet:
print("""Search 72x72 images not fitting in a hex
in directories: {}
Using alphamask image: {}
Skipping files matching regex: {}
Pixels out of hex : filename""".format(", ".join(args.dirs),
args.mask if args.mask else "",
args.regex if args.regex else ""))
# open the mask
if args.mask:
try:
mask = Image.open(args.mask)
except OSError:
print("cannot read mask file {}, exiting".format(args.mask), file=sys.stderr)
sys.exit(1)
else:
mask = Image.open(BytesIO(base64.b64decode(default_mask)))
mask_data = mask.getdata(3) # get alpha channel values
for fn in images:
try:
img = Image.open(fn)
except OSError:
print("cannot read file {}, skipping".format(fn), file=sys.stderr)
continue
# check if the image size is incorrect first
if img.size != (72, 72):
if args.format:
continue
else:
px = -1
else:
px = 0
comp = Image.alpha_composite(img, mask)
# compare the alpha channel pixel by pixel
# this was the method used by the Bash version
comp_data = comp.getdata(3)
for i, mask_alpha in enumerate(mask_data):
if comp_data[i] != mask_alpha:
px += 1
if px != 0:
print("{:d}\t: {}".format(px, fn))
sys.exit(0)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 310 B

View file

@ -1,118 +0,0 @@
#!/bin/sh
scriptname=$(basename $0)
help ()
{
echo "Search png images not fitting in a hex"
echo
echo "$scriptname [option] [directory1 directory2 ...]"
echo "-m, --mask file choose which image use as mask"
echo " (default = alphamask.png)"
echo "-a, --anim skip most animations images (containing:"
echo " -attack -defend -melee -ranged -magic -idle"
echo " -die -dying -death -healing -flying -leading)"
echo "-s, --suffix skip images with directional or numerical suffix"
echo "-r, --regex REG skip images matching the case-insensitive"
echo " regular expression REG (posix-extended)"
echo "-f, --format skip images which are not in 72x72 format"
echo "-q, --quiet only display results"
echo
echo "return numbers of pixels out of the hex for each filename"
echo "(-1 if the image is not a standard 72x72 image and -f was not used"
}
mask="$(dirname $0)/alphamask.png"
format=0
quiet=0
regex=""
suffix=".*(-n|-s|-w|-e|-ne|-nw|-sw|-se|-[0-9]+)(-|[.]).*"
anim=".*-attack.*|.*-defend.*|.*-melee.*|.*-ranged.*|.*-magic.*|.*-idle.*|.*-die.*|.*-dying.*|.*-death.*|.*-flying.*|.*-leading.*|.*-healing.*"
until [ -z "$1" ]
do
case $1 in
-h|--help)
help
exit;;
-m|--mask)
shift
mask="$1"
shift
continue;;
-r|--regex)
shift
regex="$regex|$1"
shift
continue;;
-a|--anim)
regex="$regex|$anim"
shift
continue;;
-s|--suffix)
regex="$regex|$suffix"
shift
continue;;
-f|--format)
format=1
shift
continue;;
-q|--quiet)
quiet=1
shift
continue;;
esac
# record all given directories
dir="$dir $1"
shift
done
# if no directory, use current
if [ "$dir" = "" ]
then
dir='.'
fi
if [ ! -r $mask ]
then
echo "$scriptname: cannot access $mask: No such file or directory"
exit 1
fi
if [ "$quiet" = 0 ]
then
echo "Search 72x72 images not fitting in a hex"
echo "in directories: $dir"
echo "Using alphamask image: $mask"
echo "Skipping files matching regex: $regex"
echo "Pixels out of hex : filename"
fi
test_img()
{
if [ `identify -format "%wx%h" $img` != '72x72' ]
then
if [ $format = 0 ]
then
px=-1
else
# mark as ok, because user want to skip bad format
px=0
fi
else
px=`composite $mask $img /tmp/tmp_result.png && compare -channel alpha -metric AE $mask /tmp/tmp_result.png /dev/null 2>&1 `
fi
if [ "$px" != 0 ]
then
echo "$px : $img"
fi
}
find $dir -regextype posix-extended -name '*.png' -and -not -iregex "$regex" \
-print | sort | while read img; do test_img; done
rm -f /tmp/tmp_result.png
exit