Skip to main content

PyPI GitHub Conda DOI

License Tests Documentation Downloads

Try JupyterLite

laserbeamsize is a Python library for fast and reliable calculation of laser beam sizes from images. It implements the ISO 11146 variance method with enhancements that reduce sensitivity to background offset, noise, and nonideal beam profiles.

The package also provides automated tools for estimating M² from beam diameter measurements taken at multiple propagation distances.

Extensive documentation is available at:

https://laserbeamsize.readthedocs.io

Features

  • Robust beam size extraction using the ISO 11146 variance method

  • Automatic estimation of centroid, major/minor axes, and rotation

  • Works with Gaussian, astigmatic, and strongly non-Gaussian beams (TEM modes)

  • Handles 8-, 12-, and 16-bit grayscale images

  • One-function visual reporting tools

  • Convenient functions for M² fitting and plotting

  • Minimal dependencies

Installation

Install using pip:

pip install laserbeamsize

The image-loading examples below also use imageio:

pip install imageio

Or with conda:

conda install -c conda-forge laserbeamsize

You can also run the library immediately in the browser using JupyterLite:

Try JupyterLite

Quick Start: Determining Beam Size

Example showing how to compute beam center, major/minor axes, and rotation.

import imageio.v3 as iio
import matplotlib.pyplot as plt
import laserbeamsize as lbs

data_url = "https://raw.githubusercontent.com/scottprahl/laserbeamsize/main/docs/images/"
file = data_url + "t-hene.pgm"
beam = iio.imread(file)

x, y, d_major, d_minor, phi = lbs.beam_size(beam)

print(f"Beam center: ({x:.0f}, {y:.0f})")
print(f"Major axis:  {d_major:.0f} pixels")
print(f"Minor axis:  {d_minor:.0f} pixels")
print(f"Rotation:    {phi * 180/3.1416:.0f}° CCW")

results in:

Beam center: (651, 492)
Major axis:  369 pixels
Minor axis:  347 pixels
Rotation:    -12° CCW

To produce an annotated visual report.

lbs.plot_image_analysis(beam)
plt.show()

Resulting example:

HeNe beam analysis example

Astigmatic and Non-Gaussian Beams

The analysis also works for beams that are not well approximated by a Gaussian. For example, a TEM02 mode.

import imageio.v3 as iio
import matplotlib.pyplot as plt
import laserbeamsize as lbs

data_url = "https://raw.githubusercontent.com/scottprahl/laserbeamsize/main/docs/images/"
tem02 = iio.imread(data_url + "TEM02_100mm.pgm") >> 4  # 12-bit data stored in 16-bit container
lbs.plot_image_analysis(tem02, title="TEM$_{02}$ at z=100 mm", pixel_size=3.75)
plt.show()

Example output:

TEM02 beam example

M² Determination

Estimating M² requires beam diameters at several locations along the propagation axis. The beam diameters should be measured within ±1 Rayleigh distance and more than ±2+ Rayleigh distances.

import matplotlib.pyplot as plt
import numpy as np
import laserbeamsize as lbs

lambda1 = 308e-9  # meters
z1 = np.array([-200,-180,-160,-140,-120,-100,-80,-60,-40,-20,0,20,40,60,80,99,120,140,160,180,200]) * 1e-3
d1 = 2*np.array([416,384,366,311,279,245,216,176,151,120,101,93,102,120,147,177,217,256,291,316,348]) * 1e-6

lbs.M2_radius_plot(z1, d1, lambda1, strict=True)
plt.show()

Example fit:

M² fit example

Example: Non-ISO-Conforming Measurement Set

The following analysis uses images collected at non-ideal distances but illustrates the fitting process. The beam is a HeNe laser operating predominantly in the TEM01 mode and rotated -52°

import imageio.v3 as iio
import matplotlib.pyplot as plt
import numpy as np
import laserbeamsize as lbs

data_url = "https://raw.githubusercontent.com/scottprahl/laserbeamsize/main/docs/images/"
lambda0 = 632.8e-9  # meters
z = np.array([247, 251, 259, 266, 281, 292]) * 1e-3  # meters
filenames = [data_url + f"sb_{number * 1e3:.0f}mm_10.pgm" for number in z]

