Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like a simple script to "scrape" every image in a directory into a single beamer presentation as follows:

$ ls 

%img1.jpg

%img2.jpg

%img3.jpg


$ *run script*

%OUTPUT SOMETHING LIKE THIS:

\documentclass{beamer}

% for themes, etc.
\mode<presentation>
{ \usetheme{boxes} }

\usepackage{times}  % fonts are up to you
\usepackage{graphicx}

\section{Images in this Directory}

\begin{frame}
\frametitle{img1.jpg}
\begin{center}
        \includegraphics[width=4in]{img1.jpg}
\end{center}
\end{frame}

\begin{frame}
\frametitle{img2.jpg}
\begin{center}
        \includegraphics[width=4in]{img2.jpg}
\end{center}
\end{frame}


\begin{frame}
\frametitle{img3.jpg}
\begin{center}
        \includegraphics[width=4in]{img3.jpg}
\end{center}
\end{frame}

\end{document}
share|improve this question
    
welcome to tex.sx You can indent by 4 spaces (or use the {} button) to format code sections –  David Carlisle Jan 23 '13 at 2:41
    
depending on what you're trying to do, the animate package might be helpful here –  craq Aug 20 at 9:46

3 Answers 3

Option 1

Here's a perl script that can be used in the current directory:

perl createslides.plx

or if you make it executable and add it to your path, then simply

createslides.plx

You can specify the image extensions in

my %imgextensions=("png"=>1,"jpg"=>1);

and turn them on and off with a 1 or 0 respectively

createslides.plx

#!/usr/bin/perl

use strict;
use warnings;

# list extensions to work with
my %imgextensions=("png"=>1,"jpg"=>1);

# open the current directory
my $dir = './';
opendir(DIR, $dir) or die $!;

my @lines=();               # @lines: stores lines for beamer
my $extension ='';

# setup the documentclass and preamble
push(@lines,"\\documentclass{beamer}\n");
push(@lines,"% for themes, etc.\n");
push(@lines,"\\mode<presentation>\n");
push(@lines,"{ \\usetheme{boxes} }\n");
push(@lines,"\\usepackage{graphicx}\n");
push(@lines,"\\begin{document}\n");
push(@lines,"\\section{Images in this Directory}\n");

# loop through filenames
while (my $filename = readdir(DIR))
{
   # get the file extension
   $filename =~ m/\.(.*)$/;
   $extension=$1;
   if(scalar($imgextensions{$extension}))
   {
        push(@lines,"\\begin{frame}\n");
        push(@lines,"\\frametitle{$filename}\n");
        push(@lines,"\\begin{center}\n");
        push(@lines,"\\includegraphics[width=4in]{$filename}\n");
        push(@lines,"\\end{center}\n");
        push(@lines,"\\end{frame}\n");
    }
}

# close directory
closedir(DIR);

# end the documentclass
push(@lines,"\\end{document}\n");
print(@lines);

# create slides.tex
open (MYFILE, '>slides.tex');
print MYFILE @lines;
close (MYFILE); 

exit

Option 2 (first attempt, not as good as option 1)

Here's a perl script to automate the task, which you can save as (for example) createslides.plx

You can call it with:

find . -type f \( -name "*.jpg" \) -print0|xargs -0 perl createslides.plx 

or else with the following if you have multiple extensions:

find . -type f \( -name "*.png" -or -name "*.jpg" \) -print0|xargs -0 perl createslides.plx 

createslides.plx

#!/usr/bin/perl

use strict;
use warnings;

my $filename='';
my @lines=();               # @lines: stores lines for beamer

# setup the documentclass and preamble
push(@lines,"\\documentclass{beamer}\n");
push(@lines,"% for themes, etc.\n");
push(@lines,"\\mode<presentation>\n");
push(@lines,"{ \\usetheme{boxes} }\n");
push(@lines,"\\usepackage{graphicx}\n");
push(@lines,"\\begin{document}\n");
push(@lines,"\\section{Images in this Directory}\n");

# loop through filenames
while (@ARGV)
{
    # get filename from arguments
    $filename = shift @ARGV; 

    push(@lines,"\\begin{frame}\n");
    push(@lines,"\\frametitle{$filename}\n");
    push(@lines,"\\begin{center}\n");
    push(@lines,"\\includegraphics[width=4in]{$filename}\n");
    push(@lines,"\\end{center}\n");
    push(@lines,"\\end{frame}\n");
}

# end the documentclass
push(@lines,"\\end{document}\n");
print(@lines);

# create slides.tex
open (MYFILE, '>slides.tex');
print MYFILE @lines;
close (MYFILE); 

exit

It will create a file slides.tex- a sample output is included below:

