ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Hans Hagen via ntg-context <ntg-context@ntg.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Cc: Hans Hagen <j.hagen@xs4all.nl>
Subject: Re: Use MetaPost to distort images?
Date: Fri, 3 Sep 2021 18:24:57 +0200	[thread overview]
Message-ID: <7cd61bb7-9f65-41c2-53f1-55489f9fa8f7@xs4all.nl> (raw)
In-Reply-To: <BA499834-4E4E-49C2-B3CD-06DBA1489FDB@fiee.net>

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

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
-----------------------------------------------------------------

[-- Attachment #2: meta-imp-magick.pdf --]
[-- Type: application/pdf, Size: 183893 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2021-09-03 16:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-03 10:28 Henning Hraban Ramm via ntg-context
2021-09-03 16:24 ` Hans Hagen via ntg-context [this message]
2021-09-04 12:18   ` Henning Hraban Ramm via ntg-context
2021-09-04 17:06     ` Hans Hagen via ntg-context
2021-09-05  7:40       ` Henning Hraban Ramm via ntg-context
2021-09-05  8:10         ` Hans Hagen via ntg-context
2021-09-05  9:34           ` Henning Hraban Ramm via ntg-context
2021-09-06  2:13             ` Aditya Mahajan via ntg-context
2021-09-06  8:42               ` Henning Hraban Ramm via ntg-context
2021-09-05  8:46         ` Hans Hagen via ntg-context
2021-09-03 22:16 ` Bruce Horrocks via ntg-context
2021-09-04 11:59   ` Henning Hraban Ramm via ntg-context

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7cd61bb7-9f65-41c2-53f1-55489f9fa8f7@xs4all.nl \
    --to=ntg-context@ntg.nl \
    --cc=j.hagen@xs4all.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).