Search code examples
imagemagickimage-manipulation

How to copy an image line and insert it at specified positions n-times with imagemagick?


I have some pixel based images that I would like to manipulate with imagemagick in the following way.

Each image follows the same pixel-line format:

aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb
ccccccccccccccc
ccccccccccccccc
bbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb
aaaaaaaaaaaaaaa

So what I would like to do is to copy the first bbbb line and insert it after last b line before the c lines and after the last b lines after the c lines. This will expand the images height. This process should be repeated n-times.

This means that the a and c lines part will be left untouched, while the b parts will blow up the height of the image.

After reading imagemagick option summary I have no clue how this could be done.

So any kind of help would be appreciated.


Solution

  • Thanks to @fmw42 I could script the following solution which gets the image size and then calculates the number of lines to increase the image to a height of 48/49 pixel. First it adds the lines at the image bottom and after this it adds the same number of lines at the image top. While keeping the first/last lines.

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    set image=Welcome1.gif
    rem use %1 for comanndline argument
    
    set image2=%TEMP%\temp1.gif
    set image3=%TEMP%\temp2.gif
    
    magick identify -format "%%w" %image% > %TEMP%\imwidth.txt
    set /P width=<%TEMP%\imwidth.txt
    del %TEMP%\imwidth.txt
    
    magick identify -format "%%h" %image% > %TEMP%\imheight.txt
    set /P height=<%TEMP%\imheight.txt
    del %TEMP%\imheight.txt
    
    set /A addlines=(49 - %height%) / 2
    set /A lastline=%height% - 2
    
    magick %image% -crop %width%x1+0+2 %TEMP%\copyLine.gif
    
    rem Add lines at end of image
    set image1=%image%
    FOR /L %%i IN (1,1,%addlines%) DO (
      magick !image1! -background black -splice 0x1+0+%lastline% %image2%
      magick composite %TEMP%\copyLine.gif %image2% -geometry 99x1+0+%lastline% %image3%
      del %TEMP%\%image%
      ren %image3% %image%
      set image1=%TEMP%\%image%
    )
    
    rem Add lines at top of image
    set image1=%TEMP%\%image%
    FOR /L %%i IN (1,1,%addlines%) DO (
      magick !image1! -background black -splice 0x1+0+2 %image2%
      magick composite %TEMP%\copyLine.gif %image2% -geometry 99x1+0+2 %image3%
      del %TEMP%\%image%
      ren %image3% %image%
      set image1=%TEMP%\%image%
    )
    copy %TEMP%\%image% %image:~0,-4%touch.gif
    
    
    del %TEMP%\copyLine.gif %TEMP%\temp1.gif %TEMP%\temp2.gif %TEMP%\%image%
    endlocal
    exit /b 0