
In Python JES I need someone to write a program: simple washing color in and out
5093
$$
- Posted:
- Proposals: 0
- Remote
- #155482
- Archived
Description
Experience Level: Intermediate
Goal
In this first programming assignment, you will be writing a few functions to wash out color from an an image and wash color into an image.
Learning Objectives
Familiarity with JES projects
Familiarity with creating a simple functions
Familiarity with saving, loading & executing Jython functions
Exposure to Jython instructions
Exposure to image manipulations
Programming Help
Finally, for this program (and all others) if you get stuck on a step post a description of your problem in the Python Programming forum. The instructor (and your fellow classmates) will offer suggestions. You may also wish to use the Search Forums feature on the course web page to ascertain if someone has already posted the same or similar question and solution suggestions have already been offered.
Coding
In assignment, you will create very simple functions to allow a user to select an image file for display and processing. The trivial washing color out & in manipulations will be performed on the following image:
This image is a reduced version of an image found on Wikipedia.
Refer the examples in chapters two and three if you are unsure how to perform any of the following steps.
Procedure:
Create a Jython driver function for the image
Start JES on your computer.
Create a folder for this program. For example name it Program-0. Right-click on the frog image above and save it in your Program-0 folder. You might wish to make a copy of the file as you will be changing it. Of course you can always re-download it.
Create a new program. A new JES project is initially empty, containing no functions. Select File menu, New Program
Almost all program code files begin with comment statements. These statements give identifying information about the program and programmer. At the top of your program copy and paste the following comment lines. Modify them to include a description of your program, your name and the current date. (You may need to edit the lines in JES after you paste them. Copying and pasting code between programs using the clipboard can often introduce unwanted side effects.)
1
2
3
4
5
6
#MaSc 1044 Program 0: enter a brief description
#Author: enter your name & email
#Version: yyyy.mm.dd
#
#------------------------------------------------------------------------------
#Driver function
Define a new function following the last comment line. In programming, a function is simply a way of referring to a collection of related statements by name. Give your function the name "program0". (In Python, unlike some other programming languages, it is not required that the/a function name in the file be the same as the name of the file.) This will be the driver function for the program. A driver function typically includes instructions to explain the program, to allow a user to perform input and to invoke the main processing functions.
To define a function use the Python keywordd def followed by the name of the function you decide upon. The name of the function must be immediately followed by parentheses () which in turn are immediately followed by a colon. The statements on the following lines will comprise the function. Each of the lines of the function must be consistently indented. It is recommended to use two spaces for the indentation. The indentation will allow you and others to quickly determine which statements belong to which function. In Python, statements at the same indentation level are considered a block of code. The block of code following a function is also termed the body of the function.
On the first indented line following the name of the function write an invocation to the JES showInformation() function to briefly explain the program to the user. The statment will look similar to:
showInformation( "This
program allows a user to ... \n ..." )
This will display the instructions in a pop-up window.
On the following line write a JES instruction to allow a user to select a file. Be sure to assign the selected file to a variable for later referencing.
On the next line, also indented, write a JES instruction to make a picture from the file the user selected. You will also need to assign the created picture to a variable name, (e.g., frog0). Repeat this instruction on the next two lines, changing the variable name in order to create three images from the same file, (e.g.,
frog1 & frog2). This will allow us to view the original file and to view each of the transformations separately.
On the next line of the function include a statement to display the original picture.
At this point you should test to make sure that the statements you have written are operating correctly.
Save your program, (File-Save Program As ...).
Save it in your Program-0 folder.
Name it program0.py. (Be sure to add the extension.)
Load your program, (Depress the "Load Program" button.). Fix any syntax errors that JES complains about.
Once your program loads without errors, execute it by typing the name of your function
>>> program0() in the command area.
If your program is operating correctly you should see a file selection dialog box appear allowing you to select the image of the frog you downloaded. Then the image of the frog should appear in a pop-up window. If your program is behaving improperly you must determine which statements to fix. This is termed debugging your program. When your program is operating correctly proceed to the next step.
If you used print to display your program explanation then it is likely that the print statements' output will appear in the command area after the image is displayed. You may wonder why the print statement output does not appear before the file selection dialog, especially since the print statements are the first in the function.the complete explanation for this phenomenon is fairly technical. It is a result of a programming technique termed buffering. The buffering technique is used to mask the speed differences of the various devices connected to the computer.
Create a function to darken the image (i.e., wash out some of the color)
Now you are ready to create your first image transformation function. The purpose of this function will be to darken the image. In order to achieve this effect each RGB value of each pixel will need to be reduced. The reduction you will be implementing will be based upon the average of the RGB values of each pixel. Each RGB pixel value will be reduced by two thirds of the difference of its value and the average of the pixel RGB values.
For example, consider a pixel with the following RGB values: 93, 144, 237. The average of these three values is 474/3 = 158. The difference between 93 & 158 is 65. The difference between144 & 158 is 14 and the difference between 237 & 158 is 69. Two thirds of each these differences yields 43.333, 9.333, 46 respectively. Thus the new pixel values would be 93-43.333, 144-9.333, 237-46. Which is 49.666, 134.666, 191. Since each of these new values (49.666, 134.666, 191) are less than the original values (93, 144, 237) the pixel will be darker.
All functions should be preceded with comment statements. These comment lines will simply explain what the function accomplishes. Following the last line of your driver function add a couple of blank lines for spacing purposes. Then copy and paste the following comment lines. Modify them to explain this function.
1
2
###############################################################################
#explain what the function does
Following the function explanation lines, define your function to gray out an image. This function will take one parameter which will be the image to be darkened. Don't forget to surround the parameter variable with parentheses and follow it with a colon.
Be sure to choose good meaningful variable names for the function and the parameter. Poor variable names makes a program very hard to understand. It increases the number of things one must remember while programming. Psychologists term this cognitive overload. It is one of the reasons why some people find math problems difficult. They must constantly try to remember what each of the variable names such as X, Y or Z represents in the problem. Large programs may contain literally thousands of variables. Thus good variable names that explain exactly what the variable represents is an absolute must in programming. You may lose points on your programming assignments in this course for choosing very poor variable names.
On the next indented line, following the name of your darken function, you will need to write a for in loop in order to traverse all of the pixels of the image. The syntactical format of the Python for in loop statement is:
for
loopVariable in
list:
You will need to choose an appropriate name for your loop variable. For the loop list you will substitute an array holding all of the pixels of the image.JES provides the
getPixels() function to generate this array. There are several examples of this type of loop in chapter 3 of the textbook. Be sure to follow the loop with a colon. The next several lines will form the body of the for loop. Therefore they must each be indented two spaces from the beginning of the for statement.
The next three lines of your function inside the
for in loop will need to obtain each of the red, green and blue values of the current pixel. The current pixel of the image is automatically provided by the
for in loopVariable. If you have problems with this step refer to the negative() function code in chapter 3 of the textbook.
On the next line choose a variable name to represent the average of the RGB values for the pixel. Set this average variable equal to an arithmetic expression that computes the average of the red, green and blue values of the current pixel.
On the next three lines you will need to compute the reduction for each of the RGB values as explained in step a above. You may elect to use the same variable names for the red, green and blue values that you selected step e. You will need to determine the absolute value of the RGB values from their average. The Python language has a built in absolute value function to perform this step. The function is simply abs(). It excepts one parameter which may be a variable or an arithmetic expression.
Alternatively you could choose three new different variable names for the reduced values. Either way can be made to work. So the question is which is the best technique. Programmers must often make this type of decision. In this case it is clear that the best decision is to use the same variable names for the RGB values. Since the new RGB values are based upon the old RGB values and the old RGB values will no longer be needed in the function. Creating unnecessary variable names is termed polluting the namespace. It unnecessarily increases the complexity of the code. The act of programming or coding is quite complex enough without adding a necessary complexity.
On the next line you will need to choose a variable name for the new color that you will form from the reduced values. JES has a function specifically for this task. The function is
makeColor( red, green, blue). You will need to supply function with the names of the variables you selected for the red, green and blue values of the current pixel.
On the following line, which will be the last line of the
for loop and function, you will change the color of the current pixel to the new reduced color that you just formed. In JES pull down the JES functions menu and select the pixels submenu to determine the function you will need to use to accomplish this task.
In JES save your work, select File - Save Program (ctrl-S).
You may now need to debug this function. Load your program. Fix any syntax errors that JES complains about.
The next step is to execute and test the function. In order to execute the function go back to your driver function. Following the last line that displays the original image, on a new indented line, type the name that you chose for the darkened function and within parentheses type the variable name you selected for the second copy of the image. This this will invoke and execute the function. On the next line display the second image. Check your indentation.
Save your work, (ctrl-S), load the program, fixing any syntax errors.
Test the current version of your program, execute it by typing the name of your driver function
>>> program0() in the command area. Select the frog image again. You should still see the original image appear in a window. Now you should see the JES execution bar, (located to the far right of the Load Program button), going back and forth as your darken function is executing. When it finishes you should see the darkened version of the frog image appear in another window, (the window may be over top of the original image window). If it works correctly the second image that you should see appear will be the same as the following image:
Create a function to lighten the image (i.e., wash in more of the color)
Now you are ready to create your second image transformation function. The purpose of this function will be to lighten the image. In order to achieve this effect each RGB value of each pixel will need to be increased. The increase you will be implementing will also be based upon the average of the RGB values of each pixel. Each RGB pixel value will be increased by three halves of the difference of its value and the average of the pixel RGB values.
Once again, consider a pixel with the following RGB values: 93, 144, 237. The average of these three values is 474/3 = 158. The difference between 93 & 158 is 65. The difference between144 & 158 is 14 and the difference between 237 & 158 is 69. Three halves of each these differences yields 97.5, 21, 103.5 respectively. Thus the new pixel values would be 93+97.5, 144+21, 237+103.5. Which is 190.5, 165, 240.5. Since each of these new values (190.5, 165, 240.5) are greater than the original values (93, 144, 237) the pixel will be lighter.
As you have probably realized by now this color washing in function will be almost identical to the previous color washing out function. Drag your mouse over the previous function to highlight it in JES. Copy the previous function to the clipboard. Click below the previous function on a blank line and be sure that your cursor is all the way to the left of the JES program area. Paste a copy of the previous function below itself. Make sure that teh indentation has been preserved. Correct any spacing misalignment.
Modify the comments to explain what this function will be doing. (If you didn't copy the previous function comments go ahead and do so.) Change the name of the copied function. Name it to reflect that it will be washing color into the image.
Edit the three lines that computed the reduction for each of the RGB values to now compute the three halves increase. That should be the only changes you need to make to the copied function.
In JES save your work, select File - Save Program (ctrl-S).
You may now need to debug this modified function. Load your program and fix any syntax errors that JES complains about.
Execute and test the function. In your driver function, following the last line that displays the darkened (washed-out) image, on a new indented line, type the name that you chose for the modifed (washing-in) function and within parentheses type the variable name you selected for the third copy of the image. This this will invoke and execute the function. On the next line display the third image. Check your indentation.
Save your work, (ctrl-S), load the program, fixing any syntax errors.
Test your program again, execute it by typing the name of your driver function
>>> program0() in the command area. Select the frog image again. You should once again see the original image, then the darkened (washed-out) version of the frog image, and lastly you should in a third window, (the window may be over top of the other image windows) the lightened (wahed-in) frog image appear. If it works correctly the third image that you should see appear will be the same as the following image:
Submit your program to Moodle
When you are satisfied with your work, submit to Moodle.
Locate the program0.py file in your Program-0 folder.
Use the Browse & Upload buttons at the bottom of this page below to submit the program0.py file to moodle. Do NOT submit any other file.
Back up your work
Create a zip archive file containing all of the files in your Program-0 folder. The easiest way to do this is to simply right-click the Program-0 folder in windows explorer and select Send To > Compressed (zipped) Folder.
It is your responsibility to be able to produce a copy of your work. Relying on Moodle or other servers is dangerous. Make a copy of your zip archive on a USB drive, or email the zip to yourself.
In this first programming assignment, you will be writing a few functions to wash out color from an an image and wash color into an image.
Learning Objectives
Familiarity with JES projects
Familiarity with creating a simple functions
Familiarity with saving, loading & executing Jython functions
Exposure to Jython instructions
Exposure to image manipulations
Programming Help
Finally, for this program (and all others) if you get stuck on a step post a description of your problem in the Python Programming forum. The instructor (and your fellow classmates) will offer suggestions. You may also wish to use the Search Forums feature on the course web page to ascertain if someone has already posted the same or similar question and solution suggestions have already been offered.
Coding
In assignment, you will create very simple functions to allow a user to select an image file for display and processing. The trivial washing color out & in manipulations will be performed on the following image:
This image is a reduced version of an image found on Wikipedia.
Refer the examples in chapters two and three if you are unsure how to perform any of the following steps.
Procedure:
Create a Jython driver function for the image
Start JES on your computer.
Create a folder for this program. For example name it Program-0. Right-click on the frog image above and save it in your Program-0 folder. You might wish to make a copy of the file as you will be changing it. Of course you can always re-download it.
Create a new program. A new JES project is initially empty, containing no functions. Select File menu, New Program
Almost all program code files begin with comment statements. These statements give identifying information about the program and programmer. At the top of your program copy and paste the following comment lines. Modify them to include a description of your program, your name and the current date. (You may need to edit the lines in JES after you paste them. Copying and pasting code between programs using the clipboard can often introduce unwanted side effects.)
1
2
3
4
5
6
#MaSc 1044 Program 0: enter a brief description
#Author: enter your name & email
#Version: yyyy.mm.dd
#
#------------------------------------------------------------------------------
#Driver function
Define a new function following the last comment line. In programming, a function is simply a way of referring to a collection of related statements by name. Give your function the name "program0". (In Python, unlike some other programming languages, it is not required that the/a function name in the file be the same as the name of the file.) This will be the driver function for the program. A driver function typically includes instructions to explain the program, to allow a user to perform input and to invoke the main processing functions.
To define a function use the Python keywordd def followed by the name of the function you decide upon. The name of the function must be immediately followed by parentheses () which in turn are immediately followed by a colon. The statements on the following lines will comprise the function. Each of the lines of the function must be consistently indented. It is recommended to use two spaces for the indentation. The indentation will allow you and others to quickly determine which statements belong to which function. In Python, statements at the same indentation level are considered a block of code. The block of code following a function is also termed the body of the function.
On the first indented line following the name of the function write an invocation to the JES showInformation() function to briefly explain the program to the user. The statment will look similar to:
showInformation( "This
program allows a user to ... \n ..." )
This will display the instructions in a pop-up window.
On the following line write a JES instruction to allow a user to select a file. Be sure to assign the selected file to a variable for later referencing.
On the next line, also indented, write a JES instruction to make a picture from the file the user selected. You will also need to assign the created picture to a variable name, (e.g., frog0). Repeat this instruction on the next two lines, changing the variable name in order to create three images from the same file, (e.g.,
frog1 & frog2). This will allow us to view the original file and to view each of the transformations separately.
On the next line of the function include a statement to display the original picture.
At this point you should test to make sure that the statements you have written are operating correctly.
Save your program, (File-Save Program As ...).
Save it in your Program-0 folder.
Name it program0.py. (Be sure to add the extension.)
Load your program, (Depress the "Load Program" button.). Fix any syntax errors that JES complains about.
Once your program loads without errors, execute it by typing the name of your function
>>> program0() in the command area.
If your program is operating correctly you should see a file selection dialog box appear allowing you to select the image of the frog you downloaded. Then the image of the frog should appear in a pop-up window. If your program is behaving improperly you must determine which statements to fix. This is termed debugging your program. When your program is operating correctly proceed to the next step.
If you used print to display your program explanation then it is likely that the print statements' output will appear in the command area after the image is displayed. You may wonder why the print statement output does not appear before the file selection dialog, especially since the print statements are the first in the function.the complete explanation for this phenomenon is fairly technical. It is a result of a programming technique termed buffering. The buffering technique is used to mask the speed differences of the various devices connected to the computer.
Create a function to darken the image (i.e., wash out some of the color)
Now you are ready to create your first image transformation function. The purpose of this function will be to darken the image. In order to achieve this effect each RGB value of each pixel will need to be reduced. The reduction you will be implementing will be based upon the average of the RGB values of each pixel. Each RGB pixel value will be reduced by two thirds of the difference of its value and the average of the pixel RGB values.
For example, consider a pixel with the following RGB values: 93, 144, 237. The average of these three values is 474/3 = 158. The difference between 93 & 158 is 65. The difference between144 & 158 is 14 and the difference between 237 & 158 is 69. Two thirds of each these differences yields 43.333, 9.333, 46 respectively. Thus the new pixel values would be 93-43.333, 144-9.333, 237-46. Which is 49.666, 134.666, 191. Since each of these new values (49.666, 134.666, 191) are less than the original values (93, 144, 237) the pixel will be darker.
All functions should be preceded with comment statements. These comment lines will simply explain what the function accomplishes. Following the last line of your driver function add a couple of blank lines for spacing purposes. Then copy and paste the following comment lines. Modify them to explain this function.
1
2
###############################################################################
#explain what the function does
Following the function explanation lines, define your function to gray out an image. This function will take one parameter which will be the image to be darkened. Don't forget to surround the parameter variable with parentheses and follow it with a colon.
Be sure to choose good meaningful variable names for the function and the parameter. Poor variable names makes a program very hard to understand. It increases the number of things one must remember while programming. Psychologists term this cognitive overload. It is one of the reasons why some people find math problems difficult. They must constantly try to remember what each of the variable names such as X, Y or Z represents in the problem. Large programs may contain literally thousands of variables. Thus good variable names that explain exactly what the variable represents is an absolute must in programming. You may lose points on your programming assignments in this course for choosing very poor variable names.
On the next indented line, following the name of your darken function, you will need to write a for in loop in order to traverse all of the pixels of the image. The syntactical format of the Python for in loop statement is:
for
loopVariable in
list:
You will need to choose an appropriate name for your loop variable. For the loop list you will substitute an array holding all of the pixels of the image.JES provides the
getPixels() function to generate this array. There are several examples of this type of loop in chapter 3 of the textbook. Be sure to follow the loop with a colon. The next several lines will form the body of the for loop. Therefore they must each be indented two spaces from the beginning of the for statement.
The next three lines of your function inside the
for in loop will need to obtain each of the red, green and blue values of the current pixel. The current pixel of the image is automatically provided by the
for in loopVariable. If you have problems with this step refer to the negative() function code in chapter 3 of the textbook.
On the next line choose a variable name to represent the average of the RGB values for the pixel. Set this average variable equal to an arithmetic expression that computes the average of the red, green and blue values of the current pixel.
On the next three lines you will need to compute the reduction for each of the RGB values as explained in step a above. You may elect to use the same variable names for the red, green and blue values that you selected step e. You will need to determine the absolute value of the RGB values from their average. The Python language has a built in absolute value function to perform this step. The function is simply abs(). It excepts one parameter which may be a variable or an arithmetic expression.
Alternatively you could choose three new different variable names for the reduced values. Either way can be made to work. So the question is which is the best technique. Programmers must often make this type of decision. In this case it is clear that the best decision is to use the same variable names for the RGB values. Since the new RGB values are based upon the old RGB values and the old RGB values will no longer be needed in the function. Creating unnecessary variable names is termed polluting the namespace. It unnecessarily increases the complexity of the code. The act of programming or coding is quite complex enough without adding a necessary complexity.
On the next line you will need to choose a variable name for the new color that you will form from the reduced values. JES has a function specifically for this task. The function is
makeColor( red, green, blue). You will need to supply function with the names of the variables you selected for the red, green and blue values of the current pixel.
On the following line, which will be the last line of the
for loop and function, you will change the color of the current pixel to the new reduced color that you just formed. In JES pull down the JES functions menu and select the pixels submenu to determine the function you will need to use to accomplish this task.
In JES save your work, select File - Save Program (ctrl-S).
You may now need to debug this function. Load your program. Fix any syntax errors that JES complains about.
The next step is to execute and test the function. In order to execute the function go back to your driver function. Following the last line that displays the original image, on a new indented line, type the name that you chose for the darkened function and within parentheses type the variable name you selected for the second copy of the image. This this will invoke and execute the function. On the next line display the second image. Check your indentation.
Save your work, (ctrl-S), load the program, fixing any syntax errors.
Test the current version of your program, execute it by typing the name of your driver function
>>> program0() in the command area. Select the frog image again. You should still see the original image appear in a window. Now you should see the JES execution bar, (located to the far right of the Load Program button), going back and forth as your darken function is executing. When it finishes you should see the darkened version of the frog image appear in another window, (the window may be over top of the original image window). If it works correctly the second image that you should see appear will be the same as the following image:
Create a function to lighten the image (i.e., wash in more of the color)
Now you are ready to create your second image transformation function. The purpose of this function will be to lighten the image. In order to achieve this effect each RGB value of each pixel will need to be increased. The increase you will be implementing will also be based upon the average of the RGB values of each pixel. Each RGB pixel value will be increased by three halves of the difference of its value and the average of the pixel RGB values.
Once again, consider a pixel with the following RGB values: 93, 144, 237. The average of these three values is 474/3 = 158. The difference between 93 & 158 is 65. The difference between144 & 158 is 14 and the difference between 237 & 158 is 69. Three halves of each these differences yields 97.5, 21, 103.5 respectively. Thus the new pixel values would be 93+97.5, 144+21, 237+103.5. Which is 190.5, 165, 240.5. Since each of these new values (190.5, 165, 240.5) are greater than the original values (93, 144, 237) the pixel will be lighter.
As you have probably realized by now this color washing in function will be almost identical to the previous color washing out function. Drag your mouse over the previous function to highlight it in JES. Copy the previous function to the clipboard. Click below the previous function on a blank line and be sure that your cursor is all the way to the left of the JES program area. Paste a copy of the previous function below itself. Make sure that teh indentation has been preserved. Correct any spacing misalignment.
Modify the comments to explain what this function will be doing. (If you didn't copy the previous function comments go ahead and do so.) Change the name of the copied function. Name it to reflect that it will be washing color into the image.
Edit the three lines that computed the reduction for each of the RGB values to now compute the three halves increase. That should be the only changes you need to make to the copied function.
In JES save your work, select File - Save Program (ctrl-S).
You may now need to debug this modified function. Load your program and fix any syntax errors that JES complains about.
Execute and test the function. In your driver function, following the last line that displays the darkened (washed-out) image, on a new indented line, type the name that you chose for the modifed (washing-in) function and within parentheses type the variable name you selected for the third copy of the image. This this will invoke and execute the function. On the next line display the third image. Check your indentation.
Save your work, (ctrl-S), load the program, fixing any syntax errors.
Test your program again, execute it by typing the name of your driver function
>>> program0() in the command area. Select the frog image again. You should once again see the original image, then the darkened (washed-out) version of the frog image, and lastly you should in a third window, (the window may be over top of the other image windows) the lightened (wahed-in) frog image appear. If it works correctly the third image that you should see appear will be the same as the following image:
Submit your program to Moodle
When you are satisfied with your work, submit to Moodle.
Locate the program0.py file in your Program-0 folder.
Use the Browse & Upload buttons at the bottom of this page below to submit the program0.py file to moodle. Do NOT submit any other file.
Back up your work
Create a zip archive file containing all of the files in your Program-0 folder. The easiest way to do this is to simply right-click the Program-0 folder in windows explorer and select Send To > Compressed (zipped) Folder.
It is your responsibility to be able to produce a copy of your work. Relying on Moodle or other servers is dangerous. Make a copy of your zip archive on a USB drive, or email the zip to yourself.
Gabrielle T.
0% (0)Projects Completed
-
Freelancers worked with
-
Projects awarded
0%
Last project
21 Jul 2026
United States
New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
There are no clarification messages.
We collect cookies to enable the proper functioning and security of our website, and to enhance your experience. By clicking on 'Accept All Cookies', you consent to the use of these cookies. You can change your 'Cookies Settings' at any time. For more information, please read ourCookie Policy
Cookie Settings
Accept All Cookies