ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Getting changing/random graphics?
@ 2012-02-16  6:43 Mari Voipio
  2012-02-16  9:48 ` Hans Hagen
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mari Voipio @ 2012-02-16  6:43 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hello all,

I'm trying to do something that should be possible and I think
"variables" is the answer, I just can't figure out how.

I have a pile of photos (jpg) in a directory and the assumption is
that they are all the same size and there's a spot for the photo on a
layer at the top of the page. I already found out how I can get
ConTeXt to draw the layer separately for each page (at
http://wiki.contextgarden.net/layers#Use_dynamic_content), but I think
I need a "variables for dummies" page...

This time I'm not too picky, the images can be used randomly or in the
order they are in the directory and then cycled, I'd just like to use
more than one (and I'm so not doing every page by hand).


So, if I have

photo1.jpg
photo2.jpg
photo3.jpg
photo4.jpg


and \dorecurse{10}{insert random photo here}, how do I do it?


Thank you,

Mari
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Getting changing/random graphics?
  2012-02-16  6:43 Getting changing/random graphics? Mari Voipio
@ 2012-02-16  9:48 ` Hans Hagen
  2012-02-16  9:50 ` Hans Hagen
  2012-02-16  9:53 ` Marco
  2 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2012-02-16  9:48 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Mari Voipio

