User:BarracudaATA/Scripts

Some scripts I've wrote to automate repetitive tasks when working on Minepedia.

Sprite Splitter
A shell script to extract all 16x16 sprites from items.png with ImageMagick:

 INPUT="items.png" OUTPUT="items_16x16"
 * 1) !/bin/sh

if [ ! -d $OUTPUT ]; then mkdir $OUTPUT fi
 * 1) create output dir

convert -crop 16x16 +repage $INPUT $OUTPUT/item_%02d.png
 * 1) extract all sprites from items.png to 16x16 images

find $OUTPUT -size -300c -delete
 * 1) delete empty sprites

Sprite Upscaler
A shell script to scale up all sprites in a folder to 150x150 with ImageMagick:

 INPUT="items_16x16" OUTPUT="items_150x150"
 * 1) !/bin/sh

if [ ! -d $OUTPUT ]; then mkdir $OUTPUT fi
 * 1) create output dir

for x in `ls $INPUT/*.png`; do   OUTPUT_FILE=$OUTPUT/`basename $x` # trim transparent pixels, then upscale and extent to 150x150 convert -trim -sample 150x150 -gravity center -background transparent -extent 150x150 $x $OUTPUT_FILE # optional: optimize with optipng optipng $x > /dev/null echo "Processed $x"; done

Pixel to SVG converter
A Perl script to convert small 8-bit PNG images into SVG vector images. Requires Perl GD library.


 * 1) !/usr/bin/perl

use strict; use GD;

if (!defined($ARGV[0])) { die "Usage: pixelvector.pl \n" };

my $imageFile = $ARGV[0];

my $image = GD::Image->newFromPng($imageFile); my $alpha = $image->transparent; my $pixelScale = 10; my $sourceWidth = $image->width; my $sourceHeight = $image->height; my $destWidth = $sourceWidth * $pixelScale; my $destHeight = $sourceHeight * $pixelScale;

my $viewBox = "0 0 $destWidth $destHeight";

print ' '."\n";

for (my $i = 0; $i < $sourceWidth; $i++) { for (my $j = 0; $j < $sourceHeight; $j++) { my $color = $image->getPixel($i, $j); # skip transparent pixels if ($color == $alpha) { next; }       my ($r,$g,$b) = $image->rgb($color); my $rgbHex = sprintf("%02x%02x%02x", $r, $g, $b); my $x = $i * $pixelScale; my $y = $j * $pixelScale; print ''."\n"; } }

print ' '."\n";