loop merge side by side jpgs in Cygwin
loop merge side by side jpgs in Cygwin
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
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
- 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
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
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
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
Re: loop merge side by side jpgs in Cygwin
My version is ImageMagick-6.7.6
would you please hint me how to make the shell script? I am new to cygwin
would you please hint me how to make the shell script? I am new to cygwin
- 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
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.
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.
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
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.
Re: loop merge side by side jpgs in Cygwin
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
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
-
- 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
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.
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
Re: loop merge side by side jpgs in Cygwin
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
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.
- 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
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.
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.
- 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
If you do not want to use the bc calculator, then you can always use the slower IM fx calculator.
So replace
with
So replace
Code: Select all
num=`echo "scale=0; $numimg/2" | bc`
Code: Select all
num=`convert xc: -format "%[fx:floor($numimg/2)]" info:`
Re: loop merge side by side jpgs in Cygwin
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
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
- 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
To make a shell script as a file, you need to have the first line read
Code: Select all
#!/bin/bash
Re: loop merge side by side jpgs in Cygwin
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...
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...
- 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
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.
In unix, they are special characters.
In place of
your can use
to avoid the backticks. But you cannot replace them with single quotes.
Why do you have a \r in your file name? Note the ` are backtick marks, not single quotes.'A'$'\r''*.jpg'
In unix, they are special characters.
In place of
Code: Select all
arr=(`ls ${imgname}*.png`)
Code: Select all
arr=($(ls ${imgname}*.png))
Re: loop merge side by side jpgs in Cygwin
thank... I will look at this tommorow after work..
bye till then
bye till then