Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Useing Jython Environment for Students (JES), Version 5.0 write a program that t

ID: 3589630 • Letter: U

Question

Useing Jython Environment for Students (JES), Version 5.0

write a program that tries to find a white pixel in a picture through random guesses. Your program will accomplish this by generating two random numbers that will correspond to the x and y locations of a pixel in the image, examining the pixel to see if it is white, and continuing to generate new points until the associated pixel is colored white. Each time a pixel is examined and is not white, it should be colored a new color, such as green or red. This way you will see your program making its guesses until it gets a correct one. If you see white pixels being colored over you know you have a bug in your program!

A .py file has been attached to this lab to get you started. It has a skeleton of the program setup, as well as creating a picture for you, which is a black background with a small white box in the middle. This image is good for testing your program to get an idea if it is working.

The general idea of the "get the pixel" algorithm is below. You will use it to both "prime the pump" and prepare for the loop's next iteration.

Generate two random numbers, from 0 to the width-1 1 or height-1, respectively. Get the pixel that corresponds to those numbers; use the getPixel method. Get the color of that pixel Determine if it is white and proceed accordingly

1 Why -1? Remember we start counting at 0 in programming. Thus, a picture's top left corner is (0,0). If the picture is 300x200, then the top right corner is (299,0) because 0 to 299 is a range of 300. Similarly, the bottom left would be (0,199). Page 2 of 2 In this lab we must access pixels directly instead of relying on a for loop to do it for us. In this lab, you will have to use a getPixel call to assign a pixel to a variable, probably px like we normally use. For example, if you wanted to get the pixel at (5,10) you would do px = getPixel( pic, 5, 10 ).

Making It More Useful

Now, add a counter that keeps track of the number of guesses it takes to find a white pixel. Once a one is found, display a message to the user telling them the number of guesses and the (x,y)- location of said pixel. For example, "It took 39,477 guesses to find a white pixel. This pixel was at location (150, 151)."

Once it appears to be working, get rid of the "black background with white square" picture generating code and replace it with code to ask the user to pick a file for the picture.

Some Tips

Repaint the picture in the body of the while loop so it updates the picture as the program is running. You should see your program working because pixels will constantly be changed to green/red/whatever as it is picking new place within the picture to test. If a green/red/whatever pixel gets written in the white area you know you have a bug, most likely with the conditional you use to test whether to continue looping in the while statement. Functions that you will probably need to use: randrange (or randint), setColor, getColor, getPixel. If you do not remember how to use them, ask me, find them in the slides, or use JES's help system.

Explanation / Answer

# Display a 200x200 pixel image, pixel by pixel.
def setup():
global img
size(200, 200)
img = loadImage("image.jpg")

def draw():
loadPixels()
img.loadPixels()
for y in random.randrange(height):
for x in random.randrange(width):
loc = x + y*width
if not color(255): #check if color is not white
r = red(img.pixels[loc])
g = green(img.pixels[loc])
b = blue(img.pixels[loc])
pixels[loc] = color(r,g,b)
else:
pixels[loc] = color(255) #White color
updatePixels()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote