I use this to Makefile rule create PNG files and thumbnails from .tex files that contain one tikzpicture each. It works similar to the creation of images at TeXample.net:
%.png: %.tex
@sed 's/^\\begin{document}/\
\\pgfrealjobname{dummy}\\begin{document}\\beginpgfgraphicnamed{example}/' $< | \
sed 's/^\\end{document}/\\endpgfgraphicnamed\\end{document}/' > example.tex ; \
pdflatex --jobname=example example.tex ; \
gs -dNOPAUSE -r120 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sDEVICE=png16m \
-sOutputFile=$@ -dBATCH example.pdf ; \
convert -thumbnail 200 $@ $(addsuffix .thumb.png, $(basename $@)) ; \
mv example.pdf $(addsuffix .pdf, $(basename $<)) ; rm example.*
ghostview (gs) can probably be replaced by convert and you can replace example with another tempfile prefix.