1.1Introduction
Before we begin discussion on image acquisition and processing using Python, we will provide an overview of the various aspects of Python. This chapter focuses on some of the basic materials covered by many other books [Bea09], [Het10], [Lut06], [Vai09] and also from the book, “Essential Python” ([PC18]), a book from the authors of this book. If you are already familiar with Python and are currently using it, then you can skip this chapter.
We begin with an introduction to Python. We will then discuss the installation of Python with all the modules using the Anaconda distribution. Once the installation has been completed, we can begin exploring the various features of Python. We will quickly review the various data structures such as list, dictionary, and tuples and statements such as for-loop, if-else, iterators and list comprehension.
1.2What Is Python?
Python is a popular high-level programming language. It can handle various programming tasks such as numerical computation, web development, database programming, network programming, parallel processing, etc.
Python is popular for various reasons including:
1.It is free.
2.It is available on all the popular operating systems such as Windows, Mac or Linux.
3.It is an interpreted language. Hence, programmers can test portions of code on the command line before incorporating it into their program. There is no need for compiling or linking.
4.It gives the ability to program faster.
5.It is syntactically simpler than C/C++/Fortran. Hence it is highly readable and easier to debug.
6.It comes with various modules that are standard or can be installed in an existing Python installation. These modules can perform various tasks like reading and writing various files, scientific computation, visualization of data, etc.
7.Programs written in Python can be run on various OS or platforms with little or no change.
8.It is a dynamically typed language. Hence the data type of variables does not have to be declared prior to their use, making it easier for people with less coding experience.
9.It has a dedicated developer and user community and is kept up to date.
Although Python has many advantages that have made it one of the most popular interpreted languages, it has a couple of drawbacks that are discussed below:
1.Since its focus is on the ability to program faster, the speed of execution suffers. A Python program might be 10 times or more slower (say) than an equivalent C program, but it will contain fewer lines of code and can be programmed to handle multiple data types easily. This drawback in the Python code can be overcome by converting the computationally intensive portions of the code to C/C++ or by the appropriate use of data structure and modules.
2.Indentation of the code is not optional. This makes the code readable. However, a code with multiple loops and other constructs will be indented to the right, making it difficult to read the code.
1.3Python Environments
There are several Python environments from which to choose. Some operating systems like Mac, Linux, Unix, etc. have a built-in interpreter. The interpreter may contain all modules but is not turn-key ready for scientific computing. Specialized distributions have been created and sold to the scientific community, pre-built with various Python scientific modules. When using these distributions, the users do not have to individually install scientific modules. If a particular module that is of interest is not available in the distribution, it can be installed. One of the most popular distributions is Anaconda [Ana20b]. The instructions for installing Anaconda distribution can be found at https://www.anaconda.com/distribution/.
1.3.1Python Interpreter
The Python interpreter built into most operating systems can be started by simply typing python in the terminal window. When the interpreter is started, a command prompt ( > > >) appears. Python commands can be entered at the prompt for processing. For example, in Mac, when the built-in Python interpreter is started, an output similar to the one shown below appears:
(base) mac:ipaup ravi$ python Python 3.7.3 | packaged by conda-forge | (default, Dec 6 2019, 08:36:57) [Clang 9.0.0 (tags/RELEASE_900/final)] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Notice that in the example above, the Python interpreter is version 3.7.3. It is possible that you might have a different version.
1.3.2Anaconda Python Distribution
The Anaconda Python Distribution [Ana20a] provides programmers with close to 100 of the most popular scientific Python modules like scientific computation, linear algebra, symbolic computing, image processing, signal processing, visualization, integration of C/C++ programs to Python etc. It is distributed and maintained by Continuum Analytics. It is available for free for academics and is available for a price to all others. In addition to the various modules built into Anaconda, programmers can install other modules using the conda [Ana20b] package manager, without affecting the main distribution.
To access Python from the command line, start the ‘Anaconda Prompt’ executable and then type python.
1.4Running a Python Program
Using any Python interpreter (built-in or from a distribution), you can run your program using the command at the operating system (OS) command prompt. If the file firstprog.py is a Python file that needs to be executed, then type the following command on the OS command prompt.
>> python firstprog.py
The >> is the terminal prompt and >> > represents the Python prompt.
The best approach to running Python programs under any operating systems is to use an Integrated Development Environment like IDLE or Spyder as it provides an ability to edit the file and also run it under the same interface.