CS106L Introduction to Computing and Programming in Python (Lab)
Fall 2008

Lab 4: Modifying Pixels in a Range

Using range to modify pixels in a subsection of an image

In the lab, we created a function that used range and a nested for loop to do pixel operations on a subsection of an image.
def clearGreenInRange(picture):
  for x in range(35,127):
    for y in range(10,201):
      p = getPixel(picture,x,y)
      setGreen(p,0)
      
This function selected a specific region of the image to clear the green component of the region's pixels. We "hard-coded" the range into the function. However, it would be far more useful to design the function to take any range of x and y coordinates.
def clearGreenInRange(picture,x0,y0,x1,y1):
  for x in range(x0,x1+1):
    for y in range(y0,y1+1):
      p = getPixel(picture,x,y)
      setGreen(p,0)
      
Remember:

if statements

From the Python Tutorial:
>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
...      x = 0
...      print 'Negative changed to zero'
... elif x == 0:
...      print 'Zero'
... elif x == 1:
...      print 'Single'
... else:
...      print 'More'
...
    
So, in the case of problem 3.9 from last lab's assignment:
def changeColor(picture,amount,rgb):
  for p in getPixels(picture):
    if (rgb == 1):
      redValue = getRed(p)
      setRed(p,(1+amount)*redValue)
    elif (rgb == 2):
      greenValue = getGreen(p)
      setGreen(p,(1+amount)*greenValue)
    elif (rgb == 3):
      blueValue = getBlue(p)
      setBlue(p,(1+amount)*blueValue)
    else:
      print 'Please enter a 1 (red), 2 (green), or 3 (blue) as the third argument'
      return
       
Here we used an "if/elif/else" structure to chose the correct color to do our pixel operation. A few things to notice:

MediaTools

setMediaPath and getMediaPath

>>> setMediaPath()                      # this will ask to set a directory where media is located
>>> filePath = getMediaPath('arch.jpg') # this will return path to arch.jpg in media directory
>>> picture = makePicture(filePath)     # this loads the arch image into memory
   

Picture Tool...

In the MediaTools menu item, there is a "Picture Tool..." option. This is used to garner information about individual pixels for any picture object we have loaded into memory and is directly referenced by an individual variable in the interpreter's scope. So, during the lab, when I created a list of picture objects, these objects were not available to MediaTools until I directly referenced one of them by an individual variable.

I.e.
>>> filePath = getMediaPath('arch.jpg')
>>> pictureList = []
>>> for i in range(5):
...   pictureObject = makePicture(filePath)
...   pictureList.append(pictureObject)
>>> pictureList[0]
Picture, filename Y:\cs106\MediaSources\arch.jpg height 350 width 263
>>>
>>> # MediaTools cannot see pictureList[0]
>>>
>>> archPicture = pictureList[0]
>>>
>>> # MediaTools *can* see archPicture
     
Code from Lab 4

Here is the code that we worked on during the lab. If you have any questions about it, feel free to email me at cs106ta@cis.uab.edu.

lab4code.py

LAB ASSIGNMENT

Problems 4.1, 4.2, 4.3, 4.4, 4.5 from chapter 4 in the textbook.

These are due by 3:30 next Thursday (9-18-08). After then, they will be considered late, and points will be deducted.




Last modified: 09-12-2008
By: David O'Gwynn