Skip to content

Instantly share code, notes, and snippets.

@onetransistor
Last active June 9, 2018 12:37
Show Gist options
  • Save onetransistor/26e9a62dbb86c892e773f714842ed972 to your computer and use it in GitHub Desktop.
Save onetransistor/26e9a62dbb86c892e773f714842ed972 to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################################################
#
# Generate nice looking PCB overlays from KiCad PDF plots
# Requirements: Ghostscript and ImageMagick
#
# Usage:
#
# chmod a+x ./nice.sh
# ./nice.sh /path/to/kicad/project/folder
#
################################################################################
shopt -s nullglob
if [ -z $1 ]; then
echo "Using directory" `pwd`
else
cd $1
echo "Using directory" $1
fi
for f in *.pdf;
do
echo "Converting $f to PNG"
gs -q -r200x200 -dNOPAUSE -dBATCH -sDEVICE=pngalpha -sOutputFile=`basename $f .pdf`.png $f
done
for g in *Cu.png; do
echo "Processing Copper layer file $g"
convert $g -channel R +level 20%,100% -channel G +level 75%,100% -channel B +level 40%,100% -flatten $g.tmp.png
mv $g.tmp.png $g
# get corresponding silkscreen
TMPNAME=`basename $g .Cu.png`
LAYER=${TMPNAME:${#TMPNAME}-1:1}
NAME=${TMPNAME::${#TMPNAME}-2}
if [ $LAYER = "F" ]; then
SSFILE=$NAME"-B.SilkS.png"
NLAYER="B"
else
SSFILE=$NAME"-F.SilkS.png"
NLAYER="F"
fi
if [ -a $SSFILE ]; then
echo "Using silkscreen file" $SSFILE
composite $SSFILE $g $NAME-$NLAYER.nice.tmp.png
convert $NAME-$NLAYER.nice.tmp.png -trim $NAME-$NLAYER.nice.png
rm $NAME-$NLAYER.nice.tmp.png
rm $SSFILE
else
echo "Silkscreen file" $SSFILE "does not exist"
fi
rm $g
done
# If there is any schematic plot, let's make a PNG from it
# Will not work without PCB plots because $NAME is determined from PCB plot files
if [ -a $NAME.pdf ]; then
echo "Processing schematic plot"
# I don't think this is needed anymore
# gs -q -r200x200 -dNOPAUSE -dBATCH -sDEVICE=pngalpha -sOutputFile=$NAME.png $NAME.pdf
convert $NAME.png -flatten -trim $NAME.tmp.png
mv $NAME.tmp.png $NAME.png
fi
cd `pwd`
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment