Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Stitch a planar set of tiles into a mosaic

Project description


Some features in this script require OpenCV 3.0 and ImageMagick.

Stitch2D is a Python script that can be used to stitch a two-dimensional grid of tiles into a mosaic. You can install it from the command line using pip:

pip install stitch2d


Users can set offsets manually (if the offset is regular) or automatically (if OpenCV is installed). If OpenCV is used, tiles that cannot be placed confidently are excluded from the final mosaic. Tiles are not warped in either case.

The options available in this module are fairly basic. For more complex tilesets, consider using the image stitching plugin in Fiji.

Documentation is available at ReadTheDocs.

Using the Command Line Tools

Example tileset for mosaic (18.5 MB; 8 columns, snaked)

Begin by collecting the tilesets you want to stitch as subdirectories in a single folder. Each subdirectory processed with a single command will be processed use the same parameters, so offsets for the different tilesets should be very identical.

There are four subcommands that can be accessed from the command line: mosaic, composite, organize, and select. In addition to the text below, information about of these commands can be accessed from the command line using -h.


Use the mosaic subcommand to stitch together a set of tiles. The resulting mosaic is saved in the parent of the directory containing the source tiles. From the command line:

stitch2d mosaic

You can specify additional keyword arguments to control how tiles are stitched:

stitch2d mosaic -path /path/to/tiles -matcher brute-force -scalar 0.5 -threshold 0.7 --equalize_histogram --create_jpeg

Optional arguments include:

  • -path: Specifies to path to the source tiles. This argument works in all subcommands except organize. If no path is specified, you will be prompted to select a directory.
  • -numcols: Specifies the number of columns in the mosaic. If not provided, the user will be prompted for this information.
  • –raster, –snake: Specifies whether tileset is rastered or snaked. If neither argument is provided, the user will be prompted for this information.
  • –create_jpeg: Specifies whether to create a half-size JPEG derivative of the final mosaic.
  • –manual: Force manual selection of offsets. The script will default to manual matching if OpenCV is not installed.

The following arguments can be used to tweak the behavior of OpenCV:

  • -matcher: Specifies the algorithm used for feature matching. Must be either “brute-force” or “flann”; “brute-force” is the default. Note: The flann matcher has proven unreliable and is currently disabled.
  • -scalar: Specifies the amount by which to resize source tiles before attempting to match features. Must be a decimal between 0 and 1; the default value is 0.5. Smaller values are faster but potentially less accurate. The mosaic itself will be made from the full-sized tiles.
  • -threshold: The threshold for the Lowe test. Must be a decimal between 0 and 1; the default value is 0.7. Lower values give fewer but better matches.
  • –equalize_histogram: Specifies whether to try to equalize histogram in the source image. This can increase contrast and produce better matches, but increases computation time.

These keywords can also be passed directly stitch2d.mosey().

More information about OpenCV parameters can be found in the OpenCV-Python tutorials.


Makes a composite image by recoloring and overlaying SEM element maps with data for different elements.

stitch2d composite -path /path/to/mosaics -red Fe -green Mg -blue Al

The composite commands accepts the -path, -label, and -create_jpeg arguments as defined above for mosaic. Additional arguments are used to map colors to elements. Valid color arguments include -red, -green, -blue, -cyan, -magenta, -yellow, -black, and -white. Pixels in the images being composited should be tints/shades of these eight colors.

The composite function uses the filename to determine which element is pictured, so the filename of each mosaic should end with _{element}.


Use the selector tool to select tiles to exclude from future SEM element mapping. This tool does the following:

  • Creates a points file for use with Noran System Seven. File contains the center point of each tile that was kept from the original grid. The points file has not been tested.
  • Moves excluded tiles to a directory in the source folder. These tiles are automatically reintegrated if the selection script is run again.
  • Produces a list of tiles to skip. The mosaic script uses this list to fill in gaps in the mosaic where the excluded tiles were removed.
  • Produces a screenshot showing the final selection grid.

To use the select script:

stitch2d select

Click the tiles you’d like to remove, or click a darkened tile to reinstate it. As with the mosaic script, the select command accepts an optional path argument using the -path flag.


This command organizes element maps produces by Noran System Seven into element-specific folders suitable for mosaicking. It accepts optional arguments for the source and destination directories:

stitch2d organize /path/to/source /path/to/destination

Project details

Download files

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

Files for stitch2d, version 0.31
Filename, size File type Python version Upload date Hashes
Filename, size stitch2d-0.31.tar.gz (165.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page