Commit e1bcbcec authored by Peter Billam's avatar Peter Billam

initial release

parent 53435b4f
-- This Lua5 script is Copyright (c) 2018, Peter J Billam --
-- --
-- This script is free software; you can redistribute it and/or --
-- modify it under the same terms as Lua5 itself. --
local Version = '1.0 for Lua5'
local VersionDate = '11apr2018';
local Synopsis = [[
eps2img input.esp output.jpg
# output file can be a .jpg or .png
eps2img -t input.esp output.png
# gives .png files a Transparent background
eps2img -s 1.5 input.esp output.png
# multiplies the Size by 1.5 times
Transparent = false
Size = 1.0
local iarg=1; while arg[iarg] ~= nil do
if not string.find(arg[iarg], '^-[a-z]') then break end
local first_letter = string.sub(arg[iarg],2,2)
if first_letter == 'v' then
local n = string.gsub(arg[0],"^.*/","",1)
print(n.." version "..Version.." "..VersionDate)
elseif first_letter == 's' then -- change geometry also ?
iarg = iarg + 1
Size = tonumber( arg[iarg] )
elseif first_letter == 't' then
Transparent = true
local n = string.gsub(arg[0],"^.*/","",1)
print(n.." version "..Version.." "..VersionDate.."\n\n"..Synopsis)
iarg = iarg+1
function round(x) return math.floor(x+0.5) end
function warn(...)
local a = {}
for k,v in pairs{...} do table.insert(a, tostring(v)) end
io.stderr:write(table.concat(a),'\n') ; io.stderr:flush()
function die(...) warn(...); os.exit(1) end
Input = arg[iarg]
if not Input then die('must specify input file') end
Output = arg[iarg+1]
if not Output then die('must specify output file') end
ext2dev = { jpg='jpeg', png='png16m', JPG='jpeg', PNG='png16m' }
extension = string.match(Output,'%.(%a%a%a)$')
if not extension then die('unrecognised extension in file ',Output) end
if Transparent then
if extension == 'png' then
Device = 'pngalpha'
die('-t option only makes sense with a .png output-file')
Device = ext2dev[extension]
if not Device then die('unrecognised extension ',extension) end
if Input == '-' then
Inp = io.stdin
Inp, msg =, 'r')
if not Inp then die("can't read input file ",Input," - ",msg) end
local eps = Inp:read('a')
-- print (eps)
local Width, Height
= string.match(eps, '%%BoundingBox:? 0 0 ([%d.]+)%s([%d.]+)')
-- print(Width, Height) ; os.exit()
if not Width or not Height then
if string.match(eps, '%%BoundingBox') then
die('sorry, eps2img needs the %%BoundingBox to start at "0 0 "')
die("can't find a '%%BoundingBox 0 0 width height' line in ",Input)
-- from /home/pbin/eps2png
--if (!open(P,"|$gs -sDEVICE=$dev -sOutputFile=$tmp -q -g${x}x${y} -r$res -")){
-- die "sorry, can't run $gs: $!\n";
-- where dev is either jpeg or png16m or (if transparent) pngalpha
Resolution = tostring(72 * Size)
Height = tostring(round(tonumber(Height) * Size))
Width = tostring(round(tonumber(Width) * Size))
local gs_cmd = 'gs -sDEVICE='..Device..' -sOutputFile='..Output..
' -q -g'..tostring(Width)..'x'..tostring(Height)..' -r'..Resolution..' -'
local GS = assert(io.popen(gs_cmd, 'w'))
=head1 NAME
eps2img - Converts an .eps ExtendedPostScript into a .png or .jpg image
eps2img input.esp output.jpg
# output file can be a .jpg or .png
eps2img -s 1.5 input.esp output.png
# output file Size 1.5 times larger
eps2img -t input.esp output.png
# gives .png files a Transparent background
This script converts an I<.eps> Extended PostScript file
into a I<.png> or I<.jpg> image.
The B<-s> option allows the size of the image to be scaled,
and any enlargement takes place with no loss of clarity.
The B<-t> option allows I<.png> images to be given a transparent background.
It only works if the I<%%BoundingBox> line starts at "0 0"
so it does not work on all I<.eps> files.
=over 3
=item I<-s 1.5>
Sets the output file B<S>ize 1.5 times larger than default.
By default, the conversion is one image-I<pixel> to one EPS I<point>.
=item I<-t>
Gives .png files a B<T>ransparent background.
By default, the background is white.
This option does not work for I<.jpg> files.
=item I<-v>
Print the Version
This at is available in
git clone
or at
It also needs I<gs> to be installed, if it isn't already:
apt-get install ghostscript
or see:
=head1 AUTHOR
Peter J Billam,
=head1 SEE ALSO
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment