Skip to content

Instantly share code, notes, and snippets.

@agryson
Created March 18, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agryson/bab9edd751b9b039134a28654c7e445f to your computer and use it in GitHub Desktop.
Save agryson/bab9edd751b9b039134a28654c7e445f to your computer and use it in GitHub Desktop.
Exports pngs and svgs in current folder to a big spritesheet.
#!/bin/bash
# Creates a spritesheet of all png and svg images in the current folder in
# 16, 24, 32 and 64px on light, mid-grey and dark backgrounds.
#
# Requirements: Inkscape, Imagemagick, optipng
#
# Arguments: ["dev"] Optional, if present, will export and then get all the
# exported sheets and combine them in a global sheet.
# If absent, will simply export to pngs and create a light, dark and mid-grey spritesheet
# Optional string, "dev"
input=$1
# Create a list of all svg and png images in current folder
filelist=$(ls | egrep '\.svg$|\.png$')
# Purge the output folders
rm ~/Pictures/output/*.png
rm ~/Pictures/output/export/spritesheet.png
for file in $filelist
do
# Get the current filename, sans extension
filename=$(basename "$file")
filename="${filename%.*}"
# If it's a png convert it to all required sizes, otherwise use inkscape to export
if [[ ${file: -4} == ".png" ]]; then
echo "******************"
echo "Converting ${file}"
convert "${file}" -resize 16x16 "/home/alex/Pictures/output/${filename}_16.png"
convert "${file}" -resize 24x24 "/home/alex/Pictures/output/${filename}_24.png"
convert "${file}" -resize 32x32 "/home/alex/Pictures/output/${filename}_32.png"
convert "${file}" -resize 64x64 "/home/alex/Pictures/output/${filename}_64.png"
else
/usr/bin/inkscape -z "${file}" -w 16 -h 16 -e "/home/alex/Pictures/output/${filename}_16.png"
/usr/bin/inkscape -z "${file}" -w 24 -h 24 -e "/home/alex/Pictures/output/${filename}_24.png"
/usr/bin/inkscape -z "${file}" -w 32 -h 32 -e "/home/alex/Pictures/output/${filename}_32.png"
/usr/bin/inkscape -z "${file}" -w 64 -h 64 -e "/home/alex/Pictures/output/${filename}_64.png"
fi
done
# Get the exported pngs and stitch them into spritesheets by backgroudn color.
# If the optional "dev" argument was given, consider the job finished and
# stitch all those resulting spritesheets into one big one, then optimize.
if [[ "$input" == "dev" ]]; then
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background snow1 ~/Pictures/output/export/B.png
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background snow4 ~/Pictures/output/export/D.png
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background gray17 ~/Pictures/output/export/F.png
/usr/bin/montage ~/Pictures/output/export/*.png -geometry +3+3 -tile 6x -background gray17 ~/Pictures/output/export/spritesheet.png
/usr/bin/optipng -o7 ~/Pictures/output/export/spritesheet.png
else
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background snow1 ~/Pictures/output/export/A.png
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background snow4 ~/Pictures/output/export/C.png
/usr/bin/montage ~/Pictures/output/*.png -geometry +3+3 -tile 4x -background gray17 ~/Pictures/output/export/E.png
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment