Search code examples
imagemagickimagemagick-convert

Overlapping pixels with convert -crop


Suppose i have a long chat screen which i want to split into the chunks. But i want overlapping between chunks for editing them after with Gimp.

I use right now like this:

convert chat.png -crop 100%x25% +repage chat_%d.png

How to create overlapping with pixels or percentages between them like 50px for example? That first screen have 50px of top of next screen and etc.

I have tried to find the solution, but cannot to find.

I need linux version syntax. Pls. Below is windows cmd one. How to convert it?


Solution

  • As you want a Linux solution, I wrote a little bash script. You can edit the number of chunks and the number of lines to overlap near the top of the script. You can also edit the code to pass the filename, number of chunks and overlap in as parameters:

    #!/bin/bash
    
    # User editable values
    image="image.jpg"   # filename of image
    overlap=50          # vertical overlap in pixels
    chunks=6            # number of output images
    
    # Get image height and work out basic lines per chunk excluding overlap
    h=$(identify -format %h image.jpg)
    lpc=$((h/chunks))
    echo "DEBUG: height=$h, Lines per chunk=$lpc"
    
    for ((chunk=0;chunk<$chunks;chunk++)) ; do
       (( top=chunk*lpc ))
       (( lines=lpc + 2 * overlap ))
       # First and last chunks only have 1 overlapped edge
       [ $chunk == 0 ] || [ $chunk == $((chunks-1)) ] && (( lines=lpc + overlap ))
       
       echo "DEBUG: chunk=$chunk, top=$top, height=${lines}"
       magick "$image" -crop x${lines}+0+${top} "chunk-${chunk}.jpg"
    done
    

    I am using this image:

    enter image description here

    Photo by Dave on Unsplash

    I get these results:

    DEBUG: height=2688, Lines per chunk=448
    DEBUG: chunk=0, top=0, height=498
    DEBUG: chunk=1, top=448, height=548
    DEBUG: chunk=2, top=896, height=548
    DEBUG: chunk=3, top=1344, height=548
    DEBUG: chunk=4, top=1792, height=548
    DEBUG: chunk=5, top=2240, height=498
    

    enter image description here