Use MetaPost to distort images?
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. Hraban *) not small enough for the mailing list; see https://sedna.fiee.net/f/fb532dc85c784147a149/
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.
On 9/3/2021 12:28 PM, Henning Hraban Ramm via ntg-context wrote: 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 -----------------------------------------------------------------
Am 03.09.2021 um 18:24 schrieb Hans Hagen via ntg-context
: 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.
On 9/3/2021 12:28 PM, Henning Hraban Ramm via ntg-context wrote: 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 ...
Thank you! That’s interesting and I can probably use it for a different project, but for what I had in mind, GM wasn’t enough. IM has much more features (and probably also flaws), is even bigger, with more dependencies, but I could install both with MacPorts. I guess it’s on me to write a wiki page about GM. And maybe adapt your code to IM... I just thought since MP is great for geometrical calculations it might also be able to use transformation matrices on images. But since I don’t understand matrix calculations anyway, I’ll live without. Generally, I’d like to delay image manipulation as long as possible – only ConTeXt knows the final size of the used images, and since I want to keep e.g. frames and shadows at the same size for all images I need to calculate with that final size. Hraban
On 9/4/2021 2:18 PM, Henning Hraban Ramm via ntg-context wrote:
I guess it’s on me to write a wiki page about GM.
you can then also add some comparison with im (i.e. what does it offer more than gm) 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 -----------------------------------------------------------------
Am 04.09.2021 um 19:06 schrieb Hans Hagen
: On 9/4/2021 2:18 PM, Henning Hraban Ramm via ntg-context wrote:
I guess it’s on me to write a wiki page about GM.
you can then also add some comparison with im (i.e. what does it offer more than gm)
Oh no... IM and GM have a history, and I don’t want to get involved. GM accuses IM of security issues, feature and dependency bloat, bad code quality, inefficiency, frequent API changes, license changes and whatnot. OTOH IM offers much more features (e.g. the geometric transformations I inquired about), and you can find lots of interesting examples (including the scripts Bruce mentioned). I couldn’t find another CLI tool that can do most of these things, at least not with much more programming. Dependencies according to MacPorts: Full Name: GraphicsMagick @1.3.36_1+q8 Library Dependencies: libxml2, bzip2, xz, zlib, libpng, tiff, freetype, libiconv, libtool, lcms2, jasper, libjpeg-turbo, webp Full Name: ImageMagick @6.9.11-60_1+x11 Library Dependencies: bzip2, djvulibre, xz, jbigkit, libjpeg-turbo, lcms2, libpng, libraw, tiff, webp, zlib, fftw-3, freetype, fontconfig, ghostscript, libiconv, libtool, openjpeg, openexr, expat, libxml2, libheif, pkgconfig, xorg-libX11, xorg-libXext, xorg-libXt Runtime Dependencies: urw-fonts It’s probably not wise to run IM in a web server process for security and efficiency reasons, but if I run it only on my own computer there shouldn’t be a problem. BTW, here’s my script: #!/bin/bash #INPUT=$1 OUTPUT=${INPUT/.JPG/.png} OUTPUT=${OUTPUT/.jpg/.png} OUTPUT=${OUTPUT/in\//out\/} # random rotation ±1.5˚ ROTATION=`jot -r 1 -1.5 1.5` echo $OUTPUT ${ROTATION}˚ # downscale, add a white border and a shadow, rotate, move down, add a piece of tape (tesa1.png), grayscale convert "$INPUT" \ -auto-orient \ -resize 800x800 \ -bordercolor white -border 10 \ \( +clone -background black -shadow 80x3+2+2 \) +swap \ -background transparent \ -layers merge \ -rotate ${ROTATION} \ +repage \ -gravity north \ -splice 0x10 \ \( tesa1.png -resize 200x50! \) -compose Multiply -composite \ -colorspace Gray \ "$OUTPUT" Here are some results: https://sedna.fiee.net/d/2c3218730f8341aab1fa/ Hraban
On 9/5/2021 9:40 AM, Henning Hraban Ramm via ntg-context wrote:
Am 04.09.2021 um 19:06 schrieb Hans Hagen
: On 9/4/2021 2:18 PM, Henning Hraban Ramm via ntg-context wrote:
I guess it’s on me to write a wiki page about GM.
you can then also add some comparison with im (i.e. what does it offer more than gm)
Oh no...
IM and GM have a history, and I don’t want to get involved.
GM accuses IM of security issues, feature and dependency bloat, bad code quality, inefficiency, frequent API changes, license changes and whatnot.
ah, i think i remember ... i switched to gm just because im didn't run well at that time on windows personally i think that "accuses of .." show that down side of the open source community (which includes the tex community btw) ... it gets even worse when something sells itself for big money and then after a few years spins off again attacking those who paid them ... i suppose that unconstrianed social media also play a role in this (one reason why i'm not really interested in discussions around this in public) like: what is bad code quality ... i bet one can find examples of bad, inefficient, etc in gm too (it would be quite a claim to say one can't), and the more people touch the code ... also, what about the code quality of libs then ... one bug in those could affect lots of programs using them ... most is about trust .. and even more in 'working with people you like working with' btw, 'bug' and 'many bugs' and 'big bug' are popular in subject titles and mail bodies, even it's small bugs or side effects or something that has to be tested but it might make people feel good 'security fix' is another one, which often just means: programming error e.g. array out of bounds; nowadays for me the way an email is written determines the reading order, but there is no good filter for that yet; same for (tech) websites: bluffing and barking agianst a tree (like your mentioned 'accuses' makes me go away immediately and discard whatever would follow) (what to think of: https://www.youtube.com/watch?v=BhTQyeEdnzs)
OTOH IM offers much more features (e.g. the geometric transformations I inquired about), and you can find lots of interesting examples (including the scripts Bruce mentioned). I couldn’t find another CLI tool that can do most of these things, at least not with much more programming.
ok, i'll have a look later, maybe it can be compiled lean-and-mean
Dependencies according to MacPorts:
Full Name: GraphicsMagick @1.3.36_1+q8 Library Dependencies: libxml2, bzip2, xz, zlib, libpng, tiff, freetype, libiconv, libtool, lcms2, jasper, libjpeg-turbo, webp
makes me wonder if there is some simple image manipulation lib that just deals with the pixels .. we don't need xml, freetype, libiconv, ...
Full Name: ImageMagick @6.9.11-60_1+x11 Library Dependencies: bzip2, djvulibre, xz, jbigkit, libjpeg-turbo, lcms2, libpng, libraw, tiff, webp, zlib, fftw-3, freetype, fontconfig, ghostscript, libiconv, libtool, openjpeg, openexr, expat, libxml2, libheif, pkgconfig, xorg-libX11, xorg-libXext, xorg-libXt Runtime Dependencies: urw-fonts
hm, that looks bad ... maybe not have a look after all ... i get the impression that many originally small programs get bloated by pulling in one lib that then pulls in more, even if nothing of that is used or useful to the task (we sort of had that when luatex used xpdf from which we only need the reading part but we could not really complain about that because it has been so from the start but it triggered quite a bit of updating) keep in mind that we can just call them as programs using os.execute (or the filter module or ...) ... no need to waste time in integration (apart from a fun aspect)
It’s probably not wise to run IM in a web server process for security and efficiency reasons, but if I run it only on my own computer there shouldn’t be a problem.
hm, why not? all it does in the end is "bits in - bits out"
BTW, here’s my script:
#!/bin/bash #INPUT=$1
OUTPUT=${INPUT/.JPG/.png} OUTPUT=${OUTPUT/.jpg/.png} OUTPUT=${OUTPUT/in\//out\/}
# random rotation ±1.5˚ ROTATION=`jot -r 1 -1.5 1.5` echo $OUTPUT ${ROTATION}˚
# downscale, add a white border and a shadow, rotate, move down, add a piece of tape (tesa1.png), grayscale convert "$INPUT" \ -auto-orient \ -resize 800x800 \ -bordercolor white -border 10 \ \( +clone -background black -shadow 80x3+2+2 \) +swap \ -background transparent \ -layers merge \ -rotate ${ROTATION} \ +repage \ -gravity north \ -splice 0x10 \ \( tesa1.png -resize 200x50! \) -compose Multiply -composite \ -colorspace Gray \ "$OUTPUT"
Here are some results: https://sedna.fiee.net/d/2c3218730f8341aab1fa/
Did you try that with the filter module? It would make it more portable Hans ps. It is still on my todo list to see of we can add some of metafont's bit stuff. ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Am 05.09.2021 um 10:10 schrieb Hans Hagen
: On 9/5/2021 9:40 AM, Henning Hraban Ramm via ntg-context wrote:
Am 04.09.2021 um 19:06 schrieb Hans Hagen
: On 9/4/2021 2:18 PM, Henning Hraban Ramm via ntg-context wrote:
I guess it’s on me to write a wiki page about GM.
you can then also add some comparison with im (i.e. what does it offer more than gm) Oh no... IM and GM have a history, and I don’t want to get involved. GM accuses IM of security issues, feature and dependency bloat, bad code quality, inefficiency, frequent API changes, license changes and whatnot.
ah, i think i remember ... i switched to gm just because im didn't run well at that time on windows
personally i think that "accuses of .." show that down side of the open source community (which includes the tex community btw) ... it gets even worse when something sells itself for big money and then after a few years spins off again attacking those who paid them ... i suppose that unconstrianed social media also play a role in this (one reason why i'm not really interested in discussions around this in public)
I hear you. Of course there are reasons to be opinionated and to fork some open source projects if they move into a direction that you don’t like. But that doesn’t mean you can’t be polite about it. And the GM maintainers focus too much on their hate for IM’s developer(s) for my taste. Maybe it helps them to stay focused and write better code if they have an enemy...
(what to think of: https://www.youtube.com/watch?v=BhTQyeEdnzs)
The video doesn’t run for me, just read the description: I’m sure the problem exists, but it’s maybe exaggerated?
Dependencies according to MacPorts: Full Name: GraphicsMagick @1.3.36_1+q8 Library Dependencies: libxml2, bzip2, xz, zlib, libpng, tiff, freetype, libiconv, libtool, lcms2, jasper, libjpeg-turbo, webp
makes me wonder if there is some simple image manipulation lib that just deals with the pixels .. we don't need xml, freetype, libiconv, ...
Maybe use these libs (libpng etc.) directly? I don’t know what they can do. Is there a Lua lib that parallels Python’s Pillow? It also has a lot of dependencies to support image formats or add features, but most of them are optional, see https://pillow.readthedocs.io/en/latest/installation.html#building-from-sour...
Full Name: ImageMagick @6.9.11-60_1+x11 Library Dependencies: bzip2, djvulibre, xz, jbigkit, libjpeg-turbo, lcms2, libpng, libraw, tiff, webp, zlib, fftw-3, freetype, fontconfig, ghostscript, libiconv, libtool, openjpeg, openexr, expat, libxml2, libheif, pkgconfig, xorg-libX11, xorg-libXext, xorg-libXt Runtime Dependencies: urw-fonts
hm, that looks bad ... maybe not have a look after all ... i get the impression that many originally small programs get bloated by pulling in one lib that then pulls in more,
Exactly. The criticism by the GM maintainers probably *is* justified. (When I installed IM, actually all of the dependecies were already installed for other programs.)
even if nothing of that is used or useful to the task (we sort of had that when luatex used xpdf from which we only need the reading part but we could not really complain about that because it has been so from the start but it triggered quite a bit of updating)
It might make sense for some projects to dive into these libs and pull out and combine the parts that they really need. Even if that’s against the meaning of shared libraries and makes it harder to watch upstream for security fixes...
keep in mind that we can just call them as programs using os.execute (or the filter module or ...) ... no need to waste time in integration (apart from a fun aspect)
Of course. I never wanted to suggest integrating GM, IM or whatever.
It’s probably not wise to run IM in a web server process for security and efficiency reasons, but if I run it only on my own computer there shouldn’t be a problem.
hm, why not? all it does in the end is "bits in - bits out"
Apparently there were bugs in IM that bite web developers – if you used IM via PHP and allowed users to upload arbitrary images (e.g. avatars), they could exploit buffer overflows or something like that to hack your system. Or at least stall it (DoS attack).
BTW, here’s my script: Here are some results:
Did you try that with the filter module? It would make it more portable
I completely forgot I could do that (and I even know how). Working on too many building sites at the same time... Hraban
On Sun, 5 Sep 2021, Henning Hraban Ramm via ntg-context wrote:
Am 05.09.2021 um 10:10 schrieb Hans Hagen
: BTW, here’s my script: Here are some results:
Did you try that with the filter module? It would make it more portable
I completely forgot I could do that (and I even know how).
See attached. Basically, you can just copy-paste your script as the value of the filtercommand, and that's it. Aditya PS: I realized that there is a regression, and \externalfilterbasefile does not remove the file extension. So, the output is named mill.png-taped.png rather than mill-taped.png. I'll look into it later
Am 06.09.2021 um 04:13 schrieb Aditya Mahajan via ntg-context
: On Sun, 5 Sep 2021, Henning Hraban Ramm via ntg-context wrote:
Am 05.09.2021 um 10:10 schrieb Hans Hagen
: BTW, here’s my script: Here are some results:
Did you try that with the filter module? It would make it more portable
I completely forgot I could do that (and I even know how).
See attached. Basically, you can just copy-paste your script as the value of the filtercommand, and that's it.
Thank you! I could have done it myself... But Hans was in a generous and experimenting mood and wrote - Lua code interfacing GraphicsMagick to manipulate bitmap data - a MP interface to ImageMagick I just need to test them and will report.
PS: I realized that there is a regression, and \externalfilterbasefile does not remove the file extension. So, the output is named mill.png-taped.png rather than mill-taped.png. I'll look into it later
Oh, thank you. I need to check in my LilyPond setup. Hraban
On 9/5/2021 9:40 AM, Henning Hraban Ramm via ntg-context wrote:
OTOH IM offers much more features (e.g. the geometric transformations I inquired about), and you can find lots of interesting examples (including the scripts Bruce mentioned). I couldn’t find another CLI tool that can do most of these things, at least not with much more programming.
one thing the api offers is a way to pass 'command line like options' to the converter ... so i might cook up a variant for it in the end because it needs not much interfacing 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 -----------------------------------------------------------------
On 3 Sep 2021, at 11:28, 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.
No idea about MetaPost but "Fred's ImageMagic Scripts" http://www.fmwconcepts.com/imagemagick/index.php is a fantastic resource. Start with "vintage" 1, 2 & 3 towards the bottom. The site is a little idiosyncratic in that you click and nothing appears to happen. This is because all the pages start with the same table of scripts at the top so you have to scroll down to see an example of the effect achieved by the script you just selected. — Bruce Horrocks Hampshire, UK
Am 04.09.2021 um 00:16 schrieb Bruce Horrocks via ntg-context
: On 3 Sep 2021, at 11:28, 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.
No idea about MetaPost but "Fred's ImageMagic Scripts" http://www.fmwconcepts.com/imagemagick/index.php is a fantastic resource. Start with "vintage" 1, 2 & 3 towards the bottom.
The site is a little idiosyncratic in that you click and nothing appears to happen. This is because all the pages start with the same table of scripts at the top so you have to scroll down to see an example of the effect achieved by the script you just selected.
Thank you – I know that site. I just hoped to keep everything ”inside“. I made my own (shell) script for Imagemagick now; since I didn’t understand the transformation matrix anyway, I went without that distortion. (Nobody will miss it...) Hraban
participants (4)
-
Aditya Mahajan
-
Bruce Horrocks
-
Hans Hagen
-
Henning Hraban Ramm