loop merge side by side jpgs in Cygwin

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
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

loop merge side by side jpgs in Cygwin

Post by T2ioTD »

Dear All

I am new to both Cygwin and Imagemagic, but I am planning to convert to Linux soon,
thus I would like to work in Cygwin for the moment, so I can take the algorithm with
me.

I have (say one) folder with jpg images (scans of books), and would like to merge them side by side,
like a "dual reading mode), so
a1.jpg will be merged with a2.jpg
a3.jpg will be merged with a4.jpg
etc.

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

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

What is your IM version? Please always provide that. There may be different solutions whether you are using IM 6 or IM 7. How many total images do you have that need appending?

The most portable solution would be to write a shell script to loop over each pair of images and use the command

Code: Select all

convert a1.jpg a2.jpg +append a12.jpg

For new users, see

viewtopic.php?f=1&t=9620
http://www.imagemagick.org/Usage/windows/
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
http://www.imagemagick.org/script/comma ... essing.php
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

My version is ImageMagick-6.7.6

would you please hint me how to make the shell script? I am new to cygwin
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

Here is an example. I have six images in folder test1 on my desktop -- rose1.png ... rose6.png

I set the basename of the image as imgname="rose".

I then change directories to test1 and create an array of all the rose image that end in the suffix .png. (You can change that to your basename a and suffix .jpg).

I get the total number of images. Then divide that by 2 so that I have an even number. If the total was odd, the last one would not be included.

Then I loop over the half number of images (index i) and set the first image to j=2*i+1 and the second image to k=j+1. This way it gets 1 and 2 on the first loop and 3 and 4 on the second loop.

Then I append the two images and write them to a pre-created empty directory test2 at the same level as test1.

Code: Select all

cd
cd desktop/test1
imgname="rose"
arr=(`ls ${imgname}*.png`)
numimg=${#arr[*]}
num=`echo "scale=0; $numimg/2" | bc`
for ((i=0; i<num; i++)); do
j=$((2*i+1))
k=$((j+1))
convert ${imgname}$j.png ${imgname}$k.png +append ../test2/${imgname}${j}_${k}.png
done
cd
If you are going to be using Cygwin or Linux or any Unix-like system, I think you need to learn some shell scripting.

P.S. IM 6.7.6.x is ancient -- almost 200 versions old. I suggest that you upgrade to the latest version. IM 6.7 was undergoing some significant changes and you should upgrade past IM 6.8.5 to avoid bugs during that series of versions. The latest version would be best.
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

fmw42:

I understand I should run this line by line, wright?
when I am at line:
num=`echo "scale=0; $numimg/2" | bc`

cygwin would give me:
$ num=`echo "scale=0; $numimg/2" | bc`
-bash: bc: command not found
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: loop merge side by side jpgs in Cygwin

Post by snibgo »

Fred showed you a complete script. Paste it into a text file, edit as required, and run it.

bc is a common Unix utility. I suggest you install it, with other utilities. My page "IM with Cygwin" contains many similar suggestions.
snibgo's IM pages: im.snibgo.com
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

Thanks guys.
I will have some time studying bash.
then I will study back the solution.

I will definitely read on: http://im.snibgo.com/cygwin.htm
Regards
Last edited by T2ioTD on 2016-09-04T13:06:41-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

When you install Cygwin, you need to install other unix utilities such as bc. I think there is a checkbox when you use the installer to install bc and possibly other unix utilities. You need to go back and re-install or at least install the bc utility. See Snigbo's comments and his web site. He knows more about Cygwin than I do. I am on a Mac and do not use a Windows based PC nor Cygwin.

My script is just a set of commands. You can copy and paste them all into a Cygwin window and they should run if you have bc installed. You do not need to run each line one-at-a-time, but it does not hurt. You would need to add some header lines if you want to save this as a proper bash shell script file and should do so in a Unix-style text editor and not a Windows text editor, because they have different line endings. But this is more advance than you need right now. First learn the basics of unix scripting.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

If you do not want to use the bc calculator, then you can always use the slower IM fx calculator.

So replace

Code: Select all

num=`echo "scale=0; $numimg/2" | bc`
with

Code: Select all

num=`convert xc: -format "%[fx:floor($numimg/2)]" info:`
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

wow! it worked! it worked line by line (cause my bash run.sh is not working) and with
the num=`convert xc: -format "%[fx:floor($numimg/2)]" info:` line

b4n, and again thanks
i will definitely study bash and read on cygwin with imagemagick
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

To make a shell script as a file, you need to have the first line read

Code: Select all

#!/bin/bash
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

Done, but still getting
ls: cannot access 'A'$'\r''*.jpg': No such file or directory

I suspect it is the cd thing... when it worked line by line, I navigated manually to
a location on E:/Test1
(I couldn't get to Desktop, so I cd my way to the folder on E:/Test1 and made my stuff there)

But I cannot replicate the cd E:/Test1 inside the .sh file...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: loop merge side by side jpgs in Cygwin

Post by fmw42 »

You probably need the full path from E:. Sorry I do not know your directory structure. Is Test1 at the root level of E:? If not then you need to provide the full path.
'A'$'\r''*.jpg'
Why do you have a \r in your file name? Note the ` are backtick marks, not single quotes.

In unix, they are special characters.

In place of

Code: Select all

arr=(`ls ${imgname}*.png`)
your can use

Code: Select all

arr=($(ls ${imgname}*.png))
to avoid the backticks. But you cannot replace them with single quotes.
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: loop merge side by side jpgs in Cygwin

Post by T2ioTD »

thank... I will look at this tommorow after work..
bye till then
Post Reply