Multiple crops from a single image rearranged to create a new image, one-liner with layers

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
imjosh
Posts: 2
Joined: 2016-05-11T20:11:32-07:00
Authentication code: 1151

Multiple crops from a single image rearranged to create a new image, one-liner with layers

Post by imjosh »

I'm trying to take crops from a single image and rearrange them to create a new image.

Here's an example:

Before: http://imgur.com/Z6dLcvN.png

After: http://i.imgur.com/l6iBHbu.png

I was able to do this using:

Code: Select all

convert balls_before.png -crop 170x170+57+50 ball1.png
convert balls_before.png -crop 170x170+257+50 ball2.png
convert balls_before.png -crop 170x170+257+235 ball4.png

convert \
-page 487x451+257+50  ball1.png   \
-page +257+235  ball2.png     \
-page +57+50  ball4.png   \
-background white -layers flatten balls_after.png
However, I'd like to make it one command to avoid reading and writing to disk multiple times.
I was able to use -composite instead of -layers in one line, but my understanding is that -layers would perform better than -composite.

To get -layers working on one line, I tried this (and many, many other things including trying to use -clone), but I can't get anything to work:

Code: Select all

convert \
-page 487x451+257+50  \( balls_before.png  -crop 170x170+57+50 \) \
-page +257+235 \(  balls_before.png  -crop 170x170+257+50 \) \
-page +57+50 \(   balls_before.png -crop 170x170+257+235 \) \
-background white -layers flatten balls_after.png
My version is 7.0.1-3 Q16, Windows command line (syntax converted here for linux).

What am I missing? Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Multiple crops from a single image rearranged to create a new image, one-liner with layers

Post by fmw42 »

I am not sure what is wrong with your command. You should however, put the -page inside the parentheses, though I am having trouble getting that to work. See http://imagemagick.org/script/porting.php#cli

The following works for me on IM 7.0.1.2 with either magick or convert

Code: Select all

convert -size 487x451 xc:white \( Z6dLcvN.png -write mpr:img +delete \) \
\( mpr:img -crop 170x170+57+50 +repage \) -geometry +257+50 -compose over -composite \
\( mpr:img -crop 170x170+257+50 +repage \) -geometry +257+235 -compose over -composite \
\( mpr:img -crop 170x170+257+235 +repage \) -geometry +57+50 -compose over -composite \
balls_after2.png
or

Code: Select all

convert \( Z6dLcvN.png -write mpr:img +delete \) \
\( mpr:img -crop 170x170+57+50 -set page 487x451+257+50 \) \
\( mpr:img -crop 170x170+257+50 -set page 487x451+257+235 \) \
\( mpr:img -crop 170x170+257+235 -set page 487x451+57+50 \) \
-background white -layers flatten balls_after3.png


see http://www.imagemagick.org/Usage/files/#mpr. The use of mpr: creates an in-memory image that can be repeated throughout the command without having to read the image over and over or create clones. Be sure to delete the original after writing the mpr:img, so that you do not carry the original around as well as the mpr:img.
imjosh
Posts: 2
Joined: 2016-05-11T20:11:32-07:00
Authentication code: 1151

Re: Multiple crops from a single image rearranged to create a new image, one-liner with layers

Post by imjosh »

works perfectly. thanks so much
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multiple crops from a single image rearranged to create a new image, one-liner with layers

Post by snibgo »

The OP contains two different versions of a script.

In the first version, each sub-image is first cropped and written to a file (with a canvas offset). Then the "-page" setting is given, and the file is read. The "-page" canvas setting will override the canvas from the crop.

The second version uses a different order. A "-page" setting is given first, then the large image is read, so it will take the canvas setting from the "-page". Then a crop is made from that canvas.

So the effect of the two versions is different. Crop followed by page is different to page followed by crop.

Doing the job with "-page" is possible (I suppose) but massively clumsy. It seems more logical to me to specify first (a) which portion of the original should be cropped, then (b) where in the new canvas that piece should be placed. Fred gives the methods for doing that.
snibgo's IM pages: im.snibgo.com
Post Reply