\documentclass{beamer}
% for themes, etc.
\mode<presentation>
{ \usetheme{boxes} }
\usepackage{graphicx}
\begin{document}
\section{Images in this Directory}
\begin{frame}
\frametitle{./Tux.jpg}
\begin{center}
\includegraphics[width=4in]{./Tux.jpg}
\end{center}
\end{frame}
\end{document}
share|improve this answer
    
+1 for using perl. –  Yiannis Lazarides Jan 23 '13 at 4:53

The function \multiinclude from the package xmpmulti may be able todo this for you. I may be mistaken, but I beleive you would do this by using the command \multiinclude[<+>][format=jpg,graphics={width=\textwidth}]{img}. Name the files 'img-0.jpg', 'img-1.jpg', 'img-2.jpg'... etc.

Example:

\documentclass{beamer}
\usepackage{xmpmulti}

\begin{document}

\begin{frame}{Frame Title Here}
\multiinclude[<+>][format=jpg,graphics={width=\textwidth}]{img}
\end{frame}

\end{document}

In this example the image files are placed in the same folder as the main file. Three silly picture files to try with the example may be found at https://www.dropbox.com/sh/rtsm7ypcdcxza17/6NySKH_yB8

Some more info on \multiinclude may be found in section 14.1.3 of the Beamer User's Guide. The guide should be in the ..\doc\latex\beamer\doc folder of your LateX installation.

share|improve this answer
1  
Hi Martijn, welcome to tex.SE! Maybe you could extend your answer by a MWE that illustrates this approach? –  Daniel Jan 23 '13 at 7:39

Reinhard Kotucha once send me a perl script he as created to process all image files (*.jpg, *.png, *.pdf) and putting it into a pdf-file, one image per page. It does not use the beamer package, but it really worked well for me, several times, maybe it does for you, too.

pdfcatdir

#!/usr/bin/env perl

## pdfcatdir
## Copyright 2006 Reinhard Kotucha <reinhard.kotucha@web.de>
#
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# 
# The current maintainer is Reinhard Kotucha.

my $version="20090305";

print STDERR "This is pdfcatdir, Version $version\n"; 
use Getopt::Long;
$Getopt::Long::autoabbrev=0;
Getopt::Long::Configure ("bundling");

sub usage {
    print <<'EOF'
usage: 
    pdfcatdir [options] <directory1> [<directory2> ...]

    Concatenate all PDF, PNG, or JPEG files in directory <directory>.
    The name of the resuling file will be "<directory>.pdf".

    Options:

        -a | --author
       Add an author entry to the info dictionary.

        -d | --debug
       Do not delete temporary files.

    -f | --fullscreen
       Start browser in fullscreen mode.

    -h | --help
       Display this message.

    -k | --keywords
       Add keywords to the info dictionary.

    -r | --replacefonts
       Replace fonts if possible.

    -s | --subject
       Add a subject entry to the info dictionary.

    -t | --title
       Add a title entry to the info dictionary.

    -v | --verbose
       Verbose output to screen.

    Files:

    pdfcatdir first reads a file "pdfcatdir.info" if it exists.
    Then it reads a file "<directory>.info" if it exists.
    Only data set in "<directory>.info" will overwrite data set 
    in "pdfcatdir.info".  Everything can be overwritten by 
    command-line arguments.

        Syntax:

        <key>: <value>

        <key> is case insensitive.

        Valid keys are Author, Title, Subject, Keywords.
        Everything after "#" or "%" is ignored.

    You can use pdfcatdir to create a slide show from a directory
    which contains images produced by digital cameras.  It is sometimes
    necessary to rotate individual images.  For this purpose pdfcatdir
    looks for a file "<directory>.rotate".

        Syntax:

        <filename>: <rot>

        <filename> is case sensitive.  It shouldn't contain the directory
        name.  <rot> is an integer number which denotes steps of 
        90 degrees.  Positive numbers mean counterclockwise rotation.
        Everything after "#" or "%" is ignored.

EOF
;#'
exit 1;
}


GetOptions 
    "author|a=s",
    "fullscreen|f",
    "help|h",
    "keywords|k=s",
    "replacefonts|r",
    "subject|s=s",
    "title|t=s",
    "debug|d",
    "verbose|v";

usage if $opt_help;
usage if (@ARGV < 1);
$^W=1; ### if $opt_debug;

sub debug {
    my $foo=shift;
    print STDERR "DEBUG: $foo" if $opt_debug;
}


# Read the TeX code below __DATA__ into an array.
# We probably need it more than once.

while (<DATA>) {
    push @saved_texcode, $_;
}

# Read a file "<dirname>.rotate" and create a hash. 

