I have created a simple Python module called "Image" that we'll use to begin our work with digital images. After a description of the Image module there are a set of example scripts you can download. Depending upon whether you learn from example or by description, you should take a look at the scripts first to see how much of their structure makes sense.
To load the Image module for use in the script, you use the import command.
import Image
This makes the components of the Image module available in your script
by using the module name as a prefix, separated from the component
with a period character (.). The Image class in the Image module is
therefore called Image.Image — the symbol "Image" is used for both
the name of the module and the name of the class.
You can also explicitly specify the components of a module that you want to load. To load the Image class and the function fit from the Image module, you can write:
from Image import Image, fit
By loading the specific symbols (and not the entire module) you can
refer to them without the module name as prefix. The circle.py
script is the first script in the
examples below to use this.
When you create an instance of a class, you use the class name as if it were a function. You make an instance of the Image class in two different ways by using two different types of arguments. In the first way, you create an image from scratch by specifying the image size when you create it. The arguments are the image's width, height, and the number of channels. For example, to create an image that is 400 pixels wide, 300 pixels high, with 3 channels (for red, green and blue), you would write:
p = Image(400, 300, 3)
The variable p is now an instance of the Image class. For example, black.py
creates an Image instance that
is 200 pixels square.
In the second way of creating an Image instance, you can acquire the pixel data from an image by using its filename as the argument when you create the instance:
p = Image('mantis.tif')
The size of the image instance is determined by the pixel size of the
image in the file that you specified. For example, the script negate.py
creates a negative image from the
input image file.
To save an image to a file, you use the save method. For example, to save an image to a file called "black.tif", you would use the filename as the argument to save.
p.save('black.tif')
The script black.py
is the simplest image script
we can write — it just defines a new image and writes it out. By
default, the pixel values are all 0, that is, black.
We can also create a new image instance with an initial color by specifiying the channel colors when we create it:
p = Image.Image(200, 200, 3, .4, .5, .6)
We create an image in this way in the blue.py
script.
To set a pixel value, you use the put function that is part of the Image class. Functions that are part of a class are called methods and are separated from the instance variable with a period (.) character. For example, for the instance p the put method is called:
p.put
You specify the x and y positions in the image as integers
beginning with 0 in the lower left corner. The red, green and blue
color primary values, called the image's channels, are all
specified as a fraction from 0.0 to 1.0.
For example,
p.put(100, 50, .2, .3, .7)
puts a red value of .2, a green value of .3 and a blue value of .7 at
the pixel that is 101 from the left and 51 from the bottom. (Remember
that the first pixel in the lower left corner is 0,0.)
If you want to assign alpha values, you need to create a four-channel image:
p = Image(200, 200, 4)
You can also put the channel values into a pixel separately. For example, the following is the same as the previous p.put command:
p.put_r(100, 50, .2)
p.put_g(100, 50, .3)
p.put_b(100, 50, .7)
The half.py
script uses put_b to
change the blue value by comparing the x value with the half the
width of the image.
red, green, blue = p.get(100, 50)
Just like putting the channel values individually, you can get the
channel values separately, too:
red = p.get_r(100, 50)
green = p.get_g(100, 50)
blue = p.get_b(100, 50)
If the image has an alpha channel, you can use the geta method to
get four channels:
red, green, blue, alpha = p.geta(100, 50)
The get_a method will get just the alpha value like get_r, get_g, and get_b.
We can use the range Python function to create a list of integers. With a nested loop for all the combinations of x values and y values we can specify all the pixel coordinates for the image.
In the example scripts below, you will always see this structure which loops through all the x,y positions in the image.
# The variable "p" is an Image instance
for x in range(p.width):
for y in range(p.height):
# Use x and y here.
Files to download to your image directory:
For all of the Python scripts below, you can click on the script name or the image to download it. Depending upon your browser, you may need to move it from the directory to which it was downloaded or redefine the download destination. For example, Safari defines the directory to which files are downloaded in its "Preferences" menu. For this tutorial, you could change your download directory to ~/pdm/image.
For the scripts that modify existing images, you will also need to download those image files from these links:
mantis.tif
frog.tif
frogmatte.tif
Some of the scripts that modify images use images that are produced by previous scripts, so you'll need to run those first to make the images.
Once you've downloaded the two Python module files and the scripts, you can run a script by using its filename as an argument to Python:
python color.py
Try changing the various numeric values in the scripts to see the
result. Change the name of the filename in the save method.
For the scripts that modify existing images, use these new
images by changing the name of the input image.
![]() |
import Image
p = Image.Image(200, 200, 3)
p.save('black.tif')
|
![]() |
import Image
p = Image.Image(200, 200, 3, .4, .5, .6)
p.save('blue.tif')
|