Need help for merging 2 images from folders of 100 images

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
dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Need help for merging 2 images from folders of 100 images

Post by dav22 »

Hello, I am a newbie and tried variation of script from this forum to make it work for my needs, but so far not successful,
I use ImageMagick-7.0.9-21-Q16-x64-static
running windows 7 64 bit.

I want to merge 2 image files together vertically in a batch, using 100 images from 2 folders, to have 50 new merged images in a new folder.
I have used a script from this forum, it works fine but only for 2 images at the time in a folder, since I am going to get 100 images of this kind over and over to do the job, this way with with this script make it very time consuming, i want to do it automatically when running a batch file.
I am grateful if anyone willing to help.
Here is the script that I put in my batch file:

Code: Select all

convert A_1.JPG B_2.JPG -append AB1.JPG
I used the following script in my batch file, but this stitch them all together,

Code: Select all

convert A_[1-4].JPG B_[1-4].JPG -append abc1.JPG
in this example I put 4 and it did merge 8 pictures together, that is not what I want.

Here is what I want:

in folder-1, I have 50 jpg images, sizes varies around 300 px width,
in folder-2, I have another 50 jpg images that are all the same size ( 300*260) pixel
so I want to merge one image from folder-2 join below an image from folder-1, and if the image in folder-1 is not 300px resize them to become 300 pixel in width, so the end result would have a width of 300 pixel image.

thanks for your help.

User avatar
fmw42
Posts: 26269
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need help for merging 2 images from folders of 100 images

Post by fmw42 »

You could write a .bat script "for" loop over each pair of images. Imagemagick cannot process images the way you might like.

To resize each image to a width of 150 so that when append horizontally they would total 300 pixels, then in the for loop for each pair of images:

Code: Select all

convert imageA imageB -resize 150x -gravity center +append imageAB

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

thank you for your reply,
well I understand the automation through windows batch file, but I want to get a better script to works for number of images, I should be able to do it manually with script.
besides the images should be join vertically not horizontally,

just to make a testing I had to rename image files from both folders and put them in one folder
and having the script like this:

Code: Select all

convert A_[1-2].JPG -append AB-1.JPG
convert A_[3-4].JPG -append AB-2.JPG
convert A_[4-5].JPG -append AB-3.JPG
so image A-1 is from folder-1, in here will merge with image A2 from folder-2, and image A3 from folder-1, will merge with A4 from folder-2.
so I want to have a script in a better way, without renaming the image files and put them in one folder.
thanks.

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

thanks for the code,
I tried formation of your script, and it does merge two files horizontally.

Code: Select all

convert imageA imageB -resize 150x -gravity center +append imageAB

User avatar
fmw42
Posts: 26269
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need help for merging 2 images from folders of 100 images

Post by fmw42 »

dav22 wrote:
2020-02-05T11:49:10-07:00
thanks for the code,
I tried formation of your script, and it does merge two files horizontally.

Code: Select all

convert imageA imageB -resize 150x -gravity center +append imageAB
If you need them merged vertically, then just change +append to -append

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

thanks for your reply,
now it is working fine,
is there a way to make script read images from two different folders for merging?
then I don't have to rename all files and put them in one folder.
thanks
edit: I also mean read from sub-folders as well.

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

while searching through older posts, I found a thread that talked about the same thing I want to do,
I tried the solution, but it does not work for me,
anyone willing to give some tips on make that script working?
http://www.imagemagick.org/discourse-se ... hp?t=29699

Code: Select all

convert \
   FolderA/*.png \
   -gravity north \
   -extent 100%x200% \
   null: \
   FolderB/*.png \
   -gravity south \
   -layers composite \
   -scene 1 \
      FolderC/merged%04d.png
thanks

snibgo
Posts: 12907
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help for merging 2 images from folders of 100 images

Post by snibgo »

You show bash syntax, with \ for line-continuation. Are you running this in a BAT script, or what? For Windows, use caret ^ as line-continuation. If using BAT script, also double the percent % signs.
snibgo's IM pages: im.snibgo.com

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

Thanks for your response,
since I run win 7 on my machine, so if i get a batch file to solve my problem, I use it,
I don't know about commands on batch file either.
I try to learn, do some adjustment and use it.
anyhow, you solved my problem again, thanks for the tips on \ and %, it works now, here is the final script that worked form me:

Code: Select all

convert  ^
   aa/*.jpg  ^
   -gravity north  ^
   -extent 100%%x200%%  ^
   null:  ^
   bb/*.jpg  ^
   -gravity south  ^
   -layers composite  ^
   -scene 1  ^
      cc/merged%%04d.jpg
there is only one problem, I have to resize the first image first, now in this batch script I don't know how it is done,
in my other attempt i use this to resize the image first:

Code: Select all

convert image-1.jpg -resize 300 Img-1.jpg  -append imgAB1.jpg
but now in the above batch script how do I resize the image first?

thanks

snibgo
Posts: 12907
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help for merging 2 images from folders of 100 images

Post by snibgo »

Do you want to resize it before or after the "-extent"? If before, insert "-resize 300" before the "-extent". If after, insert it after.
snibgo's IM pages: im.snibgo.com

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

thanks for the tip on resize,
it works now but I have another problem, as I mentioned I want to merge pictures vertically,
in my example case 300 * 300 top image should merge to another image of 300*240, the merged image suppose to be 300 * 540 pixel,
but imagemagick keep the ratio of 300 * 300 for the second image, the output image is 300*600, it put a white space of 60 pixel in between the pictures to compensate it to become 600 pixel.
I want to keep the size of second pictures as 240 pixel,
how can I prevent image magick to not add the space between?
and have a output image of 540 pixel height.
here is a picture to explain it better.
Thanks.
Image

snibgo
Posts: 12907
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help for merging 2 images from folders of 100 images

Post by snibgo »

For v7, I suggest you use "magick", not "convert".

Your script extends the top image to 200% in the y-dimension, to make room for the bottom image. This assumes the two images have the same height. But they don't.

Your two sets of files are in two directories. Do corresponding files have the same names? If so, then a "for" shell loop would be easy, so you would use one magick command per file pair, and a simple "-append" will work. Something like this Windows BAT script (untested):

Code: Select all

for %%F in (dirA\*.jpg) do (
  magick %%F dirB\%%~nxF -append outdir\%%~nxF
)
EDIT to add: you can insert "-resize 300" or whatever you want after reading the first file, %%F.
snibgo's IM pages: im.snibgo.com

dav22
Posts: 29
Joined: 2020-02-05T09:22:25-07:00
Authentication code: 1152

Re: Need help for merging 2 images from folders of 100 images

Post by dav22 »

thank you very much, that did the job nicely;

Post Reply