Friday, June 02, 2006

Making Pretty Pictures

I was recently faced with the problem of converting a single-page PDF document into a nice high resolution image.  Now I'm sure some company out there has created a fancy expensive tool to do this quickly and easily.  However, I am not the type to spend money on such tools.  Also, I was doing this at work on my Linux box, and chances are most software wont work on this platform.  The question is, are there any tools available for Linux to easily accomplish this?  Turns out not surprisingly the answer is yes.  The ghostcript tool GS is very well suited to accomplish this task.  Just give it the resolution, input file, output file and output type and you get an image with no hastles.
gs -r300 -dBATCH -dNOPAUSE -sDEVICE=jpeg -sOutputFile=moo.jpg moo.ps
I created a nice ruby script to gather the relavant options and run GS for me:

# Need to create commands like this:
#
# gs -r300 -dBATCH -dNOPAUSE -sDEVICE=jpeg
# -sOutputFile=arp-activity-per-20-minute-interval.jpg
# arp-activity-per-20-minute-interval.ps


if ARGV.length < 3
  puts "Usage: ruby imageconvert.rb <input> <output> <output format> <dpi>"
  exit
end

# Get the command line arguements we need
input = ARGV[0]
output = ARGV[1]
format = ARGV[2]
dpi = ARGV[3]

if (!dpi)
  dpi = 100
end

puts "Input file: #{input}"
puts "Output file: #{output}"
puts "Output format: #{format}"

puts "Running: gs -r#{dpi} -dBATCH -dNOPAUSE -sDEVICE=#{format} -sOutputFile=#{output} #{input}"
system "gs -r#{dpi} -dBATCH -dNOPAUSE -sDEVICE=#{format} -sOutputFile=#{output} #{input}"