Page 1 of 1

### How to use subshell to crop several coordinates in a single process

Posted: 2018-10-25T09:29:04-07:00
Hello to all,

I have these coordinates and the input image below.

coords="110x16+255+175
178x59+29+65
178x59+223+65
178x59+417+65
178x59+611+65
240x151+462+176
240x151+87+257
240x151+366+355
240x151+77+448
240x151+468+542
240x151+140+624"

areas.png

For the first 5 coordinates I apply this code (images 0 to 4):

Code: Select all

``````(
for coord in \$coords; do
echo >&2 "\$coord"
convert areas2.png +repage -crop \$coord +repage -bordercolor black -border 2x2 miff:-
done
) | \
convert - -write mpr:img -delete 0--1 \( mpr:img[1,2] -smush -2 \) \( mpr:img[3,4] -smush -2 \) \
+smush -2 mpr:img[0] +swap -gravity center -background none -smush +3 ImgHeader.png
``````
And for the rest of coordinates (5 to 10), I want to apply the convert command below in consecutive pairs, this is join coordinate 5 with 6, 7 with 8 and 9 with 10.
But it seems I cannot use the first subshell code where was stored the frames in miff memory for the rest of the coordinates.

So, when I run these 3 command failed.

Code: Select all

``````convert - -write mpr:img -delete 0--1 \( mpr:img[5,6]   +smush +6 \) \
+smush -2 LeftRight.png +swap -gravity center -background none -smush +3 ImgTop.png

convert - -write mpr:img -delete 0--1 \( mpr:img[7,8]   +smush +6 \)\
+smush -2 LeftRight.png +swap -gravity center -background none -smush +3 ImgMiddle.png

convert - -write mpr:img -delete 0--1 \( mpr:img[9,10]  +smush +6 \) \
+smush -2 LeftRight.png +swap -gravity center -background none -smush +3 ImgBottom.png
``````
Do I need to do a subshell for each pair of coordinates or how to mix in a single process the operations for all the coordinates?

The outputs would be like this:

ImgTop.png

ImgMiddle.png

ImgBottom.png

LeftRight.png (This is only a helper image to use it to put the header over each resulting image)

Thanks in advance for any help.

### Re: How to use subshell to crop several coordinates in a single process

Posted: 2018-10-25T11:11:44-07:00
cgkas wrote:
2018-10-25T09:29:04-07:00
Do I need to do a subshell for each pair of coordinates or how to mix in a single process the operations for all the coordinates?
You can do everything you have there in a single command by very careful use of nested sets of parentheses. Remember when you set the "-gravity" before your "-smush" operations you need to unset it again with "+gravity" before your crop operations, otherwise your crops won't come out the way you intend.

This command works for me with IM 6.8.9-9 in a bash shell inside the Windows 10 Ubuntu installation...

Code: Select all

``````convert areas2.png -write mpr:img -delete 0--1 \
-background none -bordercolor black \
\( mpr:img -crop 178x59+29+65 \) \( mpr:img -crop 178x59+223+65 \) \
\( mpr:img -crop 178x59+417+65 \) \( mpr:img -crop 178x59+611+65 \) \
-border 2 \( -clone 0,1 -smush -2 \) \( -clone 2,3 -smush -2 \) -delete 0-3 \
-gravity center +smush -2 +gravity \
\( mpr:img -crop 110x16+255+175 -border 2 \) \
+swap -gravity center -smush +3 +gravity -write ImgHeader.png \
\( \( mpr:img -crop 240x151+462+176 \) \( mpr:img -crop 240x151+87+257 \) \
+smush +6 LeftRight.png +swap -gravity center -smush +3 +gravity \
-write ImgTop.png \) -delete 0--1 \
\( \( mpr:img -crop 240x151+366+355 \) \( mpr:img -crop 240x151+77+448 \) \
+smush +6 LeftRight.png +swap -gravity center -smush +3 +gravity \
-write ImgMiddle.png \) -delete 0--1 \
\( \( mpr:img -crop 240x151+468+542 \) \( mpr:img -crop 240x151+140+624 \) \
+smush +6 LeftRight.png +swap -gravity center -smush +3 +gravity \
-write ImgBottom.png \) null:
``````

### Re: How to use subshell to crop several coordinates in a single process

Posted: 2018-10-25T11:38:30-07:00