Ken Y-N
Ken Y-N

Reputation: 15009

Split image and name each half

I have images captured from a stereo camera which I want to split into left and right halves. If I do:

convert stereo0000.png -crop 50%x100% foo.png

I get foo-0.png and foo-1.png; how do I get foo-left.png and foo-right.png. Bonus points if I can do something like:

convert stereo*.png -crop 50%x100% foo-%d.png

And get foo-0left.png, foo-0right.png, foo-1left.png, foo-1right.png, etc. As it stands, that gives me stereo0000.png -> (foo-0.png, foo-1.png), stereo0001.png -> (foo-2.png, foo-3.png), which is not that useful.

Ideally the solution would be just a single call to convert for a wildcard input file pattern, as I can relatively easily write a shell script that renames the pair of files after a single call.

Upvotes: 1

Views: 3007

Answers (3)

Felix Kemeth
Felix Kemeth

Reputation: 116

In bash,

for i in screen*.png; do convert "$i" -gravity East -crop 50%x100%+0+0 "right_$i"; done
for i in screen*.png; do convert "$i" -gravity West -crop 50%x100%+0+0 "left_$i"; done

worked for me.

Upvotes: 1

Matt Sephton
Matt Sephton

Reputation: 4122

Using two convert commands is twice as slow as each file has to be processed twice.

So, it is faster to use one convert and rename the resulting files:

$ convert stereo*.png -crop 50%x100% foo-%01d.png
$ mv foo-0.png foo-left.png
$ mv foo-1.png foo-right.png

Upvotes: 2

Ken Y-N
Ken Y-N

Reputation: 15009

RTFMing a little further, I found this acceptable two-liner:

convert stereo*.png -gravity East -crop 50%x100%+0+0 right%04d.png
convert stereo*.png -gravity West -crop 50%x100%+0+0 left%04d.png

This chops every image into left and right components

Upvotes: 5

Related Questions