Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
a small utility script to rasterize an illustrator file with ghostscript and graphicsmagick
""" - (c) 2012 Marcos Ojeda <>
--"Quis leget haec?"
graphicsmagick - brew install graphicsmagick
ghostscript - brew install ghostscript
import os
import sys
import subprocess
import tempfile
def popen_results(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return proc.communicate()[0]
def convert_png(source, output, page=None, size=None, dpi=72):
"""convert an ai file to a transparent png
dpi/density is 72 by default
page is 1-indexed and if set, will output only a single artboard
If you specify size, you should also specify a dpi to match
for context, a dpi for preventing jaggy edges isin the 1200-2400 range!
basename = source.split(".")[0]
base_output = output.split(".")[0]
command = ['gs',
'-q', '-dNOPAUSE', '-dBATCH', '-dNOPROMPT',
'-sDEVICE=pngalpha', '-dEPSCrop',
'-dTextAlphaBits=4', '-dGraphicsAlphaBits=4',
'-r%s' % dpi]
if page:
command.extend(['-dFirstPage=%s' % page,'-dLastPage=%s' % page])
if size:
temp = tempfile.mktemp()
command.append('-sOutputFile=%s.png' % temp)
if page:
command.append('-sOutputFile=%s.png' % base_output)
command.append('-sOutputFile=%s-%%02d.png' % base_output)
# run the command
# if size is specified, route through graphicsmagick
if size:
command = ['gm', 'convert',
'-background', 'transparent',
'-resize', size, '-size', size,
'%s.png' % temp, output]
res = popen_results(command)
# clean out the temp file
os.remove("%s.png" % temp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment