Combining many pairs of pics within a folder

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
fjf
Posts: 4
Joined: 2017-07-18T10:15:21-07:00
Authentication code: 1151

Combining many pairs of pics within a folder

Post by fjf »

Hello. I am trying to use this wonderful tool and I have succeeded in doing what I need, but slowly. Working in MAC OSX, I need to combine pairs of pics within a folder; they are named (something)_001.jpg and (something)_002.jpg. I merge them by using convert +append or -append, but this is slow, working with only two at the time. Is there any way to issue a command within the folder that does it automatically?. I tried : convert +append *_001.jpg *_002jpg *.jpg, but it does not work.

Any help would be appreciated. Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining many pairs of pics within a folder

Post by fmw42 »

Are "something" only in pairs. That is do you have XXX_001.jpg XXX_002.jpg and no other XXX_?

Please provide your IM version.

Can you provide a (partial) list of filenames in your folder, so we can see how they are listed in sequence.

You will likely have to write a bash script loop over the files in your directory and combine them in pairs. But you could do that by simply combining each successive pair or you might have to search for each pair of "something" names.
fjf
Posts: 4
Joined: 2017-07-18T10:15:21-07:00
Authentication code: 1151

Re: Combining many pairs of pics within a folder

Post by fjf »

Something like:

2017_07_15_19_25_22_001.jpg
2017_07_15_19_25_22_002.jpg
2017_07_15_19_25_25_001.jpg
2017_07_15_19_25_25_002.jpg
...

And so on. Yes, I think the simplest would be to just combine each successive pair, because the numbers in the filenames change (those are just scan times).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combining many pairs of pics within a folder

Post by fmw42 »

If you have no skips, then you could do something like the following. Lets call your image directory, test. Create a new directory at the same level to hold your output images, if you want. Then do the following script, which you can copy and past into a terminal window. First change directories to your test directory.

Code: Select all

cd path2/test
arr=(`ls *.jpg`)
num=${#arr[*]}
for ((i=0; i<num; i=i+2)); do
j=$((i+1))
img1=${arr[$i]}
img2=${arr[$j]}
name=${img1%_001.jpg}
convert $img1 $img2 +append ../test2/${name}_001_002.jpg
done
fjf
Posts: 4
Joined: 2017-07-18T10:15:21-07:00
Authentication code: 1151

Re: Combining many pairs of pics within a folder

Post by fjf »

I am speechless. It just works.

Thankyou. Thankyou. Thankyou. Thankyou. Thankyou.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Combining many pairs of pics within a folder

Post by GeeMack »

fjf wrote: 2017-07-18T13:27:15-07:00And so on. Yes, I think the simplest would be to just combine each successive pair, because the numbers in the filenames change (those are just scan times).
There are several ways to accomplish this, but the best approach for your needs would depend on several factors. If the mated images are the same sizes, if all the images in the directory can fit in the memory you have available, and if you're using IM 7 it could be this simple...

Code: Select all

magick *_001.jpg -extent %[fx:s.w*2] null: *_002.jpg -gravity east -layers composite _%03d_out.png
That would read in two stacks of images, the 001's and the 002's. It would extend the 001 images to double their width on the right side. Then it would composite the corresponding 002 image onto that extended section. The result would be images with filenames _001_out.png, _002_out.png, _003_out.png, etc., each with the 001 image on the left and its 002 mate on the right.

Edited to add: Using IM6 on a *nix system this command would accomplish pretty much exactly the same thing...

Code: Select all

convert *_001.jpg -set option:distort:viewport %[fx:s.w*2]x%[fx:s.h] \
   -distort SRT 0 null: *_002.jpg -gravity east -layers composite _out_%03d.png
fjf
Posts: 4
Joined: 2017-07-18T10:15:21-07:00
Authentication code: 1151

Re: Combining many pairs of pics within a folder

Post by fjf »

It also works. Now I have two different ways of doing it. Thanks for all the help. Amazing site, great software and VERY helpful people here!!.
Post Reply