On 16-2-2012 07:43, Mari Voipio wrote:
> Hello all,
>
> I'm trying to do something that should be possible and I think
> "variables" is the answer, I just can't figure out how.
>
> I have a pile of photos (jpg) in a directory and the assumption is
> that they are all the same size and there's a spot for the photo on a
> layer at the top of the page. I already found out how I can get
> ConTeXt to draw the layer separately for each page (at
> http://wiki.contextgarden.net/layers#Use_dynamic_content), but I think
> I need a "variables for dummies" page...
>
> This time I'm not too picky, the images can be used randomly or in the
> order they are in the directory and then cycled, I'd just like to use
> more than one (and I'm so not doing every page by hand).
>
>
> So, if I have
>
> photo1.jpg
> photo2.jpg
> photo3.jpg
> photo4.jpg
>
>
> and \dorecurse{10}{insert random photo here}, how do I do it?

More fancy ...

\startluacode

     local files = { }

     function document.collectgraphics(pattern)
         files = table.merged(files,dir.glob(pattern))
         files = table.unique(files)
         table.sort(files)
         logs.report(string.format("graphics: %s",table.concat(files," ")))
     end

     function document.choosegraphics()
         if #files > 0 then
             context(table.remove(files, math.random(1,#files)) or "dummy")
         else
             context("dummy")
         end
     end

\stopluacode

\def\CollectGraphics[#1]{\ctxlua{document.collectgraphics("#1")}}
\def\ChooseGraphic      {\ctxlua{document.choosegraphic()}}

\CollectGraphics[*.png]
\CollectGraphics[*.jpg]

\starttext

     \dorecurse {10} {
         \expanded{\externalfigure[\ChooseGraphic][width=4cm]}
     }

\stoptext



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Getting changing/random graphics?
  2012-02-16  6:43 Getting changing/random graphics? Mari Voipio
  2012-02-16  9:48 ` Hans Hagen
@ 2012-02-16  9:50 ` Hans Hagen
  2012-02-22  8:22   ` Mari Voipio
  2012-02-16  9:53 ` Marco
  2 siblings, 1 reply; 6+ messages in thread
From: Hans Hagen @ 2012-02-16  9:50 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Mari Voipio

[-- Attachment #1: Type: text/plain, Size: 608 bytes --]

On 16-2-2012 07:43, Mari Voipio wrote:

> I'm trying to do something that should be possible and I think
> "variables" is the answer, I just can't figure out how.

attached .. (will be in test suite)

Hans



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------

[-- Attachment #2: random-001.tex --]
[-- Type: text/x-tex, Size: 810 bytes --]

% For Mari Voipio

\startluacode

    local files = { }

    function document.collectgraphics(pattern)
        files = table.merged(files,dir.glob(pattern))
        files = table.unique(files)
        table.sort(files)
        logs.report(string.format("graphics: %s",table.concat(files," ")))
    end

    function document.choosegraphic()
        if #files > 0 then
            context(table.remove(files, math.random(1,#files)) or "dummy")
        else
            context("dummy")
        end
    end

\stopluacode

\def\CollectGraphics[#1]{\ctxlua{document.collectgraphics("#1")}}
\def\ChooseGraphic      {\ctxlua{document.choosegraphic()}}

\CollectGraphics[*.png]
\CollectGraphics[*.jpg]

\starttext

    \dorecurse {10} {
        \expanded{\externalfigure[\ChooseGraphic][width=4cm]}
    }

\stoptext

[-- Attachment #3: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Getting changing/random graphics?
  2012-02-16  6:43 Getting changing/random graphics? Mari Voipio
  2012-02-16  9:48 ` Hans Hagen
  2012-02-16  9:50 ` Hans Hagen
@ 2012-02-16  9:53 ` Marco
  2012-02-22  7:32   ` Mari Voipio
  2 siblings, 1 reply; 6+ messages in thread
From: Marco @ 2012-02-16  9:53 UTC (permalink / raw)
  To: ntg-context

On 2012-02-16 Mari Voipio <mari.voipio@iki.fi> wrote:

> So, if I have
> 
> photo1.jpg
> photo2.jpg
> photo3.jpg
> photo4.jpg
> 
> and \dorecurse{10}{insert random photo here}, how do I do it?

\dorecurse{4}{\externalfigure[photo\recurselevel]}

Beware, \recurselevel  counts go  like this  1 2 3 4 5 ….  If your
photos are not numbered according to this scheme, the 10th picture
will bite you.

Marco


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Getting changing/random graphics?
  2012-02-16  9:53 ` Marco
@ 2012-02-22  7:32   ` Mari Voipio
  0 siblings, 0 replies; 6+ messages in thread
From: Mari Voipio @ 2012-02-22  7:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Feb 16, 2012 at 11:53, Marco <netuse@lavabit.com> wrote:
> On 2012-02-16 Mari Voipio <mari.voipio@iki.fi> wrote:
>
>> So, if I have
>>
>> photo1.jpg
>> photo2.jpg
>> photo3.jpg
>> photo4.jpg
>>
>> and \dorecurse{10}{insert random photo here}, how do I do it?
>
> \dorecurse{4}{\externalfigure[photo\recurselevel]}
>
> Beware, \recurselevel  counts go  like this  1 2 3 4 5 ….  If your
> photos are not numbered according to this scheme, the 10th picture
> will bite you.

Beautiful, it works - and I even understand it. :-) And I managed to
figure out by myself that I can do a nested \dorecurse to multiply
this:

\dorecurse{3}{\dorecurse{6}{\externalfigure[pikkukuva\recurselevel][height=25mm,frame=on]}}


My not be the most elegant (or fastest compiling!) solution, but this
one I can definitely get to work and it'll suit my purpose well
enough.


Thank you,

Mari
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Getting changing/random graphics?
  2012-02-16  9:50 ` Hans Hagen
@ 2012-02-22  8:22   ` Mari Voipio
  0 siblings, 0 replies; 6+ messages in thread
From: Mari Voipio @ 2012-02-22  8:22 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On Thu, Feb 16, 2012 at 11:50, Hans Hagen <pragma@wxs.nl> wrote:
> On 16-2-2012 07:43, Mari Voipio wrote:
>
>> I'm trying to do something that should be possible and I think
>> "variables" is the answer, I just can't figure out how.
>
>
> attached .. (will be in test suite)

It works. :-)
However, this one apparently uses each pic only once and after that I
just get "dummies". Nesting this \dorecurse didn't work (not that I
expected it to, but had to try anyway) - how to tell this code either
"when you've used up all the photos, start over" or "add any random
graphic in this directory"?

It looks like I'm going to have 50-100 pages, so if there are 10-15
graphics and they are added completely at random, the likelihood of
each one to appear at least once is pretty high (at least according to
what I remember of my probability math). Thus I won't need any
complicated checkups, any old randomizer in the "take a card out of a
bag and put it bag" sense should do the trick; I'm just not used to
doing this type of stuff, so I cannot even fathom where to start!


Hmm... I'd like to include besides png and jpg also vector graphics in
pdf format, but obviously I need to exclude any pdf documents in the
same directory. Of course one solution is to put the graphics into a
separate directory, but if I only want to included .png, .jpg and .pdf
with a certain name (like 33s-sensor.jpg, 33s-cutthrough.pdf, etc.),
how should the lua code then look like? The name can be written into
the code, as long as I put decent comments in, I should be able to
adjust it as needed.

(File size shouldn't be a concern, the graphics are going to be 2-2.5
cm tall and once I've collected the appropriate graphics, I'll make
sure the bitmaps are fit for this use. Besides, I'll be using a print
shop and they get a lot worse than anything I can produce with
ConTeXt...)



In a "need-to-know" mode,

Mari
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-02-22  8:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-16  6:43 Getting changing/random graphics? Mari Voipio
2012-02-16  9:48 ` Hans Hagen
2012-02-16  9:50 ` Hans Hagen
2012-02-22  8:22   ` Mari Voipio
2012-02-16  9:53 ` Marco
2012-02-22  7:32   ` Mari Voipio

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).