Skip to content

Instantly share code, notes, and snippets.

@nsfmc
Created April 3, 2012 18:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nsfmc/2294621 to your computer and use it in GitHub Desktop.
Save nsfmc/2294621 to your computer and use it in GitHub Desktop.
a small utility script to rasterize an illustrator file with ghostscript and graphicsmagick
"""
doodler.py - (c) 2012 Marcos Ojeda <marcos@khanacademy.org>
--"Quis leget haec?"
requires
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)
else:
if page:
command.append('-sOutputFile=%s.png' % base_output)
else:
command.append('-sOutputFile=%s-%%02d.png' % base_output)
command.append(source)
# run the command
popen_results(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)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment