## Joseph Drackey

All generations.

generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def to_gray(image): # image is a 3d numpy array (x, y, z) = image.shape gray = np.zeros((x, y)) for i in range(x): for j in range(y): gray[i][j] = (image[i][j][0] + image[i][j][1] + image[i][j][2]) / 3 return gray img = np.zeros((2, 2, 3)) # blue image, 2x2 to_gray(img)```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(image, rgb_tuple): """ This function takes an RGB image and converts it to a posterized image. """ # Define your new image using a copy of the original image new_image = image.copy() # Define the RGB threshold values red_threshold = rgb_tuple[0] green_threshold = rgb_tuple[1] blue_threshold = rgb_tuple[2] # Use a nested for-loop to loop through the rows and columns of the image for row in range(new_image.shape[0]): for col in range(new_image.shape[1]): # Use if statements to determine whether to assign 0 or 255 to each # channel in the image if new_image[row][col][0] < red_threshold: new_image[row][col][0] = 0 else: new_image[row][col][0] = 255 if new_image[row][col][1] <```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def to_grayscale(image): return np.dot(image[...,:3], [0.299, 0.587, 0.114]) to_grayscale()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, radius): t.penup() t.goto(x, y) t.pendown() t.circle(radius) t.penup() t.goto(x, y) t.pendown() t.dot() def main(): wn = turtle.Screen() wn.bgcolor("white") wn.title("Exercise 5.3") alex = turtle.Turtle() alex.color("green") alex.pensize(2) drawCircle(alex, 50, 75, 100) wn.mainloop() main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```def drawCircle(myTurtle, x, y, radius): myTurtle.penup() myTurtle.setposition(x,y) myTurtle.pendown() for i in range(120): myTurtle.forward(2*3.14*radius/120) myTurtle.right(3) myTurtle.penup()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, radius): t.penup() t.setpos(x, y - radius) t.pendown() t.circle(radius) t.penup() t.setpos(x, y - radius) t.pendown() t.stamp() def main(): t = turtle.Turtle() t.shape("turtle") t.speed(0) drawCircle(t, 50, 75, 100) turtle.done() main()```

Python
View
generate functionTue, 28 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```import turtle def drawFractalLine(distance, angle, level): if level == 0: turtle.forward(distance) else: drawFractalLine(distance / 3, angle, level - 1) turtle.left(angle) drawFractalLine(distance / 3, angle, level - 1) turtle.right(2 * angle) drawFractalLine(distance / 3, angle, level - 1) turtle.left(angle) drawFractalLine(distance / 3, angle, level - 1) def main(): turtle.left(90) drawFractalLine(200, 60, 4) turtle.right(120) drawFractalLine(200, 60, 4) turtle.right(120) drawFractalLine(200, 60, 4) turtle.done() main()```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(img, rgb): return ImageOps.posterize(img, rgb)```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```def sharpen(image, radius, amount): new_image = image.clone() for y in range(image.getHeight()): for x in range(image.getWidth()): pixel = image.getPixel(x, y) new_pixel = new_image.getPixel(x, y) for i in range(3): color = pixel.getColor()[i] new_color = color * radius + (255 - radius) * (color / 255) * amount new_color = int(new_color) color_list = list(new_pixel.getColor()) color_list[i] = new_color new_pixel.setColor(color_list) return new_image```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(image, rgb): for p in getPixels(image): setRed(p, rgb[0]) setGreen(p, rgb[1]) setBlue(p, rgb[2])```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(myTurtle, x, y, radius): myTurtle.penup() myTurtle.goto(x, y - radius) myTurtle.pendown() circumference = 2 * 3.14159265359 * radius side_length = circumference / 120 turn_angle = 360 / 120 myTurtle.circle(radius, steps = 120) myTurtle.penup() myTurtle.goto(x, y) myTurtle.pendown() def main(): t = turtle.Turtle() myWin = turtle.Screen() t.speed(100) drawCircle(t, 50, 75, 100) myWin.exitonclick() main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, radius): t.goto(x, y) t.setheading(90) distance = 2.0 * math.pi * radius / 120.0 for i in range(120): t.forward(distance) t.left(3) def main(): t = turtle.Turtle() drawCircle(t, 50, 75, 100) main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, r): t.penup() t.goto(x,y) t.pendown() r = r / 2 for i in range(120): t.forward(2 * 3.14 * r / 120) t.left(3) def main(): t = turtle.Turtle() t.shape("turtle") t.speed(0) drawCircle(t, 50, 75, 100) main()```

Python
View
generate functionWed, 29 Mar 2023

## The edge-detection function (detectEdges) described in Chapter 7 and shown below returns a black and white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. Start the code this way: from images import Image def sharpen(image, degree, threshold):

```def sharpen(image, degree, threshold): for x in range(image.getWidth()): for y in range(image.getHeight()): p = image.getPixel(x, y) r = p.getRed() g = p.getGreen() b = p.getBlue() sum = r+g+b avg = sum/3 red = r + degree*(r-avg) green = g + degree*(g-avg) blue = b + degree*(b-avg) newPixel = Pixel(red, green, blue) image.setPixel(x, y, newPixel) return image```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def convertToGray(img): # loop over each pixel for r in range(img.shape[0]): for c in range(img.shape[1]): # get the avg of the r, g, b values avg = int((img[r, c, 0] + img[r, c, 1] + img[r, c, 2])/3) # set the pixel to the avg value img[r, c] = [avg, avg, avg] return img```

Python
View
generate functionTue, 28 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black

```def posterize(image, bwLevels): for pixel in image.getPixels(): red = pixel.getRed() green = pixel.getGreen() blue = pixel.getBlue() average = (red + green + blue) / 3 if average < bwLevels[0]: newRed = bwLevels[0] newGreen = bwLevels[0] newBlue = bwLevels[0] elif average < bwLevels[1]: newRed = bwLevels[1] newGreen = bwLevels[1] newBlue = bwLevels[1] elif average < bwLevels[2]: newRed = bwLevels[2] newGreen = bwLevels[2] newBlue = bwLevels[2] else: newRed = 255 newGreen = 255 newBlue = 255 pixel.setRed(newRed) pixel.set```

Python
View
generate functionTue, 28 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black

```def posterize(image, rgb): img = image.copy() for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col, row) if p.getRed() < rgb[0]: r = 0 elif p.getRed() > rgb[0]: r = 255 else: r = p.getRed() if p.getGreen() < rgb[1]: g = 0 elif p.getGreen() > rgb[1]: g = 255 else: g = p.getGreen() if p.getBlue() < rgb[2]: b = 0 elif p.getBlue() > rgb[2]: b = 255 else: b = p.getBlue() newPixel = image.Pixel(r, g, b) img.setPixel(col, row, newPixel) return img```

Python
View
generate functionMon, 27 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```import turtle def drawFractalLine(t, distance, angle, level): if level == 0: t.forward(distance) else: drawFractalLine(t, distance / 3, angle, level - 1) t.left(angle) drawFractalLine(t, distance / 3, angle, level - 1) t.right(2 * angle) drawFractalLine(t, distance / 3, angle, level - 1) t.left(angle) drawFractalLine(t, distance / 3, angle, level - 1) def main(): wn = turtle.Screen() wn.bgcolor("lightgreen") wn.screensize(200, 200) t = turtle.Turtle() t.color("blue") t.speed(0) t.penup() t.setposition(-150, -100) t.pendown() drawFractalLine(t,```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, r): t.penup() t.goto(x, y - r) t.pendown() t.circle(r) def main(): wn = turtle.Screen() wn.bgcolor("lightblue") alex = turtle.Turtle() alex.pensize(3) drawCircle(alex, 50, 75, 100) wn.exitonclick() main()```

Python
View
generate functionTue, 28 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```import turtle def drawFractalLine(distance, angle, level): #TODO: Implement this function def main(): #TODO: Use this function to draw the Koch snowflake main()```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```def sharpen(original, width, height): blank = Image.blank(original.width, original.height) for y in range(height, original.height - height): for x in range(width, original.width - width): pixel = original.get_pixel(x, y) blank.set_pixel(x, y, pixel) # Find pixel, pixel to the left, and pixel above for y in range(height, original.height - height): for x in range(width, original.width - width): pixel = original.get_pixel(x, y) left = original.get_pixel(x-1, y) above = original.get_pixel(x, y-1) # Compute the sharpened color red = weighted_average(pixel.red, left.red, above.red) green = weighted_average(pixel.green, left.green, above.green) blue = weighted_average(pixel.blue, left.blue, above.blue) newpixel = Pixel```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```from turtle import Turtle def drawCircle(t, x, y, radius): t.up() t.goto(x, y-radius) t.down() t.circle(radius) t.up() def main(): t = Turtle() drawCircle(t, 50, 75, 100) main()```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(image, tup): for row in range(image.height): for col in range(image.width): p = image.getPixel(col, row) newRed = tup[0] newGreen = tup[1] newBlue = tup[2] newpixel = image.Pixel(newRed, newGreen, newBlue) image.setPixel(col, row, newpixel) return image posterize(image, (255, 0, 0))```

Python
View
generate functionTue, 28 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```def drawFractalLine(myTurtle, distance, angle, level): if level == 0: myTurtle.forward(distance) else: newDistance = distance / 3 drawFractalLine(myTurtle, newDistance, angle, level - 1) myTurtle.left(angle) drawFractalLine(myTurtle, newDistance, angle, level - 1) myTurtle.right(angle * 2) drawFractalLine(myTurtle, newDistance, angle, level - 1) myTurtle.left(angle) drawFractalLine(myTurtle, newDistance, angle, level - 1) def main(): myTurtle = turtle.Turtle() myTurtle.speed(0) myWin = turtle.Screen() myTurtle.up() myTurtle.goto(-100, -50) myTurtle.down() drawFractalLine(myTurtle, 200, 60, 4) my```

Python
View
generate functionWed, 29 Mar 2023

## The edge-detection function (detectEdges) described in Chapter 7 and shown below returns a black and white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. Start the code this way: from images import Image def sharpen(image, degree, threshold):

```from images import Image def sharpen(image, degree, threshold): for x in range(image.getWidth()): for y in range(image.getHeight()): p = image.getPixel(x, y) r=p.getRed() g=p.getGreen() b=p.getBlue() if r > threshold: r = r + degree if b > threshold: b = b + degree if g > threshold: g = g + degree newpixel = Pixel(r, g, b) image.setPixel(x, y, newpixel) return image```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def gray_scale(img): gray_image = np.zeros(img.shape) gray_image[:, :, 0] = gray_image[:, :, 1] = gray_image[:, :, 2] = np.mean(img[:, :, :], axis=2) return gray_image```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(img, rgb_tuple): num_bins = len(rgb_tuple) img_mod = img.copy() for channel in range(3): channel_values = img[:,:,channel] for bin_index in range(num_bins): if bin_index == num_bins - 1: mask = channel_values >= rgb_tuple[bin_index] else: mask = np.logical_and(channel_values >= rgb_tuple[bin_index], channel_values < rgb_tuple[bin_index + 1]) img_mod[mask, channel] = rgb_tuple[bin_index] return img_mod```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```def sharpen(image, threshhold, factor): for y in range(image.getHeight()): for x in range(image.getWidth()): pixel = image.getPixel(x, y) if y == 0 or x == 0 or y == image.getHeight()-1 or x == image.getWidth() -1: continue right = image.getPixel(x+1, y) left = image.getPixel(x-1, y) top = image.getPixel(x, y-1) bottom = image.getPixel(x, y+1) r = right.getRed() - pixel.getRed() g = right.getGreen() - pixel.getGreen() b = right.getBlue() - pixel.getBlue() if r > threshhold or r < -threshhold: pixel.setRed(pixel.getRed() + factor * r) if g > threshhold or g < -threshhold: pixel.setGreen(pixel.get```

Python
View
generate functionMon, 27 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```def drawFractalLine(distance, angle, level): if level == 0: t.forward(distance) else: drawFractalLine(distance/3, angle, level-1) t.left(angle) drawFractalLine(distance/3, angle, level-1) t.right(angle*2) drawFractalLine(distance/3, angle, level-1) t.left(angle) drawFractalLine(distance/3, angle, level-1) def main(): t.speed('fastest') for i in range(3): drawFractalLine(size, angle, level) t.right(120) t.done()```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def gray_scale(img): img_height = len(img) img_width = len(img[0]) gray_img = [] for i in range(img_height): gray_img.append([]) for j in range(img_width): gray_img[i].append(0) for k in range(3): gray_img[i][j] = gray_img[i][j] + img[i][j][k] gray_img[i][j] = gray_img[i][j]/3 return gray_img```

Python
View
generate functionWed, 29 Mar 2023

## The edge-detection function (detectEdges) described in Chapter 7 and shown below returns a black and white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. Start the code this way: from images import Image def sharpen(image, degree, threshold):

```from images import Image def sharpen(image, degree, threshold): for x in range(image.getWidth()): for y in range(image.getHeight()): (red, green, blue) = image.getPixel(x, y) if (red <= threshold) and (blue <= threshold) and (green <= threshold): newred = 0 newgreen = 0 newblue = 0 else: newred = red + degree newgreen = green + degree newblue = blue + degree image.setPixel(x, y, (newred, newgreen, newblue)) return image```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def grayscale(image): width = image.get_width() height = image.get_height() newImage = pg.Surface((width, height)) for y in range(height): for x in range(width): r,g,b = image.get_at((x,y)) newImage.set_at((x,y), (int((r+g+b)/3), int((r+g+b)/3), int((r+g+b)/3))) return newImage```

