Add a print statement to Newton’s sqrt function that prints out better each time it is calculated. Call your modified function with 25 as an argument and record the results.
Write a function print_triangular_numbers(n) that prints out the first n triangular numbers. A call to print_triangular_numbers(5) would produce the following output:
1 1
2 3
3 6
4 10
5 15
(hint: use a web search to find out what a triangular number is.)
3.
Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(25))
print(is_prime(7))
print(is_prime(251))
print(is_prime(20))
4.
Modify the walking turtle program so that rather than a 90 degree left or right turn the angle of the turn is determined randomly at each step.
5.
Modify the turtle walk program so that you have two turtles each with a random starting location. Keep the turtles moving until one of them leaves the screen.
Modify the previous turtle walk program so that the turtle turns around when it hits the wall or when one turtle collides with another turtle (when the positions of the two turtles are closer than some small number).
7.
Write a function to remove all the red from an image.
<img src="../_static/LutherBellPic.jpg" id="luther.jpg"> <h4 style="text-align: left;">For this and the following exercises, use the luther.jpg photo.</h4>
import image
def convertBlackWhite(input_image):
grayscale_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())
for col in range(input_image.getWidth()):
for row in range(input_image.getHeight()):
p = input_image.getPixel(col, row)
red = p.getRed()
green = p.getGreen()
blue = p.getBlue()
avg = (red + green + blue) / 3.0
newpixel = image.Pixel(avg, avg, avg)
grayscale_image.setPixel(col, row, newpixel)
blackwhite_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())
for col in range(input_image.getWidth()):
for row in range(input_image.getHeight()):
p = grayscale_image.getPixel(col, row)
red = p.getRed()
if red > 140:
val = 255
else:
val = 0
newpixel = image.Pixel(val, val, val)
blackwhite_image.setPixel(col, row, newpixel)
return blackwhite_image
win = image.ImageWin()
img = image.Image("luther.jpg")
bw_img = convertBlackWhite(img)
bw_img.draw(win)
win.exitonclick()
10.
Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows:
newR = (R × 0.393 + G × 0.769 + B × 0.189)
newG = (R × 0.349 + G × 0.686 + B × 0.168)
newB = (R × 0.272 + G × 0.534 + B × 0.131)
Write a function to convert an image to sepia tone. Hint: Remember that rgb values must be integers between 0 and 255.
11.
Write a function to uniformly enlarge an image by a factor of 2 (double the size).
After you have scaled an image too much it looks blocky. One way of reducing the blockiness of the image is to replace each pixel with the average values of the pixels around it. This has the effect of smoothing out the changes in color. Write a function that takes an image as a parameter and smooths the image. Your function should return a new image that is the same as the old but smoothed.
13.
Write a general pixel mapper function that will take an image and a pixel mapping function as parameters. The pixel mapping function should perform a manipulation on a single pixel and return a new pixel.
When you scan in images using a scanner they may have lots of noise due to dust particles on the image itself or the scanner itself, or the images may even be damaged. One way of eliminating this noise is to replace each pixel by the median value of the pixels surrounding it.
15.
Research the Sobel edge detection algorithm and implement it.
import image
import math
import sys
# Code adapted from http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/image-processing/edge_detection.html
# Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
# this algorithm takes some time for larger images - this increases the amount of time
# the program is allowed to run before it times out
sys.setExecutionLimit(20000)
img = image.Image("luther.jpg")
newimg = image.EmptyImage(img.getWidth(), img.getHeight())
win = image.ImageWin()
for x in range(1, img.getWidth()-1): # ignore the edge pixels for simplicity (1 to width-1)
for y in range(1, img.getHeight()-1): # ignore edge pixels for simplicity (1 to height-1)
# initialise Gx to 0 and Gy to 0 for every pixel
Gx = 0
Gy = 0
# top left pixel
p = img.getPixel(x-1, y-1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
# intensity ranges from 0 to 765 (255 * 3)
intensity = r + g + b
# accumulate the value into Gx, and Gy
Gx += -intensity
Gy += -intensity
# remaining left column
p = img.getPixel(x-1, y)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gx += -2 * (r + g + b)
p = img.getPixel(x-1, y+1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gx += -(r + g + b)
Gy += (r + g + b)
# middle pixels
p = img.getPixel(x, y-1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gy += -2 * (r + g + b)
p = img.getPixel(x, y+1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gy += 2 * (r + g + b)
# right column
p = img.getPixel(x+1, y-1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gx += (r + g + b)
Gy += -(r + g + b)
p = img.getPixel(x+1, y)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gx += 2 * (r + g + b)
p = img.getPixel(x+1, y+1)
r = p.getRed()
g = p.getGreen()
b = p.getBlue()
Gx += (r + g + b)
Gy += (r + g + b)
# calculate the length of the gradient (Pythagorean theorem)
length = math.sqrt((Gx * Gx) + (Gy * Gy))
# normalise the length of gradient to the range 0 to 255
length = length / 4328 * 255
length = int(length)
# draw the length in the edge image
newpixel = image.Pixel(length, length, length)
newimg.setPixel(x, y, newpixel)
newimg.draw(win)
win.exitonclick()