|
1 | | -""" Here is the challenge problem for 2d loops> |
2 | | -Images are often represented as 3d arrays, |
| 1 | +""" |
| 2 | +Image Average |
| 3 | +
|
| 4 | +Here is the challenge problem for 2D loops: |
| 5 | +Images are often represented as 3D arrays, |
3 | 6 | where the rows and columns are the pixels in the image, |
4 | | -and each pixel has an r, g, and b value. |
| 7 | +and each pixel has an RGB (red, green, blue) value |
| 8 | +which determines the color of the pixel. |
5 | 9 |
|
6 | 10 | The interesting thing is that we can iterate over images. |
7 | 11 | The challenge is, given an image, create a program that |
8 | 12 | will return a different image where each pixel is the average |
9 | 13 | of the pixels surrounding it in the original image. |
10 | 14 |
|
11 | | -The neighbors of an image are all the pixels that surroun it, |
12 | | -1 on each side, and 4 on the diagonals, for 8 in total. |
13 | | -Each pixel doesn't necessarily have 8 neighbors, though (think about why) |
| 15 | +The neighbors of an image are all the pixels that surround it, |
| 16 | +1 on each side, and 4 on the diagonals, for 8 in total. Each |
| 17 | +pixel doesn't necessarily have 8 neighbors, though (think about why). |
14 | 18 |
|
15 | 19 | The code to grab an image from the internet and make it |
16 | 20 | into an array is given to you. The code also displays the new image |
17 | 21 | you create in the end. |
18 | 22 |
|
19 | | -NOTE: The image is 3 dimensional because each pixel has rgb values. |
| 23 | +NOTE: The image is 3 dimensional because each pixel has RGB values. |
20 | 24 | To find the average value of all of a pixels neighbors, you must |
21 | 25 | change the average of the red value to the red value, blue to blue, etc. |
22 | | -For example, if the neighbors of a pixel with value [1,2,3] |
23 | | -were [20,30,40] and [10,120,30], the new pixel that would replace the original one would be |
24 | | -[15,75,35] |
| 26 | +For example, if the neighbors of a pixel with value [1, 2, 3] |
| 27 | +were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the |
| 28 | +original one would be [15, 75, 35] |
25 | 29 | """ |
26 | 30 |
|
27 | 31 | from PIL import Image |
|
37 | 41 |
|
38 | 42 | # write code to create newimg here |
39 | 43 | def solution1(): |
40 | | - """Iterating over the image here. i is a variable from 0 to the width of the image. |
41 | | - j is a variable that ranges from 0 to the height of the image. i is associated with |
42 | | - values""" |
| 44 | + """ |
| 45 | + Iterating over the image here. i is a variable from |
| 46 | + 0 to the width of the image. |
| 47 | + j is a variable that ranges from 0 to the height of the image. |
| 48 | + i is associated with values |
| 49 | + """ |
43 | 50 | for i in range(len(img)): |
44 | 51 | for j in range(len(img[0])): |
45 | 52 | x_n = [0] |
|
0 commit comments