Skip to content

Instantly share code, notes, and snippets.

@snhobbs
Created February 25, 2023 14:16
Show Gist options
  • Save snhobbs/85aaf8b2750a3a163d3579257b71d124 to your computer and use it in GitHub Desktop.
Save snhobbs/85aaf8b2750a3a163d3579257b71d124 to your computer and use it in GitHub Desktop.
Inkscape label generation makefile

I've been needing to add labels to our products at Hobbs ElectroOptics and have been suffering through keeping multiple versions of each label up to date. In the spirit of spending twice as long to script it as it takes normally I've been investigating using Inkscape's command line tool to batch process these.

Requirements

Text to path

A master version of the label with text boxes is useful for editing but can be a headache if the font isn't installed on the target computer. Exporting to paths means that wherever this SVG goes it will look the same as the original design.

inkscape ${FILENAME_IN} --export-text-to-path -o ${FILENAME_OUT}

Export a laser engraving visualization

This is more ambitious than text to path. I'm laser engraving the labels in some objects and printing labels for others. This should:

  • Remove the laser engraving sections by color (Outline in red, guide markings in Blue, engraving in Black).
  • Set the background color
  • Set the engraved color

Set Background Colour

inkscape ${FILENAME_IN} --export-background=#ff0f00 -o ${FILENAME_OUT}.png

Remove Colours

This is less pretty, I'm using sed to change the XML file directly setting the colour you want to remove to the background colour.

sed s/#000000/#ffffff/ ${FILENAME_IN}.svg >> ${FILENAME_OUT}.svg

Putting it together

FILENAME_IN=in.svg
FILENAME_OUT=out
BLACK=#000000
BACKGROUND=${BLACK}
BLUE=#0000ff
RED=#ff0000
WHITE=#ffffff
sed s/${BLUE}/${BACKGROUND}/ ${FILENAME_IN} > ${FILENAME_OUT}.svg
sed s/${RED}/${BLACK}/ ${FILENAME_OUT}.svg > ${FILENAME_OUT}.svg
inkscape ${FILENAME_OUT}.svg --export-text-to-path -o ${FILENAME_OUT}.svg
inkscape ${FILENAME_OUT}.svg --export-background=${BACKGROUND} -o ${FILENAME_OUT}.png

Makefile

FILENAME_IN=label.svg
FILENAME_OUT=$(basename ${FILENAME_IN})-export
BLACK=\#000000
BACKGROUND=${BLACK}
BLUE=\#0000ff
RED=\#ff0000
YELLOW=\#ff00ff
WHITE=\#ffffff
DPI=1024


# Use Black for engraving
# Red for cutting

.PHONY: all
all: ${FILENAME_OUT}.png ${FILENAME_OUT}-preview.svg ${FILENAME_OUT}-engrave.svg ${FILENAME_OUT}-engrave-all.svg ${FILENAME_OUT}-path.svg ${FILENAME_OUT}-outline.svg


clean:
	rm ${FILENAME_OUT}*.png ${FILENAME_OUT}*.svg

${FILENAME_OUT}-path.svg: ${FILENAME_IN} 
	inkscape $< --export-text-to-path -o $@

# 
${FILENAME_OUT}-outline.svg: ${FILENAME_OUT}-path.svg
	cat $< | sed s/${BLUE}/${WHITE}/ | sed s/${BLACK}/${WHITE}/ > $@

# Remove red and blue leaving engraving only
${FILENAME_OUT}-engrave.svg: ${FILENAME_OUT}-path.svg
	cat $< | sed s/${BLUE}/${WHITE}/ | sed s/${RED}/${WHITE}/ > $@


# Turn all guides and cutting into black
${FILENAME_OUT}-engrave-all.svg: ${FILENAME_OUT}-path.svg
	cat $< | sed s/${BLUE}/${BLACK}/ | sed s/${RED}/${BLACK}/ > $@


${FILENAME_OUT}-preview.svg: ${FILENAME_OUT}-path.svg
	cat $< | sed s/${BLACK}/${WHITE}/ | sed s/${BLUE}/${BACKGROUND}/ | sed s/${RED}/${BACKGROUND}/ > $@


${FILENAME_OUT}.png: ${FILENAME_OUT}-preview.svg
	inkscape $< --export-dpi=${DPI} --export-background=${BACKGROUND} -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment