ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Benjamin Buchmuller <benjamin.buchmuller@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: [NTG-context] Re: setupexternalfigures and resoĺution
Date: Fri, 28 Jul 2023 21:14:41 -0400	[thread overview]
Message-ID: <F56658E4-26E7-489B-814E-09891E6EBBF1@gmail.com> (raw)

This thread about downsampling external images on the fly is a really helpful feature! Thanks, Hraban for sharing the code!

I was wondering if it was possible to downscale also raster images embedded in pdf/svg files loaded via \externalfigure?

I found a ghostscript solution online (https://stackoverflow.com/questions/9497120/how-to-downsample-images-within-pdf-file)

gs \
-o downsampled.pdf \
-sDEVICE=pdfwrite \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=72 \
-dGrayImageResolution=72 \
-dMonoImageResolution=72 \
-dColorImageDownsampleThreshold=1.0 \
-dGrayImageDownsampleThreshold=1.0 \
-dMonoImageDownsampleThreshold=1.0 \
input.pdf

Now, I'm wondering how to adapt grph-downsample.lua to do this?

I've tried:

\setupexternalfigure[
	resolution=\Resolution, conversion=lowres.pdf
]

And added to grph-downsample.lua

local function sample_down_pdf(oldname, newname, resolution)
	print("DOWNSAMPLE sample_down " .. oldname .. " to " .. newname)
	
    -- MkIV:
    -- local image = img.scan{filename = oldname}

    -- LMTX:
    local image = figures.getinfo(oldname,1)
    image = image.status.private

    local s = format("gs \
-o \"%s\" \
-sDEVICE=pdfwrite \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=%s \
-dGrayImageResolution=%s \
-dMonoImageResolution=%s \
-dColorImageDownsampleThreshold=1.0 \
-dGrayImageDownsampleThreshold=1.0 \
-dMonoImageDownsampleThreshold=1.0 \
\"%s\"",
		oldname, resolution, resolution, resolution, newname)
					   print("DOWNSAMPLE Conversion: " .. s)
	  os.execute(s)
end

figures.converters["pdf"]["lowres." .. "pdf"] = sample_down_pdf

But I must be missing something as the code doesn't seem to be called.

Any suggestions?

Thanks!


Ben

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

                 reply	other threads:[~2023-07-29  1:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=F56658E4-26E7-489B-814E-09891E6EBBF1@gmail.com \
    --to=benjamin.buchmuller@gmail.com \
    --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).