Appendix B
Introduction to Python
Python is a programming language that was initially released in 1991 by Guido Van Rossum,
and which has since become deservedly popular for its elegance, ease of use, readability,
portability, and extensibility. It marks code blocks using indentation, instead of parentheses
(such as C), or “begin,” “end” words (such as MATLAB). Since its inception Python has
matured, is used worldwide for enterprise systems as much as for elementary teaching, and
there are a vast number of specialized libraries—over 54,000 at the time of writing—which
support particular programming tasks. Some of the most richly developed and most used
libraries include:
numpy “Numerical Python,” routines for array manipulation and high-
level functions for operating on arrays.
scipy “Scientific Python,” which includes modules for optimization, nu-
merical analysis, signal and image processing, and differential
equation solvers.
matplotlib
A plotting library, which includes an interface
pyplot
designed
to emulate the plotting tools of MATLAB.
Python’s standard library is rich and full-featured, and allows the handling of many differ-
ent data types, mathematics, Internet protocols, file and data handling, system interfaces,
multimedia, and graphics interface design.
Unlike MATLAB or Octave, not all of Python’s f unctions are available to the user at
the start; many functions have to be “imported”, either by importing their library or by
importing just the f unctions needed. This makes the namespace very manageable, and can
allow for different functions sharing the same name.
Python has many interfaces. IDLE (“Integrated Development Environment”) is bundled
with the language, and may be considered a simple and uncluttered Python shell. IPython
is an enhanced shell that includes syntax highlighting, automatic indentation, inline graph-
ics, and many other enhancements. Spyder provides a graphics development environment,
similar in some ways to that of MATLAB or Octave, and with integration of numpy, scipy,
matplotlib, and iPython. There are numerous other interfaces, some open source, others
proprietary. Unlike most programming systems, where the choice is betwee n a graphics
interface or a command line interface, the Python user is sp oiled for choice.
Figure B.1 shows two standard development environments: iPython and Spyder. IPython
has been configured to produce graphics “inline”; this is just one of many configuration op-
tions. Note that Spyder includes the iPython enhanced shell within it (and support for
other consoles), as well as a variable and file browser, integrated editor, and interface to the
Python debugger.
505
506 A Computational Introduction to Digital Image Processing, Second Edition
iPython Spyder
FIGURE B.1: Python development environments
B.1 Basic Use
Suppose Python is started, and you have your prompt. Depending on the interface, it
might be three “greater than” signs (IDLE):
Python
>>>
or a prompt that includes the command number (iPython):
Python
In [1]:
For ease of exposition, we shall use an unnumbered prompt
Python
In :
Python can perform standard arithmetic, with a couple of caveats: the “double division”
sign:
// is used f or integer division, and exponentiation is implemented with two asterisks:
**
; the caret ^ is used for bitwise conjunction. Here are a few examples:
Introduction to Python 507
Python
In : 37+42
Out: 79
In : 2
**
67-1
Out: 147573952589676412927L
In : 1.0/7
Out: 0.14285714285714285
In : 11/7
Out: 1
In : 11.0/7
Out: 1.5714285714285714
In : 11.0//7
Out: 1.0
Python will return a value of the same type as the input: thus 11/7,” having integer
inputs, will return an integer result. Python can also perform integer arithmetic to arbitrary
precision:
Python
In : 37
**
100
Out:
6609557828843866774348296857793615320986068325257944996730965130260195627
4934906370480041052565637429940700377695998823990123971705692002794664127
58131334001L
Arbitrary precision real arithmetic can be obtained through specialized libraries.
Standard mathematical functions are available in the
math standard library, which must
be loaded before its functions are available. This can be done in several ways:
Python
In : from math import
*
This makes all functions from the math library available.
Python
In : import math
This imports the library; individual functions (such as sin) must be called as math.sin:
Python
In : sqrt(3.0)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-d4d1770368ad> in <module>()
----> 1 sqrt(3.0)
NameError: name ’sqrt’ is not defined
In : import math
508 A Computational Introduction to Digital Image Processing, Second Edition
Python
In : math.sqrt(3.0)
Out: 1.7320508075688772
In : from math import exp
In : exp(1.5)
Out:
4.4816890703380645
In : sin(pi/6)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-dbaab9b2f48b> in <module>()
----> 1 sin(pi/6)
NameError: name ’sin’ is not defined
In : from math import
*
In : sin(pi/6)
Out: 0.49999999999999994
In : cos(pi/12)
Out: 0.9659258262890683
If you want to keep the namespace functions different, you can import a library with an
alias:
Python
In : import math as m
In : m.tan(pi/8)
Out: 0.41421356237309503
To see all math functions, enter math. (including the period) and press the tab key:
Python
In : math. Tab
math.acos math.cos math.factorial math.ldexp math.sin
math.acosh math.cosh math.floor math.lgamma math.sinh
math.asin math.degrees math.fmod math.log math.sqrt
math.asinh math.e math.frexp math.log10 math.tan
math.atan math.erf math.fsum math.log1p math.tanh
math.atan2 math.erfc math.gamma math.modf math.trunc
math.atanh math.exp math.hypot math.pi
math.ceil math.expm1 math.isinf math.pow
math.copysign math.fabs math.isnan math.radians
Introduction to Python 509
B.2 Arrays
Arrays are the basic data type for image processing; in Python, an array can be entered
as a list of lists, using the
array f unction from numpy
Python
In :
import
numpy as np
In : A = np.array([[1,2,3],[4,5,6],[7,8,9]])
Python does not return the result of a variable assignment, so we can see this new array by
calling it:
Python
In : A
Out:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In : print A
[[1 2 3]
[4 5 6]
[7 8 9]]
Python also has a matrix class, in which operations are slightly different, but there will be
no need of it in this text; most image processing functions return arrays. Python arrays do
not use the “dot” syntax of MATLAB for operation on every element:
Python
In : A
**
3
Out:
array([[ 1, 8, 27],
[ 64, 125, 216],
[343, 512, 729]])
In : A
**
(0.5)
Out:
array([[ 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974],
[ 2.64575131, 2.82842712, 3. ]])
In : np.sqrt(A)
Out:
array([[ 1. , 1.41421356, 1.73205081],
[ 2. , 2.23606798, 2.44948974],
[ 2.64575131, 2.82842712, 3. ]])
To multiply two matrices use the dot method:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.6