On 9/3/2021 12:28 PM, Henning Hraban Ramm via ntg-context wrote: > Hi, is it possible to apply geometric transformations to images using MetaPost? > > This is again way over my head, but I’m still trying different methods to let a ConTeXt document look more “hand-made”. > > In my example*, I added frame and shadow to a photo and _distorted_ it slightly to have it look like a postcard was glued on the page (with a piece of tape). I’d like to automate and randomize that process. > > I guess I could achieve something similar with ImageMagick (not with GraphicsMagick), but if it would be possible inside LMTX that would be great. the question is not so much if it can be done but how far we should go in doing things .. so, only as friday afternoon distraction, and because i wanted to upload anyway (not done yet) i'll meta-imp-magick that uses the gm lib interface (that only does simple conversions, but i added bur and noise an dcan add some more if needed as long as there is no weird stuff or code bloat) not in the core, just as 'imp' because we don't want library dependencies (and the optional libs are lightweight delayed loading ones) now, the drawback of course is that one needs gm installed (quite large) but it does work on my windows box so ... here is the educational part: % we load the library \registerctxluafile{libs-imp-graphicsmagick}{autosuffix} % we define the lua end of the mp plugin \startluacode function mp.lmt_magick_convert() local specification = metapost.getparameterset("magick") local inputname = specification.filename if inputname then -- we don't want to trigger reuse when we have the same input file local hash = md5.HEX(table.sequenced(specification)) local outputname = file.addsuffix("m_k_i_v_mp_fuzzy_" .. hash,file.suffix(inputname)) luatex.registertempfile(outputname) -- make the table a bit unique and don't polute it local whattodo = table.setmetatableindex( { inputname = inputname, outputname = outputname, }, specification) -- now do the magick utilities.graphicsmagick.convert(whattodo) -- and return the result return [[figure("]] .. outputname .. [[")]] else -- bad luck return [[textext("missing filename")]] end end \stopluacode % we define the lmt_ plugin \startMPdefinitions presetparameters "magick" [ filename = "unset", % blur = [ radius = 10, sigma = 5 ], % noise = [ type = 4 ], ] ; def lmt_magick = applyparameters "magick" "lmt_do_magick" enddef ; vardef lmt_do_magick = lua.mp.lmt_magick_convert() enddef ; \stopMPdefinitions % and this is how that one is used \startMPpage draw lmt_magick [ filename = "hacker.jpg", blur = [ radius = 10, sigma = 5 ], noise = [ type = 2 ], ] ysized 4cm ; draw lmt_magick [ filename = "hacker.jpg", blur = [ radius = 5, sigma = 3 ], noise = [ type = 4 ], ] ysized 4cm shifted (0, -4cm) ; draw lmt_magick [ filename = "hacker.jpg", blur = [ radius = 10, sigma = 5 ], noise = [ type = 4 ], ] ysized 4cm shifted (0, -8cm) ; \stopMPpage % you can add ornaments to this of course using other mp commands like % putting it on top of your postcard paper % as you see, there is not even that much code involved (apart from % some in the lib module but that is also rather minimalistic % the result is attached Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------