# the 12-bit pixel images are stored in high-order bits in 16-bit values
tem10 = [iio.imread(name) >> 4 for name in filenames]

# remove top to eliminate artifact
for i in range(len(z)):
    tem10[i] = tem10[i][200:, :]

# find beam in all the images rotated -52° and create arrays of beam diameters
options = {
    "pixel_size": 3.75,
    "units": "µm",
    "crop": [1400, 1400],
    "z": z,
    "phi_fixed": np.radians(-52),
    "iso_noise": False
}

d_major, d_minor = lbs.plot_image_montage(tem10, **options)
plt.show()

Example montage output:

Montage of beam images

Plotting the M² Fit

Continuing with the measured diameters returned by the montage:

lbs.M2_diameter_plot(z, d_major * 1e-6, lambda0, d_minor=d_minor * 1e-6)
plt.show()

In the example, the dashed curve indicates the ideal divergence of a Gaussian beam. The fitted M² of approximately 2.6 ± 0.7 agrees with the expected value of 3 for a TEM10 mode, though the undersampling results in underestimated divergence.

M² divergence comparison plot

Citation

If you use laserbeamsize in academic, instructional, or applied technical work, please cite:

Prahl, S. (2026). laserbeamsize: ISO 11146 Calculation of Laser Beam Center, Diameter, and M² (Version 2.4.1) [Computer software]. Zenodo. https://doi.org/10.5281/zenodo.8346799

BibTeX

@software{laserbeamsize_prahl_2026,
  author    = {Scott Prahl},
  title     = {laserbeamsize: ISO 11146 Calculation of Laser Beam Center, Diameter, and M²},
  year      = {2026},
  version   = {2.4.1},
  doi       = {10.5281/zenodo.8346799},
  url       = {https://github.com/scottprahl/laserbeamsize},
  publisher = {Zenodo}
}

License

laserbeamsize is distributed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

laserbeamsize-2.5.0.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

laserbeamsize-2.5.0-py3-none-any.whl (42.8 kB view details)

Uploaded Python 3

File details

Details for the file laserbeamsize-2.5.0.tar.gz.

File metadata

  • Download URL: laserbeamsize-2.5.0.tar.gz
  • Upload date:
  • Size: 73.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for laserbeamsize-2.5.0.tar.gz
Algorithm Hash digest
SHA256 6fa66736af04a79973fd43ab9188cb14eff1d4b899ca4c7c1937e8cf7b488742
MD5 240ce13b62ab2c2a5d9fc586ec3303f1
BLAKE2b-256 ec8ca4ed1b3ccb2223a6c3af697bf96265aa37eaf419edaa9e897e23656e4bc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for laserbeamsize-2.5.0.tar.gz:

Publisher: pypi.yaml on scottprahl/laserbeamsize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file laserbeamsize-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: laserbeamsize-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 42.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for laserbeamsize-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c5fe171e9f403fde43088052a2a8044e50c8711501c709405c6d1268fc9873f
MD5 ad565eff1d97042b23c4e12d36c5b0ae
BLAKE2b-256 ea5b2b3d0722dc893d829aafa42b29d4e14b22a97d2bf9deefb6235f0ed07fec

See more details on using hashes here.

Provenance

The following attestation bundles were made for laserbeamsize-2.5.0-py3-none-any.whl:

Publisher: pypi.yaml on scottprahl/laserbeamsize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 files

2.4.1

2 files

2.4.0

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.0

2 files

2.1.0

2 files

2.0.5

2 files

2.0.4

2 files

2.0.3

2 files

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.9.4

2 files

1.9.3

2 files

1.9.2

2 files

1.9.1

2 files

1.9.0

2 files

1.8.0

2 files

1.7.3

2 files

1.7.2

1 file

1.7.1

1 file

1.7.0

1 file

1.6.1

1 file

1.6.0

1 file

1.5.0

1 file

1.2.0

1 file

1.1.0

1 file

1.0.3

1 file

1.0.2

1 file

1.0.1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page