sub get_rotates {
    my $dirname=shift;
    my %rotate=();
    if (-f "$dirname.rotate") {
    open ROTATE, "$dirname.rotate";
    while (<ROTATE>) {
        my ($key, $value);
        s/(#|%).*//g; 
        next if /^\s*$/;
        ($key,$value)=/(.*?):\s*(.*)/;
        $rotate{$key}=((($value+1)%4)-1)*90;
    }
    close ROTATE;
    }
    return %rotate;
}

# Prosess the PDF info files.  

sub eval_info_file {
    my ($key, $value);
    s/(#|%).*//g;
    unless (/^\s*$/) {
    ($key,$value)=/(.*?):\s*(.*)/;
    $key=lc($key);
    $_=$value;
    s/\s+/ /g;
    s/\s*,\s*/, /g;
    $value=$_;
    $info{$key}=$value;
###    print "$key -> $value\n";
    }
}

# These values will appear in the pdfinfo dictionary unless they will
# be overwrirtten by a file "pdfcatdir.info" or "<dirname>.info".

sub set_pdfinfo_defaults {
    %info=();
    $info{"author"}="This file had been produced automatically by pdftex."; 
    $info{"title"}=""; 
    $info{"subject"}=""; 
    $info{"keywords"}=""; 
}

# If a file "pdfcatdir.info" exists, variables set in this file will
# overwrite default values.

sub set_pdfinfo_pdfcatdir {
    if (-f "pdfcatdir.info") {
    open INFO, "pdfcatdir.info" or die "Can't open pdfcatdir.info";
    while (<INFO>) {
        eval_info_file; 
    }
    close INFO;
    }
}

# If a file "<dirname>.info" exists, variables set in this file will
# overwrite default values and variables set in "pdfcatdir.info".

sub set_pdfinfo_dirname {
    my $dirname=shift;
    if (-f "$dirname.info") {
    open DIRINFO, "$dirname.info";
    while (<DIRINFO>) {
        eval_info_file; 
    }
    close DIRINFO;
    }
}

# Every variable in the pdfinfo dictionary can be overwritten by a 
# command-line argument.

sub set_pdfinfo_cli {
    $info{"author"}=($opt_author) if $opt_author;
    $info{"title"}=($opt_title) if $opt_title;
    $info{"subject"}=($opt_subject) if $opt_subject;
    $info{"keywords"}=($opt_keywords) if $opt_keywords;
}

# Determine the final values fir the PDF info dictionary.

sub set_pdfinfo {
    my $dirname=shift;
    set_pdfinfo_defaults;
    set_pdfinfo_pdfcatdir;
    set_pdfinfo_dirname "$dirname";
    set_pdfinfo_cli;
}

# Return a sorted list containing all usable files in a particular
# directory.

sub create_filelist {
    my $dirname=shift;
    my @allfiles=();
    opendir DIR, "$dirname" or die "Can't open directory '$dirname'";
    while ($_=readdir DIR) {
    next if /^\..*/; # skip all dot files
    next unless /\.(jpg|jpeg|pdf|png)$/i;
    next if -d $_; # skip subdirs
    push @allfiles, "$dirname/$_"
    }
    closedir DIR;
    my @sorted_files=sort @allfiles;
    return @sorted_files;
}

# Write a file "<dirname>.files".

sub write_file_list {
    my $dirname=shift;
    my @sorted_files=@_;
    open FILES, ">$dirname.files";
    for $pdf (@sorted_files) {
    ($pdfbasename=$pdf)=~s/.*\///;
    my $angle=0;
    if (exists $rotate{"$pdfbasename"}) {
        $angle=$rotate{"$pdfbasename"};
        debug "Rotate: '$dirname/$pdfbasename' by $angle degrees\n";
    }
    printf FILES "%03d:%s\n", $angle, $pdf;
    }
    close FILES;
}

# Create and process the TeX files.

sub write_tex_file {
    my $dirname=shift;
    debug "Open: \"$dirname.tex\".\n";
    open TEX, ">$dirname.tex" or die "Can't open \"dirname.tex\".\n";
    my $bookmark_mode=
    '\pdfcatalog{/PageMode/UseOutlines/PageLayout/SinglePage}';
    my $fullscreen_mode=
    '\pdfcatalog{/PageMode/FullScreen}';
    set_pdfinfo "$dirname";
    debug "PDF_Author:   $info{author}\n";
    debug "PDF_Title:    '$info{title}'\n";
    debug "PDF_Subject:  '$info{subject}'\n";
    debug "PDF_Keywords: '$info{keywords}'\n";
    my @texcode;
    for (@saved_texcode) {
    push @texcode, $_;
    }
    for (@texcode) {
    last if /__END__/;
    if ($opt_verbose) {
        s/\@interactionmode\@/\\def\\verbose{}\\errorstopmode/;
    } else {
        s/\@interactionmode\@/\\batchmode/;
    }
    if ($opt_replacefonts) {
        s/\@replacefonts\@//;
    } else {
        s/\@replacefonts\@/\\pdfmapfile{}/;
    }
    if ($opt_fullscreen) {
        s/\@pdfcatalog\@/$fullscreen_mode/;
    } else {
        s/\@pdfcatalog\@/$bookmark_mode/;
    }
    s/\@title\@/$info{title}/;
    s/\@author\@/$info{author}/;
    s/\@keywords\@/$info{keywords}/;
    s/\@subject\@/$info{subject}/;
    print TEX;
    }
    print TEX "\\input \"$dirname.files\"\n";
    print TEX '\errorstopmode\end', "\n";
    close TEX;
}

sub run_pdftex {
    my $dirname=shift;
    my @texcommand=qw(pdftex -ini);
    push @texcommand, "$dirname";

    if ($opt_debug) {
    my @commandlist;
    for my $entity (@texcommand) {
        push @commandlist, "[$entity]";
        $texcommand=join " ", @commandlist;
    }
    debug "TeX-command: $texcommand\n";
    }
    system @texcommand;
}

# find directories.

for my $dirname (@ARGV) {
    if ($dirname=~/\/$/) {
    $dirname=substr "$dirname", 0, length ($dirname) -1;
    }
    push @directories, $dirname if (-d $dirname);
} 

## main()

for my $dirname (@directories) {
    debug "Directory: '$dirname'\n";

    @sorted_files=create_filelist "$dirname";
    foreach $file (@sorted_files) {
    debug "File: '$file'\n";
    }
    %rotate=get_rotates "$dirname";
    if (@sorted_files >0) {
    write_file_list "$dirname", @sorted_files;
    write_tex_file "$dirname", @sorted_files;
    run_pdftex "$dirname";
    unlink ("$dirname.tex", "$dirname.log", "$dirname.files")
        unless $opt_debug;
    }
}
print "@texcode\n";
__DATA__
%% This is an automatically generated file. Do not change anything here.

\catcode`\{=1 \catcode`\}=2 \catcode`\#=6 \catcode`^=7 
\catcode`\^^M=13 \let^^M=\par
\pdfoutput=1
\pdfoptionpdfminorversion=4
\pdfcompresslevel=0
\pdfhorigin0pt 
\pdfvorigin0pt
\def\space{ }
\def\linewidth{70}
\endlinechar=-1 \newlinechar=`\^^J
@interactionmode@ 
@replacefonts@
@pdfcatalog@
\pdfinfo {
  /Title      (@title@)
  /Author     (@author@)
  /Subject    (@subject@)
  /Keywords   (@keywords@)
  /Creator    (pdfTeX and pdfcatdir)
}
\def\bar#1{\if#1m=\expandafter\bar\fi}
\edef\bar{\expandafter\bar\romannumeral\number\linewidth 000\relax}


\count0=1
\def\recurse{\pdfximage page \count1 {\dir/\file}
  \edef\Rotate{/Rotate \pdfrotate}
  \expandafter\pdfpageattr\expandafter{\Rotate}
  \ifnum\count1=1 \pdfoutline goto page \number\count0 {/Fit} {\file}
    \ifx\verbose\undefined\errorstopmode
      \message{\file:\space\the\pdflastximagepages}
      \ifnum\pdflastximagepages>1 \message{pages}\else\message{page}\fi
      \ifnum\rotate=0 \else\message{rotated \rotate}\fi
      \batchmode
    \else
      \message{^^J\bar^^J}
      \message{\file:\space\the\pdflastximagepages}
      \ifnum\pdflastximagepages>1 \message{pages}\else\message{page}\fi 
      \ifnum\rotate>0 \message{rotated \rotate}\fi
      \message{\bar^^J^^J}
    \fi
  \fi
  \setbox0\hbox{\pdfrefximage\pdflastximage}
  \pdfpagewidth=\wd0 \pdfpageheight=\ht0 \shipout\box0 
  \advance\count0 by 1 \advance\count1 by 1
  \ifnum\count1=\pdflastximagepages\relax
  \else\expandafter\recurse\fi}

\def\process#1:#2/#3^^M{\count20=#1 
    \edef\rotate{\number\count20 }\multiply\count20by-1
    \edef\pdfrotate{\number\count20 }
    \edef\dir{#2}\edef\file{#3}\count1=1 \recurse\par}

\catcode32=13 \let \space
\everypar{\setbox2\lastbox\process}\endlinechar=`\^^M %
__END__
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.