Python
View
generate functionWed, 29 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments.

```def posterize(img, rgb_tuple): """Given a tuple of RGB values and an image, returns a copy of the image with each color replaced by the corresponding values from the tuple.""" new_img = img.copy() for pixel in new_img: for i in range(3): pixel[i] = rgb_tuple[i] return new_img```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def grayscale(im): return np.average(im, axis=2)```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```def drawCircle(t, x, y, radius): t.penup() t.goto(x, y) t.pendown() step = 2.0 * 3.1416 * radius / 120.0 for i in range(120): t.left(3) t.forward(step) def main(): t = turtle.Turtle() drawCircle(t, 50, 75, 100) turtle.done() main()```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```def sharpen(image, width, height): original_image = image for y in range(height, original_image.getHeight() - height): for x in range(width, original_image.getWidth() - width): (red, green, blue) = original_image.getPixel(x, y) red2 = original_image.getPixel(x - 1, y) red4 = original_image.getPixel(x + 1, y) red5 = original_image.getPixel(x, y - 1) red6 = original_image.getPixel(x, y + 1) sharpred = red * 4 - red2[0] - red4[0] - red5[0] - red6[0] if sharpred < 0: sharpred = 0 elif sharpred > 255: sharpred = 255 green2 = original_image.getPixel(x - 1, y) green4 = original_image.getPixel(x + 1, y) green5```

Python
View
generate functionTue, 28 Mar 2023

## Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black

```def posterize(image, rgb): for pixel in image.getPixels(): pixel.red = rgb[0] pixel.green = rgb[1] pixel.blue = rgb[2] return image```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```def convert_to_grayscale(image): gray_image = image.copy() gray_image[:, :, 0] = .2126 * image[:, :, 0] gray_image[:, :, 1] = .7152 * image[:, :, 1] gray_image[:, :, 2] = .0722 * image[:, :, 2] return gray_image```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()```

Python
View
generate functionTue, 28 Mar 2023

## Converts an image to grayscale using the crude average of the r, g, and b

