ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: "Peter Münster" <pm@a16n.net>
To: ntg-context@ntg.nl
Subject: [NTG-context] Re: Downsampling images (again)
Date: Sun, 22 Sep 2024 09:06:57 +0200	[thread overview]
Message-ID: <87y13kqi9a.fsf@a16n.net> (raw)
In-Reply-To: <23dd9e7a-a61c-4697-998c-ac4de8273592@fiee.net> (Henning Hraban Ramm's message of "Sat, 21 Sep 2024 13:21:08 +0200")

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

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

[-- Attachment #2: grph-downsample.lua --]
[-- Type: application/octet-stream, Size: 1610 bytes --]

if not modules then modules = { } end modules ['grph-downsample'] = {
  version   = 1.001,
  comment   = "companion to grph-inc.mkiv",
  author    = "Peter Münster",
  copyright = "PRAGMA ADE / ConTeXt Development Team",
  license   = "see context related readme files"
}

local format = string.format
-- figures.cachepaths.path = "cache" -- should be setup-option
local function sample_down(oldname, newname, resolution)
    local request = figures.current().request
    local width = request.width
    local height = request.height
    if resolution == "" or (not width and not height) then
        print(format("Nothing to do: %s, %s, %s", oldname, newname, resolution))
        return
    end
    local inch = 72.27
    local image = img.scan{filename = oldname}
    local xy = image.xsize / image.ysize
    if not width then
        width = height * xy / 65536
    end
    if not height then
        height = width / xy / 65536
    end
    local xsize = resolution * width / inch
    local ysize = resolution * height / inch
    if xsize < image.xsize or ysize < image.ysize then
        local s = format("gm convert -resize %dx%d %s %s",
                         xsize, ysize, oldname, newname)
        print("Conversion: " .. s)
        os.execute(s)
    else
        print(format("Nothing to do: %s, %s, %s", oldname, newname, resolution))
        print(format("xsize = %d, ysize = %d", xsize, ysize))
    end
end

local formats = {"png", "jpg", "gif"}

for _, s in ipairs(formats) do
    figures.converters[s] = figures.converters[s] or {}
    figures.converters[s]["lowres." .. s] = sample_down
end

[-- Attachment #3: Type: text/plain, Size: 511 bytes --]

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

  parent reply	other threads:[~2024-09-22  7:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21 11:21 [NTG-context] " Henning Hraban Ramm
2024-09-21 14:30 ` [NTG-context] " Alan Braslau via ntg-context
2024-09-21 19:44   ` Henning Hraban Ramm
2024-09-23  1:48     ` Alan Braslau via ntg-context
2024-09-22  7:06 ` Peter Münster [this message]
2024-10-09 16:52   ` Henning Hraban Ramm
2024-10-16 17:59   ` Henning Hraban Ramm
2024-10-17 11:11     ` Peter Münster

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=87y13kqi9a.fsf@a16n.net \
    --to=pm@a16n.net \
    --cc=ntg-context@ntg.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).