ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Michael Guravage <guravage@literatesolutions.com>
To: ntg-context@ntg.nl
Subject: [NTG-context] Rotating and scaling images to fit a page
Date: Thu, 24 Apr 2025 10:28:24 +0200	[thread overview]
Message-ID: <CAL-A5LZSQSf2XQ5NECLy6Mcqn2rEFLqAB6K-38haTeHWCHoQMA@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1400 bytes --]

Greetings,

I have many images, all with different aspect ratios, that I would like to
collect in a document. Each image should be rotated and scaled to fit its
page. My MWE uses an image's width and height to discern whether it's wide,
tall, or square. This works, but I feel I'm reinventing the wheel. Is there
a more straightforward way to implement this? Any suggestions would be much
appreciated.

-- 
With kind regards,

Michael Guravage


\showgrid
\setuplayout
  [header=0pt, footer=0pt,
   topspace=0pt, bottomspace=0pt,
   height=middle, width=middle,
   backspace=0pt]

\starttext
  \startluacode
    require "lfs"
    function GetFileExtension(url)
      return url:match("^.+(%..+)$")
    end
    cwd=lfs.currentdir()
    for file in lfs.dir(cwd) do
      suffix = GetFileExtension(file)
      if suffix == ".png" then
        image = figures.getinfo(file).used
        width = image.width or 0
        height = image.height or 0
        aspect = height/width
        if (aspect > 0.9 and aspect < 1.1 )  then
          context.externalfigure({file},{width="\\textwidth"})  -- almost
square
        elseif height < width then
          context.externalfigure({file},{width="\\textheight",
orientation=90}) -- landscape
        else
          context.externalfigure({file},{height="\\textheight"}) -- portrait
        end
        context.page()
      end
    end
  \stopluacode
\stoptext

[-- Attachment #1.2: Type: text/html, Size: 2064 bytes --]

[-- Attachment #2: square_s.png --]
[-- Type: image/png, Size: 3412 bytes --]

[-- Attachment #3: scaleme.tex --]
[-- Type: text/x-tex, Size: 954 bytes --]

\showgrid
\setuplayout
  [header=0pt, footer=0pt,
   topspace=0pt, bottomspace=0pt,
   height=middle, width=middle,
   backspace=0pt]

\starttext
  \startluacode
    require "lfs"
    function GetFileExtension(url)
      return url:match("^.+(%..+)$")
    end
    cwd=lfs.currentdir()
    for file in lfs.dir(cwd) do
      suffix = GetFileExtension(file)
      if suffix == ".png" then
        image = figures.getinfo(file).used
        width = image.width or 0
        height = image.height or 0
        aspect = height/width
        if (aspect > 0.9 and aspect < 1.1 )  then
          context.externalfigure({file},{width="\\textwidth"})  -- Almost square
        elseif height < width then
          context.externalfigure({file},{width="\\textheight", orientation=90}) -- landscape
        else
          context.externalfigure({file},{height="\\textheight"}) -- portrait
        end
        context.page()
      end
    end
  \stopluacode
\stoptext

[-- Attachment #4: landscape_s.png --]
[-- Type: image/png, Size: 3085 bytes --]

[-- Attachment #5: portrait_s.png --]
[-- Type: image/png, Size: 3139 bytes --]

[-- Attachment #6: scaleme.pdf --]
[-- Type: application/pdf, Size: 18461 bytes --]

[-- Attachment #7: 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
___________________________________________________________________________________

             reply	other threads:[~2025-04-24  8:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-24  8:28 Michael Guravage [this message]
2025-04-24 13:02 ` [NTG-context] " Fabrice L

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=CAL-A5LZSQSf2XQ5NECLy6Mcqn2rEFLqAB6K-38haTeHWCHoQMA@mail.gmail.com \
    --to=guravage@literatesolutions.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).