snibgo's ImageMagick pages

Assembling with dark paths

Images can be assembled in grid patterns with minimum error boundary cuts.

This page follows from material in Dark paths.

This technique is closely related to Rectangle boundaries with dark paths, and also to Tiling with dark paths.

Cutting patches at their overlaps is an alternative to blending. See Blending tiles.

The method

If we have a number of same-size images, we can assemble them with ImageMagick into a grid pattern by using +append to build each row, then -append to build the output image from all the rows.

However, we might want the input images to overlap each other in the output, with a minimum error boundary cut (MEBC) at each overlap. The script mebcTile.bat does the job.

The first argument is the pattern for the filenames, with XX standing for a number, from zero upwards, with no leading zeros. Then the numbers of columns and rows in the grid must be given. Then the horizontal and vertical overlaps must be given; each may be suffixed with one of '%', 'c' or 'p'. Finally, an output filename may be given.

The script will substitute a number for XX in the filename pattern. It will also set the canvas offsets in each input file. (Hence, the file type must be capable of storing canvas offsets. PNG and MIFF are two such file types.) For each input file, it calls mebcOne.bat which does the hard part.

mebcOne.bat examines the offsets. If the image has no offsets, no cut is made. If it has no x-offset, it is cut on the top edge to overlap with the canvas. If it has no y-offset, it is cut on the left edge to overlap with the canvas. Otherwise, it has both x- and y-offsets, so is cut on both edges.

mebcOne.bat operates much like rectDp.bat (see Rectangle boundaries with dark paths) but is simpler as only two edges may need cutting.

Example

Create four sample input files:

%IMG7%magick ^
  dpt_lvs_sm.jpg ^
  ( +clone ^
    -sigmoidal-contrast 5,20%% ^
    +write dpa_tmp_1.miff ^
    +delete ) ^
  ( +clone ^
    -sigmoidal-contrast 5,70%% ^
    +write dpa_tmp_2.miff ^
    +delete ) ^
  ( +clone ^
    -sigmoidal-contrast 5,90%% ^
    +write dpa_tmp_3.miff ^
    +delete ) ^
  dpa_tmp_0.miff

Assemble them in a 2x2 grid, with 10% overlap in each direction:

call %PICTBAT%mebcTile dpa_tmp_XX.miff 2 2 10c 10c dpa_ass1.png
dpa_ass1.pngjpg

Cleanup

del dpa_tmp_?.miff

Future

Why did I do this with canvas offsets? I forget. I may change it to use command arguments, which would save updating metadata in the input files, and then reading it in mebcOne.bat.

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

mebcTile.bat

rem Assemble images with Minimum Error Boundary Cut.

@rem %1 is input files with XX, eg cr_XX.png, XX will be substituted with an integer.
@rem   Any offsets in inputs will be overridden.
@rem %2 is number of image columns
@rem %3 is number of image rows
@rem %4 is horizontal overlap in pixels (can be suffixed)
@rem %5 is vertical overlap in pixels (can be suffixed)
@rem %6 is output
@rem
@rem Updated:
@rem   18-July-2022 for IM v7.
@rem

@rem Inputs are assumed to be the same size.

@if "%5"=="" findstr /B "rem @rem" %~f0 & exit /B 1

@setlocal enabledelayedexpansion

@call echoOffSave

call %PICTBAT%setInOut %1 mebct


set INFILES=%1

set /A Wm1=%2-1
set /A Hm1=%3-1

set overX=%4
set overY=%5

if not "%6"=="" set OUTFILE=%6

set FIRST_IN=%INFILES:XX=0%

set WW=
for /F "usebackq" %%L in (`%IMG7%magick identify ^
  -format "WW=%%w\nHH=%%h" ^
  %FIRST_IN%`) do set %%L
if "%WW%"=="" exit /B 1

call xyCoord %WW% %HH% %overX% %overY%

echo xycXi=%xycXi% xycYi=%xycYi%

set overX=%xycXi%
set overY=%xycYi%

set TEMP_OUT=mebct.miff
del %TEMP_OUT% 2>nul

set CNT=0
for /L %%y in (0,1,%Hm1%) do (

  for /L %%x in (0,1,%Wm1%) do (
    set INONE=%INFILES:XX=!CNT!%

    rem for /F "usebackq" %%L in (`%IM%identify ^
    rem   -format "offX=%%X\noffY=%%Y" ^
    rem   !INONE!`) do set %%L

    %IMG7%magick ^
      !INONE! ^
      -set page "+%%[fx:%%x*(%WW%-%overX%)]+%%[fx:%%y*(%HH%-%overY%)]" ^
      !INONE!

    call %PICTBAT%mebcOne %TEMP_OUT% !INONE! %overX% %overY%

    if ERRORLEVEL 1 exit /B 1

    set /A CNT+=1
  )
)

