Post all Gimp scripts and script writing questions here
Post a reply

Zoom

Wed Jul 28, 2021 2:23 pm

Hi all,

anybody know a script-fu way to set the zoom of an image to 100% ?

Thanks

Re: Zoom

Wed Jul 28, 2021 8:46 pm

Is it to write a script?

If not -> Just hit the 1 key on your keyboard = 100%
2 = 200%
3 = 400%
and so

Ctrl +Shift + J = fit canvas

Re: Zoom

Thu Jul 29, 2021 1:52 am

Sweeney wrote:Hi all,

anybody know a script-fu way to set the zoom of an image to 100% ?

Thanks


There is none, by design, scripts cannot interact with the UI. But the shortcut to "View->100%" is "1".

Driving Gimp's GUI with PowerShell

Fri Nov 05, 2021 3:26 pm

Hello Sweeney,

1. Run :bowdown PowerShell

Click Windows "Start" > Windows "PowerShell" or "Windows PowerShell ISE"

Before running any PowerShell script, give the following permission one time per session:
Code:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

Copy in the clipboard the above PowerShell command by Ctrl+C.
Then paste it by Ctrl+V in the PowerShell console or the Windows ISE running console.
Don't forget to validate this command by pressing the "ENTER" key in the console.

Dlgbox "Execution Policy Change":
Windows PowerShell wrote:The execution policy helps protect you from scripts that you do not trust.
Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170.
Do you want to change the execution policy?

Click Yes
The change of policy is enabled only during the current session.
If you close Windows ISE or the PowerShell console, the protective policy is again applied.
Remember that the (pre-)historic :shoot MS/DOS :pound console did not have this kind of policy.


2. Run Gimp

Click Windows "Start" > Gimp 2.10.28
Reorganize the Gimp window and the PowerShell ISE or PowerShell console side-by-side in order to see the result of the PowerShell command on Gimp.
Do not overlap the PowerShell console window above the Gimp window.

Gimp menu "File" > "Open" your preferred image you wish to zoom.

Be sure that the zoom is at for example 200%:
Gimp menu "View" > "Zoom" > 2:1 (200%)
or press the shortcut "2" just after the loading of your picture.


3. Define GimpZoom

Copy by Ctrl+C the following PowerShell script defining the function GimpZoom:
Code:
function GimpZoom
{
    param   (
        [parameter(Position=0)][int]$level = 100
            )
    process
    {
        $wsh = New-Object -ComObject WScript.Shell
        $wsh.AppActivate((Get-Process gimp-2.10).Id)   
        start-sleep -Milliseconds 500
        switch ($level)
        {
           1600     {$key = "5" ; Break} # 1600%
            800     {$key = "4" ; Break} #  800%
            400     {$key = "3" ; Break} #  400%
            200     {$key = "2" ; Break} #  200%
             50     {$key = "6" ; Break} #   50%
            default {$key = "1" ; Break} #  100%
        }
        if ($key.Length -gt 0) {$wsh.SendKeys($key)}
        Write-Host "key = $key"
    }
}

The script retrieves the ID of the Gimp 2.10 process.
Then it sends the wished shortcut according to the level parameter.
The COM application is named Windows Script Host (WSH) available in each standard Windows.

Paste by Ctrl+V the content of the clipboard in the PowerShell ISE editor or the PowerShell console:
In the PowerShell IDE, click Run (F5) to define the function GimpZoom.
In the PowerShell console, after the paste, the function GimpZoom is defined.


4. Call GimpZoom

Copy and paste the call of the function with as level parameter the integer 100 meaning 100%: :keybdtype
Code:
GimpZoom -level 100

Do not enter the "%" symbol. :nono

When you validate the call of the function by "ENTER"
the GIMP zoom is set to 100% as required. :geek:

Note: if you wish to reduce the zoom to 50%, the default shortcut "Shift+2" :roll: is too difficult to simulate.
Normally Shift is coded by "+" but on QWERTY keyboard even if the key "2" is directly available,
Shift+2 would generate another character that Gimp does not understand.

That is why it is suggested to replace the Shift+N shortcuts with direct simpler integers such as 6 to 8.
0 and 9 are already used.
That is why for the -level 50, the shortcut 6 is simulated.


5. Define and call GimpZoomLoop :hexer

Finally, optionally copy and paste GimpZoomLoop.
Code:
function GimpZoomLoop()
{
    for ($level = 100; $level -le 1600; $level *= 2)
    {
        GimpZoom -level $level
        start-sleep -Seconds 1
    }
    for ($level = 1600; $level -ge 100; $level /= 2)
    {
        GimpZoom -level $level
        start-sleep -Seconds 1
    }
}

This function calls GimpZoom with level from 100% to 1600% then from 1600% to 100% :wh
sleeping 1 second between two calls.


6. Conclusion about automation testing via the GUI

The PowerShell script GimpZoom does not know when the operation of change of the Gimp zoom is really over.
We need to sleep :zzz during a tempo that you could need to adapt according to the power of your PC.

Driving the Gimp's GUI is strongly dependent on:
  • the Operating System, Windows;
  • the Gimp's shortcut and language;
  • the language of the keyboard;
  • the power of the PC about each tempo.

Synchronizing automatically the PowerShell script with Gimp :hugme is out of the scope of this article. :bawl
For example, reading that the GUI changed
in order to be sure that the current operation is over :rofl
before sending another keystroke. :lmao

For more information, search free tools for automation testing.

Re: Zoom

Fri Nov 05, 2021 5:58 pm

You're out of luck. Can't use Gimp to do zooms.


Image

Infinite Zoom

Sat Nov 06, 2021 6:06 am

Tas_mania, I cannot compete with your Infinite Zoom! :jumpclap

Re: Zoom

Sun Nov 07, 2021 10:52 pm

That is a cool effect, Tas. :)

Re: Zoom

Sun Nov 07, 2021 11:43 pm

eh, eh the zoom only has 6 frames. Done with G'MIC zoom and a batch script I made for the command line version of G'MIC.
Post a reply