Lab 4
This assignment will have you use JES - Jython Environment for
Students.
In the ACCC Labs that have JES, it can be found by:
- Clicking on Start
- Then click on All Programs
- Then click on Class Applications
- Then click on Engineering
- Finally click on Jython Environment for Students
Images Using a GrayScale
A black and white picture uses various shades of gray.
These shades of grey all have the same amount of red,
green and blue color. The higher the color value, the
lighter the shade of gray is. The lower the color
value, the darker the shade of gray is. For example,
look at the following squares for various shades of gray.
The amount of red, green and blue for the color is displayed
as text in each square. Note that the amount of red,
green and blue is the same in each square. Also note that
the larger the amount of color, the lighter the color of gray.
r = 0 g = 0 b = 0
| r = 63 g = 63 b = 63
| r = 127 g = 127 b = 127
| r = 191 g = 191 b = 191
| r = 255 g = 255 b = 255
|
Changing the color in a pixel
The color in a pixel can be changed by using the setRed(),
setGreen() and setBlue() functions in JES. Each of these
functions take two input values. The first input value is
the pixel to change, while the second input value is the
new amount of the color the pixel should have.
Another way to change the color in a pixel is to use makeColor() to create a color
value by speciying the red, green and
blue values in a single call and then to use setColor() set the three
color values of a pixel.
The JES Help pages gives the following information.
setRed |
setRed(pixel, redValue):
pixel: the pixel you want to set the red value in.
redValue: a number (0 - 254) for the new red value of the pixel
Takes in a Pixel object and a value (between 0 and 254) and sets the redness of that pixel to the given value.
|
setGreen |
setGreen(pixel, greenValue):
pixel: the pixel you want to set the green value in.
greenValue: a number (0 - 254) for the new green value of the pixel
Takes in a Pixel object and a value (between 0 and 254) and sets the greenness of that pixel to the given value.
|
setBlue |
setBlue(pixel, blueValue):
pixel: the pixel you want to set the blue value in.
blueValue: a number (0 - 254) for the new blue value of the pixel
Takes in a Pixel object and a value (between 0 and 254) and sets the blueness of that pixel to the given value.
|
makeColor |
makeColor(red, green, blue):
red: the amount of red you want in the color
green: the amount of green you want in the color
blue: the amount of blue you want in the picture
returns: the color made from the inputs
Takes three inputs: For the red, green, and blue components (in order), then returns a color object.
|
setColor |
setColor(pixel, color):
pixel: the pixel you want to set the color of
color: the color you want to set the pixel to
Takes in a pixel and a color, and sets the pixel to the provided color.
|
Determining the amount of gray
One method to determine which grayscale color to use
for a pixel when creating a black and white picture
from a color picture is to average the amount of
red, green and blue color the corresponding pixel
in the colored picture. This method works fairly
well and is fairly easy to understand and use.
This way using the intensity of each color to determine
the grayscale value.
Another method is to take a weighted average of the
amount of red, green and blue color instead of an
evenly-weighted average. A weighted average would
give more weight to certain values and less weight to
other values. For example, a weighted average will be used when
determining the grade for this course. Exams have a higher
weight (i.e. more impact on the final grade), while
lab assignments have a lower weight (i.e. less impact
on the final grade). For determining the amount of
color for the graycolor color, the original amount of
green is often given a higher weight while the original
amount of blue is given a lesser weight. The percent
of each color that should be used according to some
research done on luminance is:
Lab Assignment 4
Due: Tuesday 2/6/2007 by 11:59 pm
In a single file, write three python functions in JES that will do the following.
Function 1: makeBlackAndWhite ()
- The function is to be named: makeBlackAndWhite
- Print out your name and your net-id
- Allow the user to select a picture from a file stored on
the local machine
- Change the image to black and white (i.e. grayscale) using either the
average brightness method or the weighted average for luminance method discussed
above. You may use which ever method you wish. Note, the code for both is
given in the book and in the lecture slides. This modification will need to be
done for every pixel in the selected image. Refer to the
python examples
page for the examples from Lecture
on 2/6/2007, you will see code that almost does what you need to do.
Your loop should be patterned after the loop in the function
decreaseRed2 ().
- Display the modified image
- Save the modfied image to a file on the local machine
Function 2: makeBlackAndRed ()
- The function is to be named: makeBlackAndRed
- Print out your name and your net-id
- Allow the user to select a picture from a file stored on
the local machine
- Change the image to a "red scale" image. This is done in a similar manner
as the creation of a grayscale image, except the values for green and blue are
kept at zero. Only the red value for each pixel will range from 0 to 255.
Where the grayscale image is black, the red scale image will also be black.
Where the grayscale image is white, the red scale image will be red.
Where the grayscale image is gray, this image will range from black to red
(the middle values will be a shade of maroon).
- Display the modified image
- Save the modfied image to a file on the local machine
Function 1: makeRedAndWhite ()
- The function is to be named: makeRedAndWhite
- Print out your name and your net-id
- Allow the user to select a picture from a file stored on
the local machine
- Change the image to a version of a red scale image. This is done in a
similar manner as the creation of a grayscale image, but with a twist.
Where the grayscale image is black, this image will be red.
Where the grayscale image is white, this image will also be white.
Where the grayscale image is gray, this image will range from red to white
(the middle values will be rather "pinky").
- Display the modified image
- Save the modfied image to a file on the local machine
Some of the resulting colors will appear as:
makeBlackAndWhite()
|
|
|
|
|
|
makeBlackAndRed()
|
|
|
|
|
|
makeRedAndWhite()
|
|
|
|
|
|
When saving your image, use writePictureTo(). If you hardcode the
filename in your program, remember two points.
- If working in windows, proceed the name with an r.
- The directory/folder used must already exist. This is what
caused Prof. Troy's problem during class on Tuesday 1/30/2007.
Submittal of the Lab Assignment
You are submit your program electronically by placing it in the
digital drop box in blackboard. Programs that are correct and
submitted by the end of the student's lab time on the Wednesday
the lab assignment is discussed will receive an extra 10% on the
score of the lab.
When saving your program, use a meaningful name like lab4.py.
When submitting your lab assignment, place all of the needed files it in a
zippped folder (even if you are only submitting one file) with the name
of yournetid_lab# and submit that zipped folder to the digital
drop box. For instance, if your net id is
abc32 then the folder name should be abc32_lab#4.
Comments on the ACCC Labs
On the computers in the ACCC Labs there should an H: drive.
This drive is actually a networked connection to your own file space
maintained by the ACCC. No matter what machine you use or what lab
you are in, the H: drive will access the same file space. This means
that you can save a file on the H: drive on a computer in one lab and
access that same file on a computer in another lab. This can be very
helpful. It is suggested that you store your python program files
on your H: drive.