%IMG7%magick %TEMP_OUT% %OUTFILE%

call echoRestore

endlocal & set mebctOUTFILE=%OUTFILE%

mebcOne.bat

rem Called from mebcAssemble.bat and mkQuilt.bat, and others.

rem Given %1 is input/output canvas,
rem %2 is image patch to be merged onto canvas, with the required canvas offset,
rem %3 is horizontal overlap in pixels
rem %4 is vertical overlap in pixels
rem merges images possibly with Minimum Error Boundary Cut along top edge or left edge or both.

@rem
@rem Updated:
@rem   14-July-2022 for IM v7.
@rem

@rem FUTURE: accept parameters for offsets, then no need to "identify".
@rem FUTURE: make the blur an environment variable.

@setlocal enabledelayedexpansion

set CANV=%1
set IMG=%2
set overX=%3
set overY=%4

for /F "usebackq" %%L in (`%IMG7%magick identify ^
  -format "WW=%%w\nHH=%%h\noffX=%%X\noffY=%%Y" ^
  %IMG%`) do set /A %%L

echo !IMG! WW=%WW% HH=%HH% offX=%offX% offY=%offY%

set /A xPlusy=%offX%+%offY%

if %xPlusy%==0 (
  echo Cut nothing
  if exist %CANV% (
    %IMG7%magick ^
      %CANV% ^
      %IMG% ^
      -compose Over -layers Merge ^
      %CANV%
  ) else (
    %IMG7%magick ^
      %IMG% ^
      %CANV%
  )

  if ERRORLEVEL 1 exit /B 1

) else if %offX%==0 (
  set /A upY=!offY!-%overY%
  echo Cut top upY=!upY!

  %IM7DEV%magick ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %WW%x%overY%+0+%upY% +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %WW%x%overY%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -alpha off ^
    -compose Difference -composite ^
    -rotate -90 ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -alpha off ^
    -blur 0x0.5 ^
    -rotate 90 ^
    -background White -extent %WW%x%HH% ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

) else if %offY%==0 (
  set /A leftX=!offX!-%overX%
  echo Cut leftX=!LeftX!

  %IM7DEV%magick ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %overX%x%HH%+%leftX%+0 +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %overX%x%HH%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -alpha off ^
    -compose Difference -composite ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -alpha off ^
    -blur 0x0.5 ^
    -background White -extent %WW%x%HH% ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

) else (
  set /A upY=!offY!-%overY%
  set /A leftX=!offX!-%overX%
  rem set /A upY=!offY!
  rem set /A leftX=!offX!

  echo Cut left and top leftX=!LeftX! upY=!upY!

  rem Does this correctly cut left and top "torn" edges?

  %IM7DEV%magick ^
    ^( %CANV% +write mpr:CANV ^
       +repage ^
       -crop %WW%x%overY%+0+%upY% +repage ^
    ^) ^
    ^( %IMG% +write mpr:IMG ^
       +repage ^
       -crop %WW%x%overY%+0+0 +repage ^
    ^) ^
    -colorspace Gray ^
    -alpha off ^
    -compose Difference -composite ^
    -rotate -90 ^
    -process darkestpath ^
    -morphology dilate:-1 2x1+1+0:1,1 ^
    -alpha off ^
    -blur 0x0.5 ^
    -rotate 90 ^
    -background White -extent %WW%x%HH% ^
    ^( ^
      ^( mpr:CANV ^
         +repage ^
         -crop %overX%x%HH%+%leftX%+0 +repage ^
      ^) ^
      ^( mpr:IMG ^
         +repage ^
         -crop %overX%x%HH%+0+0 +repage ^
      ^) ^
      -colorspace Gray ^
      -alpha off ^
      -compose Difference -composite ^
      -process darkestpath ^
      -morphology dilate:-1 2x1+1+0:1,1 ^
      -alpha off ^
      -blur 0x0.5 ^
      -background White -extent %WW%x%HH% ^
    ^) ^
    -compose Multiply -composite ^
    mpr:IMG ^
    +swap ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    mpr:CANV ^
    +swap ^
    -background None ^
    -compose Over -layers Merge ^
    +repage ^
    %CANV%

  if ERRORLEVEL 1 exit /B 1

)

endlocal

All images on this page were created by the commands shown, using:

%IMG7%magick -version
Version: ImageMagick 7.1.0-42 Q16-HDRI x64 396d87c:20220709 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenCL 
Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193231332)

Source file for this web page is dpassem.h1. To re-create this web page, execute "procH1 dpassem".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 10-Dec-2015.

Page created 25-Aug-2022 21:12:37.

Copyright © 2022 Alan Gibson.