```from PIL import Image import numpy as np def imageToGrayScale(imageName): im = Image.open(imageName) grayIm = im.convert('LA') grayIm.save('gray' + imageName) imageToGrayScale("lena.png")```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, r): t.goto(x, y) t.down() t.circle(r) t.up() def main(): wn = turtle.Screen() wn.bgcolor("black") wn.title("Circle") wn.setup(600, 600) t = turtle.Turtle() t.color("red") t.pensize(4) t.speed(0) drawCircle(t, 50, 75, 100) wn.exitonclick() main()```

Python
View
generate functionWed, 29 Mar 2023

## The edge-detection function (detectEdges) described in Chapter 7 and shown below returns a black and white image. Think of a similar way to transform color values so that the new image is still in its original colors but the outlines within it are merely sharpened. Then, define a function named sharpen that performs this operation. The function should expect an image and two integers as arguments. One integer should represent the degree to which the image should be sharpened. The other integer should represent the threshold used to detect edges. Start the code this way: from images import Image def sharpen(image, degree, threshold):

```def sharpen(image, degree, threshold): for x in range(image.getWidth()): for y in range(image.getHeight()): (r,g,b) = image.getPixel(x,y) SharpPixel = (r-degree,g-degree,b-degree) image.setPixel(x,y,SharpPixel) return image im = Image("luther.jpg") im = sharpen(im, 1, 10) im.draw()```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```def sharpen(image, max_x, max_y): newimage = Image(image.getWidth(), image.getHeight()) for y in range(image.getHeight()): for x in range(image.getWidth()): r, g, b = image.getPixel(x, y) if x > max_x and x < (image.getWidth() - max_x) and y > max_y and y < (image.getHeight() - max_y): newimage.setPixel(x, y, r, g, b) else: r = 255 g = 255 b = 255 newimage.setPixel(x, y, r, g, b) return newimage```

Python
View
generate functionMon, 27 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```from turtle import * def drawFractalLine(distance, angle, level): if level == 0: forward(distance) return else: drawFractalLine(distance/3, angle, level-1) left(angle) drawFractalLine(distance/3, angle, level-1) right(angle*2) drawFractalLine(distance/3, angle, level-1) left(angle) drawFractalLine(distance/3, angle, level-1) def main(): speed(0) width(2) up() backward(200) down() for i in range(3): drawFractalLine(300, 60, 4) right(120) up() forward(300) main()```

Python
View
generate functionThu, 30 Mar 2023

## Complete this program with a sharpen function that sharpens the edge of an image while keeping its original colors: from images import Image def main(): filename = input("Enter the image file name: ") image = Image(filename) newimage = sharpen(image, 20, 15) newimage.draw() if __name__ == "__main__": main()

```from images import Image def sharpen(img, threshold, amount): newimg = img.clone() w = img.getWidth() h = img.getHeight() for x in range(1, w-1): for y in range(1, h-1): p = img.getPixel(x, y) left = img.getPixel(x-1, y) above = img.getPixel(x, y-1) right = img.getPixel(x+1, y) below = img.getPixel(x, y+1) r = p.getRed() g = p.getGreen() b = p.getBlue() leftr = left.getRed() leftg = left.getGreen() leftb = left.getBlue() abover = above.getRed() aboveg = above.getGreen() aboveb = above.getBlue() rightr = right.getRed() rightg = right.getGreen()```

Python
View
generate functionTue, 28 Mar 2023

## At the top level, the script uses a function drawFractalLine to draw three fractal lines. Each line is specified by a given distance, direction (angle), and level: The initial angles are 0, -120, and 120 degrees. The initial distance can be any size, such as 200 pixels. If the level is 0, then the turtle moves the given distance in the given direction. Otherwise, the function draws four fractal lines with ⅓ of the given distance, angles that produce the given effect, and the given level minus 1. The function drawFractalLine is recursive. Write a script that draws the Koch snowflake. Define a function main that will draw a Koch snowflake with the following parameters when the program is run: Width = 200 Height = 200 Size = 150 Level = 4

```import turtle def drawFractalLine(distance, angle, level): if level > 0: drawFractalLine(distance/3, angle, level - 1) turtle.right(angle) drawFractalLine(distance/3, angle, level - 1) turtle.left(2*angle) drawFractalLine(distance/3, angle, level - 1) turtle.right(angle) drawFractalLine(distance/3, angle, level - 1) else: turtle.forward(distance) def main(): turtle.setup(width = 200, height = 200) turtle.speed(0) turtle.penup() turtle.goto(-100, -120) turtle.pendown() turtle.color("black", "lightblue") turtle.begin_fill() turtle.pensize(3) turtle.penup() turtle.goto(-50, -120) turtle.pendown() drawFractal```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```from turtle import * def drawCircle(t, x, y, radius): t.penup() t.goto(x, y-radius) t.pendown() t.circle(radius) def main(): wn = Screen() alex = Turtle() drawCircle(alex, 50, 75, 100) wn.exitonclick() main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(t, x, y, r): t.penup() t.goto(x, y) t.pendown() t.circle(r) def main(): wn = turtle.Screen() t = turtle.Turtle() drawCircle(t, 50, 75, 100) wn.exitonclick() main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import turtle def drawCircle(turtle, x, y, radius): turtle.penup() turtle.goto(x, y - radius) turtle.pendown() turtle.circle(radius) def main(): wn = turtle.Screen() wn.bgcolor("lightgreen") tess = turtle.Turtle() tess.color('hotpink') drawCircle(tess, 50, 75, 100) wn.exitonclick() main()```

Python
View
generate functionMon, 27 Mar 2023

## Define a function drawCircle. This function should expect a Turtle object, the coordinates of the circle’s center point, and the circle’s radius as arguments. The function should draw the specified circle. The algorithm should draw the circle’s circumference by turning 3 degrees and moving a given distance 120 times. Calculate the distance moved with the formula 2.0 × π × radius ÷ 120.0. Define a function main that will draw a circle with the following parameters when the program is run: X = 50 Y = 75 Radius = 100

```import math def drawCircle(turtle, x, y, radius): for i in range(120): turtle.forward(2.0 * math.pi * radius / 120.0) turtle.left(3) def main(): wn = turtle.Screen() alex = turtle.Turtle() drawCircle(alex, 50, 75, 100) wn.exitonclick() main()```

Python
View