Search code examples
image-processingimagemagickimage-manipulationimagemagick-convert

slice image based on horizontal lines


Hope someone can help me on this or just to confirm if this is actually doable or not.

I've a PNG file that has horizontal lines. I'd like to split the image into smaller files based on these lines. How can I achieve this? Is this possible using imageMagick? Or from any other application for that matter that would allow it be automatic and batch processable.

I'm attaching the files as well so that it is more clear.

Thanks

Original file: enter image description here

Sliced Part 1: enter image description here

Sliced Part 2: enter image description here

Sliced Part 3: enter image description here

Sliced Part 4: enter image description here


Solution

  • Bash script:

    #!/bin/sh
    w=$(identify -ping -format '%[width]' green_lines.png)
    h=$(identify -ping -format '%[height]' green_lines.png)
    convert green_lines.png -resize 1x"${h}"\! -resize "${w}"x"${h}"\! out_green.png
    convert out_green.png -colorspace HSV -channel S -separate  out_greenS.png
    convert out_greenS.png -threshold 50% -negate out_greenS.png
    
    convert out_greenS.png -define connected-components:verbose=true \
    -define connected-components:area-threshold=500000 \
    -connected-components 8 objects.png| awk '{print $2}'|tail -n +2 >coodr.txt
    i=0
    while read size
    do 
    convert green_lines.png -crop "${size}"  crop_"${i}".png
    i=$((i+1))
    done < coodr.txt