#!/usr/bin/perl ## PURPOSE: ## create single ps file from multiple image files in a folder ## USE: ## images2ps [-s] [-r] [-h] [-o] [-p] [-rotate=XX] ## [-image=XX] [-align=l/r] [-width=YY | --scale=YY] folder ## ## --s silent mode (doesn't open gv) ## --r re-run latex without re-writting tex file ## --V only open the combined ps file ## --rotate=XX angle to rotate image ## --align=l/r alignment to filename, might need to be ## altered to align image to name ## --width=YY width of image relative to text width (1 is text width) ## --scale=YY scale an image on page ## --image=XX type of image that is in folder, ie eps,jpg ## CAVEAT: ## image files must all be the same ## 21/02/07 - changed to perl ## 20/02/08 - added ability to convert all pictures using 'convert' ## - changed name ## - general tidying up ## 21/02/08 - added scale parameter use warnings; use strict; use Getopt::Long; ## process arguments my $silent=0; my $rerun=0; my $open=0; my $help=0; my $rotate=0; my $pdf=0; my $align='l'; my $width=0.8; my $scale; my $folder=''; my $image_type='eps'; my $argret = GetOptions ( ## just incase I want long options later ;) "s!" => \$silent, #"silent!"=> \$silent, "r!" => \$rerun, #"rerun!"=> \$rerun, "o!" => \$open, #"open!"=> \$open, "p!" => \$pdf, #"pdf!" => \$pdf, "h!" => \$help, #"help!"=> \$help, "rotate=s" => \$rotate, "align=s" => \$align, "width=s" => \$width, "scale=s" => \$scale, "image=s" => \$image_type, ); #"folder=s"=> \$folder, if($#ARGV < 0) { $help=1; } if($help) { die qq| images2ps [-s] [-r] [-h] [-rotate=XX] [-align=l/r] [-width=YY \| -scale=YY] folder --h print this help --s silent mode (doesn't open gv) --r re-run latex without re-writting tex file --o only open the combined ps file --p make pdf file --rotate=XX degrees to rotate image --align=l/r alignment to filename, might need to be altered to align image to name --width=YY width of image relative to text width (1 is text width) --scale=YY scale image --image=XXX type of image in folder, ie eps, jpg |; } $folder=$ARGV[0]; if(! -e $folder) { die "Enter a valid folder containing images\n"; } ## setup image insert options my $options; if(defined($rotate)) { $options = $options . "angle=$rotate," } if(defined($scale)) { $options = $options . "scale=$scale," } elsif(defined($width)) { $options = $options . "width=${width}\\textwidth," } ## remove comman after last option $options =~ s/,\]/\]/; ## move into folder to create latex file chdir $folder; ## write the tex file here if(($rerun == 0) and ($open == 0)) { ## get list of images of type $image_type to insert opendir(DIR, ".") or die "Can't read directory; $!"; my @imageFile = grep { /\.${image_type}/ && ! /ps2tex/ } readdir(DIR); #find images and convert to '.eps' if($image_type !~ 'eps') { foreach my $non_eps (@imageFile) { $non_eps =~ s@(.*)\.${image_type}@$1@; system("convert $non_eps.${image_type} ${non_eps}.eps"); print "Converting $non_eps.$image_type to eps image...\n"; } } ## get list of eps images to insert opendir(DIR, ".") or die "Can't read directory; $!"; my @psfiles = grep { /\.eps/ && ! /ps2tex/ } readdir(DIR); closedir(DIR); ## write tex file open(LATEX, ">ps2tex.tex") or die "Can't write to ps2tex.tex: $!"; print LATEX qq/ \\documentclass{article} \\usepackage[dvips,a4paper]{geometry} \\usepackage{graphicx} \\usepackage{rotating} \\usepackage{calc} %% will be specific to size of object to be used \\setlength{\\topmargin}{-0.5in} \\setlength{\\textheight}{12.0in} \\begin{document} \\parindent0pt \\pagestyle{empty} \\center /; ## insert images in tex file foreach my $file (@psfiles) { (my $label = $file) =~ s/_/\\_/g; print LATEX qq/ %\\begin{turn}{0} \\begin{tabular}{c c} \\includegraphics[${options}]{${file}} & \\rotatebox[origin=${align}]{90}{${label}} \\\\ \\vspace{10mm} \\end{tabular} %\\end{turn} /; } print LATEX qq/ \\end{document} /; close(LATEX); } ## re-run latex/dvips if($open == 0) { ## create the ps file system("latex","ps2tex.tex"); system("dvips","ps2tex.dvi"); } ## don't open ps file if($silent == 0) { system("gv","ps2tex.ps"); } if($pdf) { system("ps2pdf","ps2tex.ps"); }