Mahjongg builder - bash script


I like playing the Gnome games that come with most Linux setups, especially Mahjongg as a quick time waster.

I thought it would be really cool if I could create my own tile set, only when I opened the /usr/share/pixmaps/mahjongg/smooth.png image I found 42 tiles needed to be created.
example of Mahjongg tiles
What if I could use the huge store of digital images that have accumulated over the years as source material for a new tileset and even better, what if I could write a script that would do all the resizing and building automatically ?
collection of source imagesresult as a tileset
Welcome to Mahjongg_builder a bash script using Imagemagick.

How to operate the Mahjongg_builder script.

Download the script from the resources list.

Usage: -s <source-folder> [-d <destination-folder>] [-S] [-i <mahjongg-pixmap-folder>]

examples -s myimages -d result -S
Would try and take the images from myimages folder, save the results in results and run everything at normal size -s myimages
Would try and take the images from myimages folder, save the results in tmp and run everything at double size
(double size works best for photos as source images)

What does it take to create a Gnome Mahjongg tile set ?

1. 42 images

2. access to the original Gnome Mahjongg tileset.
This is used to get a blank tile, on which we mount each image.

3. each image resized and applied to a blank tile.

4. each image on its tile stuck sequentialy together making a long strip.

5. create and apply 'markers' defining special titles that match even though they are not an exact pair.

6. a copy of that strip coloured in some way that would make it appear 'selected' in the Mahjongg application.

7. stick the 'selected' strip on the bottom of the original tileset strip.

8. copy the final .png file to the Gnome Mahjongg pixmap folder.

Sounds easy doesn't it ?

Well it was not so bad, but to make those 8 steps into a bash script was a bit more tricky.

A brief look at the script

just tells any body who is interested that this script is designed to the executed by the bash shell.

# rednuht 03/2006
Any line starting with a hash(#) is a comment and is not executed.

while getopts "s:d:Si:" opt; do
Looks for the command line options -s/-d/-i followed by a parameter and -S as an option on its own.
If nothing is supplied then the /? kicks in a showes the usage message

if [ -z $sourceDir ]; then and if [ ! -d $sourceDir ]; then
Make sure the supplied source directory is not a blank string and the directory is accessable.

Quietly assigns a default value to the variable if none currenly exists.

count=`ls $sourceDir/*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null | wc -l`
Assign the numeric result of counting the number of lines/files returned by the ls command.(note back ticks ` not single quotes ')
2> /dev/null is required to hide any errors about certain file extensions not being found.

convert $installDir/smooth.png -crop 64x88+2688x0 +repage "$tempDir/blnk.png"
Crop out the blank tile that is part of the original tileset image.
By default we then double the size.

for image in $( ls $sourceDir*{.png,.jpg,.jpeg,.PNG,.JPG,.JPEG} 2> /dev/null ) 
	convert $image -resize $x"x"$y\!  $tempDir/TILE_${image:${#sourceDir}}
  echo -n "."
For each image found resize and save in the temp directory.
The ${image:${#sourceDir}}removes the path information from the file name, or more the point it takes a new string from $image starting at the nth character, being the length of $sourceDir, or maybe its just magic.
The -n in the echo means we end up with a nice list of dots as a progress bar.

composite -geometry +$offX+$offY $image "blnk.png" -background none $image
Creates a new image that is built up from the blank image with the new image applied at a offset.

montage strip.png $image -geometry +0+0 -background none strip.png
Sticks each image sequentialy together to create one long strip.

convert +antialias -size 15x15 xc:none -draw 'circle 7,7 3,3' ball.png
Creates a ball image out of fresh air.
the buildBullet() script then kicks in and makes it nicely shaded.
taken from

composite -geometry +$loc+5 red_ball.png strip.png -background none strip.png
Apply the marks to the right tiles.

convert selected.png -fill yellow -colorize 25% selected.png
Smudge yellow all over a copy of the tiles.

montage $tempDir/strip.png $tempDir/selected.png -tile 1x2 -geometry +0+0 -background none $destDir/tileset.png
Add the yellowy tiles onto the bottom of the tileset and we are all done.

When assigning variables make sure there are no spaces.
var = "value" - Wrong !
var="value" - Right.
If conditions must have spaces, due to historicaly actual commands being run.
if [$var="value"]; then - Wrong !
if [ $var = "value" ]; then - Right.


the Mahjongg_builder script file.
Gnome Games.
ImageMagik homepage
Lots of detailed examples for using imagemagik, invaluable.
The code for the buildBullet function.



This page is by me for me, if you are not me then please be aware of the following
I am not responsible for anything that works or does not work including files and pages made available at I am also not responsible for any information(or what you or others do with it) available at
In fact I'm not responsible for anything ever, so there!

[Pay4Foss banner long]