A collection of Python variables, functions and objects, all stored in a file
Modules allow code to be shared across many different programs.
Before we can use a module, we need to import it:
>>> import module_name
Importing makes the functions in the module available for use. We have seen this in homeworks.
You can also write code in a module that is executed only when the module is directly executed, but not when it is exported. Let us write a simple module named test_module.py
def test_square(num):
return num**2
if __name__ == '__main__':
print test_square(4)
We will see the difference between exporting and running this module in class.
When a Python script is executed, its name is __main__.
When a Python script is imported, its name is the name of the file, e.g. test_module. You can also rename it as you wish:
>>> import test_module as t
>>> t.test_square(4)
Type
>>> help(__builtins__)
to see all of the modules Python has built-in. These do not need to be imported.
You will notice several that have two underscores at the start and end, such as
__add__
__contains__
__eq__
__len__
These names have special Python syntax associated with them:
For example,
>>> s0 = "Hello"
>>> s1 = "Good-bye"
>>> s0 + s1
'HelloGood-bye'
>>> s0.__add__(s1)
'HelloGood-bye'
>>> s2 = "Go"
>>> s1.__contains__(s2)
True
>>> s2 in s1
True
>>> s1.__len__()
8
>>> len(s1)
8
We will generally use the “special” syntax, but be aware of the equivalence to the __ syntax when you are learning to use object methods.
PIL is a series of modules built around the Image type, our first object type that is not part of the main Python language
We will use images as a continuing example of what can be done in programming beyond numbers and beyond text.
See
http://effbot.org/imagingbook/pil-index.htm
for more details.
An image is a two-dimensional matrix of pixel values
The origin is in the upper left corner, see below:
Pixel values stored in an image can be:
Some basic colors:
Color (red,green,blue) value Black (0,0,0) Red (255,0,0) Green (0,255,0) Blue (0,0,255) White (255,255,255) Light Gray (122,122,122)
import Image filename = "chipmunk.jpg" im = Image.open(filename) print '\n' '********************' print "Here's the information about", filename print im.format, im.size, im.mode gray_im = im.convert('L') scaled = gray_im.resize( (128,128) ) print "After converting to gray scale and resizing," print "the image information has changed to" print scaled.format, scaled.size, scaled.mode scaled.show() scaled.save(filename + "_scaled.jpg")
This example crops three boxes from an image, creates a new image and pastes the boxes at different locations of this new image.
import Image
im = Image.open("lego_movie.jpg")
w,h = im.size
# Crop out three columns from the image
## Note: the crop function returns a new image
part1 = im.crop((0,0,w/3,h))
part2 = im.crop((w/3,0,2*w/3,h))
part3 = im.crop((2*w/3,0,w,h))
## Create a new image
newim = Image.new("RGB",(w,h))
## Paste the image in different order
## Note: the paste function changes the image it is applied to
newim.paste(part3, (0,0))
newim.paste(part1, (w/3,0))
newim.paste(part2, (2*w/3,0))
newim.show()
Important image methods used:
Note that many of the PIL functions actually change the image object we are working with
Modules contain a combination of functions, variables, object definitions, and other code, all designed for use in other Python programs and modules
After they are imported, the functions in a module can be executed by a call of the form:
module_name.function_name(arguments)
PIL provides a set of modules that define the Image object type and associated methods.