Search code examples
cropoffsetimagemagick-convert

imagemagick: How to crop a picture and paste it elsewhere on the same picture?


What I want:

Using ImageMagick I crop a part of the picture and then paste it somewhere else on the same picture.

Eg: enter image description here

What I tried:

convert in.jpg -page +0+300 \( -clone 0 -crop 650x600+0+300 \) -flatten out.jpg

Result: enter image description here

The issue:

The cropping starts from (0,0) of the image. I can't figure out a way use an offset.

How can I crop a portion that is in the middle of a picture and paste it somewhere else on the same picture using ImageMagick?


Solution

  • In ImageMagick, you do that using -gravity and -geometry with -composite.

    Here I set -gravity center for the crop, then -gravity northwest for the composite. I crop a region from the center and move it to the right:

    convert pic.jpg \
    \( +clone -gravity center -crop 650x600+0+0 +repage \) \
    -gravity northwest -geometry +1200+300 \
    -compose over -composite result.jpg
    


    enter image description here

    Here I set -gravity center for the crop, then -gravity center for the composite. I crop a region from the center and move it to the right.

    convert pic.jpg \
    \( +clone -gravity center -crop 650x600+0+0 +repage \) \
    -gravity center -geometry +500-100 \
    -compose over -composite result2.jpg
    


    enter image description here