This is lying around for years, let’s try to get it finally working. The code below works, but doesn’t do the right thing. Since the original image doesn’t contain a resolution setting, gm doesn’t do anything: “gm convert: image does not contain resolution” It would be more reliable to use “gm convert -resize”, but for that we need to know the final pixel size. How can a converter function access … – the final (scaled) size of a placed image – the original pixel size of an image ? With that information it would be easy to calculate the target pixel size. And then it would be nice to hook this function into \setupexternalfigure[conversion=] """ \startluacode local function downsampler(oldname, newname, resolution) if not resolution or resolution == "" then resolution = 72 end local cmd = string.format( [[gm convert -resample %ix%i "%s" "%s"]], resolution, resolution, oldname, newname) logs.report("downsample", cmd) os.execute(cmd) end -- Set the PDF and default JPEG converters to the above function. figures.converters.jpg.pdf = downsampler figures.converters.jpg.default = downsampler \stopluacode \setupexternalfigure[ %conversion=downsampler,% not used resolution=10, ] \starttext \externalfigure[https://upload.wikimedia.org/wikipedia/commons/d/dd/Hermann_Zapf_signing.jpg][width=.5\textwidth] \stoptext """ Hraban
Hraban,
I would first "fix" my image sources, individually, to contain missing
resolution information.
Alan
On Sat, 21 Sep 2024 13:21:08 +0200
Henning Hraban Ramm
This is lying around for years, let’s try to get it finally working. The code below works, but doesn’t do the right thing.
Since the original image doesn’t contain a resolution setting, gm doesn’t do anything: “gm convert: image does not contain resolution”
It would be more reliable to use “gm convert -resize”, but for that we need to know the final pixel size.
How can a converter function access … – the final (scaled) size of a placed image – the original pixel size of an image ? With that information it would be easy to calculate the target pixel size.
And then it would be nice to hook this function into \setupexternalfigure[conversion=]
""" \startluacode local function downsampler(oldname, newname, resolution) if not resolution or resolution == "" then resolution = 72 end local cmd = string.format( [[gm convert -resample %ix%i "%s" "%s"]], resolution, resolution, oldname, newname) logs.report("downsample", cmd) os.execute(cmd) end
-- Set the PDF and default JPEG converters to the above function. figures.converters.jpg.pdf = downsampler figures.converters.jpg.default = downsampler \stopluacode
\setupexternalfigure[ %conversion=downsampler,% not used resolution=10, ]
\starttext
\externalfigure[https://upload.wikimedia.org/wikipedia/commons/d/dd/Hermann_Zapf_signing.jpg][width=.5\textwidth]
\stoptext """
Hraban ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net ___________________________________________________________________________________
Am 21.09.24 um 16:30 schrieb Alan Braslau:
I would first "fix" my image sources, individually, to contain missing resolution information.
The resolution setting is just metadata, it doesn’t affect pixel or file size. I need a function that works with “real” (pixel) sizes of images. Hraban
On Sat, 21 Sep 2024 21:44:53 +0200
Henning Hraban Ramm
Am 21.09.24 um 16:30 schrieb Alan Braslau:
I would first "fix" my image sources, individually, to contain missing resolution information.
The resolution setting is just metadata, it doesn’t affect pixel or file size. I need a function that works with “real” (pixel) sizes of images.
Exactly. The raw data is just pixels, and metadata sets what the expected pixel size might be. Of course, this metadata can be meaningless as the user can and often does rescale the image to meet the typesetting needs. The image manipulation tools use this metadata to resample resolution. I found this very useful as I used *hundreds* of scanned X-ray images that had real sizes, high resolution and 16 or even 24 bit depth. Of course, I had to reduce all of this in order to include them in a digestible and printable pdf. Alan
On Sat, Sep 21 2024, Henning Hraban Ramm wrote:
How can a converter function access … – the final (scaled) size of a placed image
Hi, I use "figures.current().request" in my module (here attached).
– the original pixel size of an image ?
I use img.scan() for that. HTH, -- Peter
Am 22.09.24 um 09:06 schrieb Peter Münster:
On Sat, Sep 21 2024, Henning Hraban Ramm wrote:
How can a converter function access … – the final (scaled) size of a placed image
Hi,
I use "figures.current().request" in my module (here attached).
– the original pixel size of an image ?
I use img.scan() for that.
Hi Peter, sorry, I forgot to answer. I was using your code for years, and AFAIR it also doesn’t use the final scaled size. But I must check again – too much going on ATM… Hraban
Am 22.09.24 um 09:06 schrieb Peter Münster:
On Sat, Sep 21 2024, Henning Hraban Ramm wrote:
How can a converter function access … – the final (scaled) size of a placed image
Hi,
I use "figures.current().request" in my module (here attached).
– the original pixel size of an image ?
I use img.scan() for that.
Hi Peter, are you sure that your code works with LMTX? Find attached my version. It seems to work, but only for the first instance of an image, not for different sizes of the same image – makes sense WRT efficiency, but might give wrong results for some uses. Here’s my test code: """ \loadluafile[grph-downsample.lua] \setupexternalfigure[ location={local,global,default}, conversion=lowres.jpg, resolution=20, ] \starttext \useexternalfigure[zapf][https://upload.wikimedia.org/wikipedia/commons/d/dd/Hermann_Zapf_signing.jpg] \useexternalfigure[lino][https://upload.wikimedia.org/wikipedia/commons/b/bd/Linotype-vorne-deutsches...] \externalfigure[zapf][width=.5tw] \externalfigure[lino][height=.3th] \externalfigure[zapf][height=.5th] \stoptext """ Hraban
On Wed, Oct 16 2024, Henning Hraban Ramm wrote:
I use img.scan() for that.
are you sure that your code works with LMTX?
Hi Hraban, No, sorry, I didn’t test it...
Find attached my version.
Thanks, I have integrated your ideas in a new version here attached.
It seems to work, but only for the first instance of an image,
Yes, you need to patch the file grph-inc.lmt: https://lists.contextgarden.net/archives/list/ntg-context@ntg.nl/message/ES7... Cheers, -- Peter
participants (3)
-
Alan Braslau
-
Henning Hraban Ramm
-
Peter Münster