How To: Using Cygwin and Bash Scripts (e.g., Fred's) with IM

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by jwtrexler »

I appreciate your help, but I need to ask for a little more guidance as some of the Pointers would be more familiar to a computer programmer than myself. Particularly, I need a better understanding of Pointers 2 and 3.

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Pointers for use:

Be sure you have installed the unix calculator, bc. It is an option that you may have to select when installing unix. --Done

Download the script --Done

Change the name to add or remove the .sh as desired when running It does not have the .sh appended to the name. --Is it best to include the .sh and what is the reason/function?

Set the script to executable (chmod u+x) --Not sure how to do this?

Find the full path to where IM (convert) resides by typing in a shell terminal window: type -a convert
If type -a convert returns more than one path, type in a shell terminal window: path2/convert -version, where path2 is each of the paths found. Decide which version of IM you want to use.

Modify your PATH environment variable so that it includes the full path to where IM (convert) resides (often /usr/bin or /usr/local/bin). This can be done by editing your .profile file. --Done

Alternately, edit the script somewhere between the comments and the first use of any IM command, such as just below the defaults section to add the following two lines:
imdir="path2" #(such as imdir="/usr/local/bin" or imdir="/usr/bin")
PATH="${imdir}:${PATH}" Will not do this as I have modified the PATH

Open a shell terminal window
bash /fullpathto/scriptname(.sh) arguments /fullpathto/inputimage /fullpathto/outputimage --Am I correct to assume the argument will be "downsize" if that is the task I wish to do? Or, do I skip this per the Pointer following next?

To avoid the bash and just use scriptname(.sh) ... set your PATH to contain the location of the script I will add .sh as directed. --I believe with this extension and understanding how to do Pointers 3 and 4 above, I should be able to run the script.

Optionally edit the script to change the default directory (found after the defaults section) from dir="." to dir="/tmp" May consider this after I accomplish getting the downsize script to function as outlined above.

If you have trouble with filenames with spaces in them, then you will need to edit the script in several places until I have time to review and fix all my scripts. Find where infile and outfile (maskfile or any others) are defined at the end of the argument trapping section. Make sure to enclose in double quotes any declarations of those files that include $1, $2, etc, such as infile="$1", etc. Find any occurrences of $infile or $outfile and enclose them in double quotes as "$infile", etc.

These are general unix instructions. Modify the paths as appropriate for Cygwin on Windows.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Change the name to add or remove the .sh as desired when running It does not have the .sh appended to the name. --Is it best to include the .sh and what is the reason/function?
It really does not matter as long as you are consistent. Some systems prefer to have a suffix. So if any doubt add .sh for example downsize.sh. But then when using it, use downsize.sh.
Set the script to executable (chmod u+x) --Not sure how to do this?
Open a terminal window in Cygwin. Cd to the directory where you have downsize. Then just type

chmod u+x downsize.sh

open a shell terminal window
bash /fullpathto/scriptname(.sh) arguments /fullpathto/inputimage /fullpathto/outputimage --Am I correct to assume the argument will be "downsize" if that is the task I wish to do? Or, do I skip this per the Pointer following next?
You can do it either way. Probably does not matter if you set your PATH and then even use "bash". But if you set the PATH to contain your path to the script, then you don't need to use "bash" before the scriptname.

If you have named the script downsize, then use that. If you have named the script downsize.sh, then you have to use that.

This would be the same for any of my scripts. Follow the Pointers to make the changes to edit the file. If you keep all the scripts in the same directory, then once you edit the PATH to contain the location of IM and location of the scripts (folder not file), then you don't need to do those steps any more. Once is sufficient. The main thing will be to edit dir="/tmp" rather than dir="./" and to make the script executable with chmod.

P.S. When editing and saving the file, it might be necessary to make sure your editor uses Unix line endings (new lines) rather than Windows line endings. I don't know how sensitive it would be to have mixed line endings.
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by jwtrexler »

Close, very close. When I follow your Pointers and enter the command line as you specified earlier in this thread, the script is attempting to do what the algorithm is directing it to do, however a window opens indicating "Windows can't open this file: downsize.sh". Additional info in the window says "To open this file, Windows needs to know what program you want to use to open it. Windows can go online to look it up automatically, or you can manually select from a list of programs that are installed on your computer."

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

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

It seems to see the downsize.sh as a binary file and thinks it is an image. Try downloading it again and do not change it to .exe. Perhaps leave the .sh off or make it .txt. But do make the file executable with chmod.

If you still have trouble, look to see if there is an error log console and let me know what it says. I am not familiar enough with Windows to tell you where.

Also send me a listing of what you have in your terminal window, with the exact command you used and all the subsequent messages.

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

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

I hope you were not trying to run the script in Windows terminal. It needs to be done in a Cygwin terminal.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

"Windows can't open this file: downsize.sh". Additional info in the window says "To open this file, Windows needs to know what program you want to use to open it. Windows can go online to look it up automatically, or you can manually select from a list of programs that are installed on your computer."
This message is typical of Windows needing a file extension. It does not recognize .sh or probably if there is no extension unless you tell it what program to use to open it. This sounds like you might be using a Windows terminal and not a Cygwin terminal. Cygwin should recognize the .sh.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

I suspect Fred is correct, that you are using a Windows command console, not the Cygwin command console.

I have just installed Cygwin on my computer, under Windows 8.1. I included a small section of extra tools (such as graphics, and bc), and it took 4 hours. It created a "Cygwin64 terminal" on my desktop. Running that gives me a console that looks similar to a Windows console (but annoyingly doesn't seem to have copy-and-paste. EDIT: Yes it does: right-click, or ctrl-ins and shift-ins).

The directory structure under Windows and Cygwin works like this:

Code: Select all

Windows                        Cygwin
-------                        ------
C:\                            /cygdrive/c
F:\                            /cygdrive/f
C:\cygwin64\home\Alan          /home/Alan (aka "~")
c:\im\ImageMagick-6.8.9-Q16\   /cygdrive/c/im/ImageMagick-6.8.9-Q16
The last of these is where I keep my current IM installation. If it was in my standard Windows path, Cygwin would (apparently) automatically know this. But it isn't, so I have to add it to the Cygwin path by typing in the Cygwin terminal:

Code: Select all

PATH=/cygdrive/c/im/ImageMagick-6.8.9-Q16:$PATH
Using Windows notepad, I create a file in my Cygwin home directory. To Windows, this directory is "C:\cygwin64\home\Alan". I name the file "hello" (with no extension). The file contains two lines:

Code: Select all

#!/bin/bash
echo Hello world
In the cygwin terminal, I type two commands:

Code: Select all

ls hello
cat hello
The first lists the files that match "hello". There is only one file. The second writes the file contents to the screen, like Windows "type". Then I execute the script named "hello" by typing the command:

Code: Select all

bash hello
The script, as expected, writes "Hello world" to the console.
snibgo's IM pages: im.snibgo.com
jwtrexler
Posts: 31
Joined: 2014-07-03T22:03:37-07:00
Authentication code: 6789

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by jwtrexler »

I wanted to say thank you for all the help you provided guiding me through the enabling and running of external scripts to IM using Cygwin on a Windows 7 machine. As outlined throughout this thread, I was able to get the downsize script to work, however I did need to reinstall the script, append the .sh extension, and use the full path names including a colon after the C and prior to the right slash symbol. Hopefully this thread will help others not intimately familiar with IM and Cywin use the powerful tools you have developed.

I do have one last question, would you explain how the downsize script works? In other words, I want to make sure the size reduction does not cause a loss of the pixel color information resulting in a deterioration of correlation between the original and resize pixel color space values (i,e., the mean and standard deviations of the distributions for the three color coordinates).

Again, thank you for your expert guidance and valuable time.

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

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Explanation of how downsize works is at the bottom of my example page at http://www.fmwconcepts.com/imagemagick/ ... /index.php. Note it always starts at full resolution for each iteration. If you are using JPG images, it would be better to do it directly with IM -define jpeg:extent={size}. See http://www.imagemagick.org/Usage/formats/#jpg_write
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

Thanks snibgo for going through the motions and providing an explanation of how to install Cygwin and to explain the file naming syntax differences.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

Since my earlier post, I've discovered that bash scripts can easily be run from ordinary Windows consoles. Just give the script an extension of .sh (or anything else) and associate that extension with bash.exe, which in my case is C:\cygwin64\bin\bash.exe.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

snibgo wrote:Since my earlier post, I've discovered that bash scripts can easily be run from ordinary Windows consoles. Just give the script an extension of .sh (or anything else) and associate that extension with bash.exe, which in my case is C:\cygwin64\bin\bash.exe.
That is very interesting. Does that mean it does not use Cygwin? I think you still have to have Cygwin installed for the Windows terminal to get to the bash terminal, since you show its location at C:\cygwin64\bin\bash.exe. Neverthless, it makes it easier to avoid using the Cygwin terminal. Though one still has to go through all the installations and cofigurations of Cygwin.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

I have configured previous computers to dual-boot Windows or Linux. I thought Cygwin was somewhat similar but without the need to reboot.

But it seems to be a load of standard *nix programs compiled for Windows. sed, awk, bc, and loads of other console programs. Plus "mintty.exe" which is the Cygwin console. And bash.exe, of course. It seems to have Gnome if I want a different GUI (no thanks).

I can run Windows console programs from the Cygwin console, and Cygwin programs from the Windows console. The main complication is that Cygwin's tools expect a different directory structure that doesn't have drive-letter-colon devices, but they provide a utility to translate directory names.

I couldn't get the hang of Cygwin when I tried it a year or two ago, possibly because I hadn't used Unix seriously for 20 years (and I didn't like it then). But now I've discovered I can pick and choose what I want from it, and use all my old software without needing to recompile everything, I'm much happier.

So, rather than the sweeping statement that your scripts can run under Cygwin on Windows, it would be more precise to say they run under bash, which is available as part of Cygwin, but the scripts also need other tools that are available in Cygwin.

Your histmatch script runs fine this way, in a Windows console, with Cygwin's bin directory on my path.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by fmw42 »

What kind of paths are you using when running in the Windows console, if the images are not in the same directory. Also if you have to provide the path to the script.

If you put the path to where you keep all my scripts (assuming in the same directory) in the PATH variable, then bash may not be needed or at least not when using the Cygwin terminal rather than the Windows terminal.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How To: Using Cygwin and Scripts (e.g., Fred's) with IM

Post by snibgo »

I renamed your script "histmatch.sh" to associate it with bash from the Windows console. But that doesn't work:

Code: Select all

histmatch.sh f:\web\im\img_Left.jpg f:\web\im\img_Right.jpg f:\web\im\x.jpg

F:\web\im\histmatch.sh: line 100: type: F:\web\im\histmatch.sh: not found
I guess Windows expands %0 to a Windows-style path, but "type" expects a Cygwin-style path.

When I use "./histmatch.sh", Windows complains (probably at the "/").

When I use ".\histmatch.sh", I again get:

Code: Select all

F:\web\im\histmatch.sh: line 100: type: F:\web\im\histmatch.sh: not found

These work:

From Windows console:

Code: Select all

c:\cygwin64\bin\bash /cygdrive/f/web/im/histmatch.sh f:\web\im\img_Left.jpg f:\web\im\img_Right.jpg f:\web\im\x.jpg
or

Code: Select all

bash /cygdrive/f/web/im/histmatch.sh f:\web\im\img_Left.jpg f:\web\im\img_Right.jpg f:\web\im\x.jpg
(as c:\cygwin64\bin\ is in my path).

From Cygwin console (which uses bash for its shell):

Code: Select all

/cygdrive/f/web/im/histmatch.sh f:/web/im/img_Left.jpg f:/web/im/img_Right.jpg x.jpg
From Cygwin, all the slashes need to be forwards.

It's a bit weird. I think bash is a Cygwin tool so expects its argument (the script name) to be in a Cygwin-style directory. The other arguments are simply passed to your script, which passes them (via infile1, infile2 and outfile) to IM's convert, which isn't a Cygwin tool but an ordinary Windows tool, so it expects Windows-style directories.
snibgo's IM pages: im.snibgo.com
Post Reply