Getting changing/random graphics?
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
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 -----------------------------------------------------------------
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 -----------------------------------------------------------------
On Thu, Feb 16, 2012 at 11:50, Hans Hagen
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
On 2012-02-16 Mari Voipio
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
On Thu, Feb 16, 2012 at 11:53, Marco
On 2012-02-16 Mari Voipio
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
participants (3)
-
Hans Hagen
-
Marco
-
Mari Voipio