view games/cards/split_card.sh @ 4:b51e7418840e

file reorganisation
author Goffi <goffi@goffi.org>
date Sat, 18 Jun 2011 16:39:37 +0200
parents frontends/src/wix/images/split_card.sh@8b41eb519075
children
line wrap: on
line source

#!/bin/sh
#This script split an image with cards, used to split cards from the Tarot deck found on Wikimedia Commons
#This script work with any resolution on the initial image
#Copyright (C) 2009, 2010  Jérôme Poisson (goffi@goffi.org)

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

dest_dir=cards

get_face_name()
{
	if [ $1 -le 10 ]
	then
		echo $1
	else
		case $1 in
		11) echo valet;;
		12) echo cavalier;;
		13) echo dame;;
		14) echo roi;;
		esac
	fi
}

get_card_name()
{
	if [ $1 -le 21 ]
	then
		echo "atout_$1"
	elif [ $1 -eq 22 ]; then
		echo "atout_excuse"
	elif [ $1 -le 36 ]; then
		echo "pique_$(get_face_name $(($1-22)))"
	elif [ $1 -le 50 ]; then
		echo "coeur_$(get_face_name $(($1-36)))"
	elif [ $1 -le 64 ]; then
		echo "carreau_$(get_face_name $(($1-50)))"
	else
		echo "trefle_$(get_face_name $(($1-64)))"
	fi
}

#We check the version of convert
CONVERT_VER=`convert --version | grep Version | grep -o "[0-9]\.[0-9]\.[0-9]"`
CONVERT_MAJOR=`echo $CONVERT_VER | cut -d . -f 1`
CONVERT_MINOR=`echo $CONVERT_VER | cut -d . -f 2`
CONVERT_REV=`echo $CONVERT_VER | cut -d . -f 3`

if [ $CONVERT_MAJOR -lt 6 -o $CONVERT_MAJOR -eq 6 -a $CONVERT_MINOR -lt 6 ]
then
	echo "ImageMagick convert must be at least version 6.6.0 (current: $CONVERT_VER)"
	exit 1
fi

SYNTAXE="Split card image\nsyntaxe: $0 image_to_split.ext"

if [ $# -ne 1 ]
then
	echo  $SYNTAXE
	exit 1
fi

echo `file -b --mime-type $1` | grep image 2>&1 > /dev/null

if [ $? -ne 0 ]
then
	echo "target file is not an image"
	exit 1
fi

current=`pwd`
#TODO: check directory presence
#echo "making directory"
if test -e $dest_dir
then
	if test -n "`ls -A $dest_dir`"
	then
		echo "$dest_dir directory exists and is not empty !"
		exit 1
	fi
else
	mkdir $dest_dir
fi
echo "splitting cards"
convert Tarotcards.jpg -bordercolor black -crop 14x6@ -fuzz 50% -trim $dest_dir/card_%02d.png 2>/dev/null
cd $dest_dir

#POST PROCESSING

nb_files=`ls -A1 card*png | wc -l`
num=0
idx=0
max_w=0
max_h=0
deleted=""
for file in card*png
do
	num=$((num+1))
	size=`stat -c%s $file`
	width=`identify -format "%w" $file`
	height=`identify -format "%h" $file`

	if [ $width -gt $max_w ]
	then
		max_w=$width
	fi

	if [ $height -gt $max_h ]
	then
		max_h=$height
	fi

	echo -n "post processing file [$file] ($num/$nb_files) | "
	echo -n `echo "scale=2;$num/$nb_files*100" | bc`%
	echo -n "\r"

	if test $size -lt 1000
		then #we delete empty files (areas without card on the initial picture)
			deleted="$deleted$file\n"
			rm -f $file
		else
			idx=$((idx+1))
			#We use transparency for the round corners
			mogrify -fuzz 80% -fill none -draw "matte  0,0 floodfill" \
										 -draw "matte  $((width-1)),0 floodfill"\
										 -draw "matte  0,$((height-1)) floodfill"\
										 -draw "matte  $((width-1)),$((height-1)) floodfill"\
										 $file
			#Time to rename the cards
			mv "$file" "$(get_card_name $idx).png"

		fi
done
echo "\nEmpty files deleted:\n$deleted"
echo "\nBiggest size: ${max_w}X${max_h}"
cd "$current"
echo "DONE :)"