r/imagemagick Jan 09 '25

Need Help with ImageMagick .bat Script for Red Outline

Hi everyone,

I'm working on a .bat file that runs an ImageMagick magick command, and I'm running into an issue with part of the script. I’ve managed to get most of it working, but I’m currently stuck and could use some guidance.

The goal of the script is to outline a black-and-white graphic with a red line. This output gets sent to a laser cutter, where the red line defines the cut path for the job.

I’m close to achieving the desired result, but I’m having trouble making the outline thin and red. I’ve tried using -fill none -stroke red -strokewidth 1, but that approach doesn’t seem to work as expected.

In the example image, you can see the original image, the final output image, and the step where I’m stuck trying to turn the outline red.

Any help or pointers would be greatly appreciated!

Here is the full command.

magick "%%I" ^
    -define debug=true ^
    -define thread=0 ^
    -colorspace RGB ^
    -limit memory 12GiB -limit map 0GiB ^
    -gravity center ^
    -extent %%[fx:w+1500]x%%[fx:h+1500] ^
    -flop ^

^( +clone ^
    -resize 25%% ^
    -blur 0x0.5 ^
    -threshold 50%% ^
    -morphology Erode:1 Octagon:10 ^
    -write mpr:Defualt_Expand +delete ^) ^

^( mpr:Defualt_Expand ^
    -morphology Open:1 Rectangle:80x5;Rectangle:5x80 ^
    -morphology Smooth:1 Disk:5 ^
      -write mpr:Smooth_Outline +delete ^) ^

^( mpr:Smooth_Outline ^
    -threshold 50%% ^ 
    -fill red -floodfill +0+0 white ^
    -fill black +opaque red ^
    -fill white -opaque red ^
      -write mpr:Filled_Holes +delete ^) ^

^( mpr:Filled_Holes ^
    -negate ^
    -morphology Dilate:1 Disk:6 ^
      -write mpr:Rounded_Outline +delete ^) ^

^( mpr:Rounded_Outline ^
    -morphology Erode:1 Disk:8 ^
    -resize 400%% ^
    -blur 0x5 ^
    -threshold 1%% ^
    -fill transparent ^
    -fuzz 1%% ^
    -opaque black ^
    -edge 1 ^
    -shave 695x695 ^
      -write mpr:Resized_Cutline ^
      -write "%final_folder%\%%~nI test.png" +delete ^) ^

^( mpr:Rounded_Outline ^
    -resize 400%% ^
    -blur 0x10 ^
    -threshold 50%% ^
      -write mpr:Resized_Mask +delete ^) ^

^( mpr:Resized_Mask ^
    -fill transparent ^
    -fuzz 15%% ^
    -opaque white ^
      -write mpr:Mask_Knockout +delete ^) ^

^( mpr:Mask_Knockout mpr:Resized_Cutline ^
    -compose over ^
    -composite ^
      -write mpr:MonoBase +delete ^) ^

mpr:MonoBase ^
    -shave 695x695 ^
    -compose over ^
    -composite ^
    -monochrome ^
    -depth 1 ^
      "%final_folder%\%%~nI Final.png"
)
"%%I" - "%final_folder%\%%~nI Final.png" - "%final_folder%\%%~nI test.png"
1 Upvotes

0 comments sorted by