This project provides a Gwyddion process module that readds python to Gwyddion via a c module. For a windows user, all they need to do is copy the supplied .dll file into the gwyddion modules folder, and then supply the path to the python executable and python script and they shall be ready to execute that code in gwyddion on its next launch.
Data Process → STM → Run Python Script
When clicked, it:
- exports the current channel to a temporary
in.npy - runs your Python:
python script.py in.npy out.npy - loads
out.npyand overwrites the current channel
Windows Win64 only in this repo release (contains a .dll).
No Linux/macOS binaries are included.
Tested on windows with Gwyddion 2.69
Your script must accept:
python process.py <in.npy> <out.npy>
- input:
float64NumPy array, shape(ny, nx) - output: must write
float64array with the same shape
Minimal template:
import sys
import numpy as np
import yaml
import os
def process(img: np.ndarray, cfg: dict) -> np.ndarray:
# TODO: your logic here
# REMEMBER: It can depend on a config file! so if you would like to iteratively
# tweek a process on a particular file, that is possible by modifying and saving
# the config file (or of course, by directly modifying the python script)
return img.astype(np.float64)
def main():
inpath, outpath = sys.argv[1], sys.argv[2]
img = np.load(inpath)
# here we suppose that you add a config.yml somewhere, and then
# add the config.yml path to the .bat environment variables (explained below)
cfg_path = os.environ.get("GWY_PYTHON_CONFIG", "")
cfg = {}
if cfg_path:
with open(cfg_path, "r") as f:
cfg = yaml.safe_load(f) or {}
out = process(img, cfg)
np.save(outpath, out.astype(np.float64))
if __name__ == "__main__":
main()The module does not compile your Python into the DLL.
Each click spawns Python using environment variables set when Gwyddion starts:
GWY_PYTHON_EXE= full path topython.exe(e.g. your conda env python)GWY_PYTHON_SCRIPT= full path to your.pyscript- (optional)
GWY_PYTHON_CONFIG= full path to a config file your script loads
This means:
- Editing the
.pyfile does not require recompiling the DLL. - Editing a
config.ymlthe script reads changes behaviour immediately on next click (even while Gwyddion stays open). - Changing
GWY_PYTHON_EXE/GWY_PYTHON_SCRIPTusually requires restarting Gwyddion (env vars are captured at process start).
Copy the DLL (located in precompiled/windows/threshold-example.dll) into:
Windows user modules folder
C:\Users<YOU>\gwyddion\modules\process\
Example:
C:\Users\rnpla\gwyddion\modules\process\threshold-example.dll
Note: Some builds may also use
C:\Users\<YOU>\.gwyddion\..., but on Windows the folder that actually worked here isC:\Users\<YOU>\gwyddion\....
in precompiled/Windows there is a file 'run_gwy_py.bat'. save this file to your desktop screen (this is your new gwyddion launcher), and edit lines 4 and 5to be the path to the python executable that runs your script, and the path to the python script respectively:
set "GWY_PYTHON_EXE=C:\your\path\to\python.exe"set "GWY_PYTHON_SCRIPT=C:\your\path\to\your\python-script\example-script.py" You are now done. Double click the bat script just like you would with the gwyddion desktop short cut and your python script will be a module inside Data Process → STM → Run Python Script
This repo includes two demo scripts you can run via the Gwyddion Python bridge.
- A simple “obvious” test: it clips the image to the 1–99 percentile, then wipes the right half by filling it with a constant derived from the left half.
- The provided
precompiled/windows/run_gwy_py.batalready points totool_python_scripts\absurd_process.py. - its an absurd script haha, only meant to show you that it works, however you can begin to see how it manipulates the data.
- Applies a segmentation pipeline with:
- global plane fit + flattening
- TV denoise + Gaussian smoothing + Sobel edge map
- interactive marker selection (TkAgg popup) to seed watershed
- To run it, set
GWY_PYTHON_SCRIPTin your launcher BAT topath\to\quicksegment.py(after downloading the script and the pyproject.toml) and install the necessary dependencies by runnningpip install -e .in the directory containing the pyproject.toml file. Ensure your Python env hastkinteravailable for the GUI. Then, as before, once you set the python executable path in your .bat launcher, you'll be ready to go.
This project contains code derived from/compatible with Gwyddion module templates and uses Gwyddion APIs.