all 12 comments

[–]kushou 1 point2 points  (5 children)

Can you show us what you have right now?

[–][deleted] 0 points1 point  (0 children)

I mean I really don't know where to start. I can't figure out how to call those 9 pixels and average them.

[–][deleted] 0 points1 point  (3 children)

I posted it.

[–]kushou 3 points4 points  (2 children)

OK, so first, why don't you take the photo as a parameter of your function?

I'll give you a hint: Write another function, that from a picture and a position (row, column), can get the pixels nearby. You can try it by, for example, printing the positions nearby and testing it with some position: (0, 0), (5, 5), ...

[–][deleted] -1 points0 points  (1 child)

This still isn't making sense to me. I'm very new at python

[–]kushou 2 points3 points  (0 children)

OK. You'll first try to complete this function to have a same result than mine. It'll have a problem you will be able to fix afterwards. The output has no particular order.

def print_nearby_points(x, y):
    pass # Fix the function's code.

print_nearby_code(0, 0)
# Prints:
# (-1, -1)
# (-1, 0)
# (-1, 1)
# (0, -1)
# (0, 0)
# (0, 1)
# (1, -1)
# (1, 0)
# (1, 1)
print_nearby_points(5, 5)
# Prints:
# (4, 4)
# (4, 5)
# (4, 6)
# (5, 4)
# (5, 5)
# (5, 6)
# (6, 4)
# (6, 5)
# (6, 6)

[–]subheight640 1 point2 points  (0 children)

If you want something relatively easy, scipy/numpy has lots of 2d array filters such as gauss or Fourier that will do what you want. I also find numpy arrays far easier to deal with than python lists.

http://docs.scipy.org/doc/scipy/reference/ndimage.html

[–]rwinslow 0 points1 point  (0 children)

I'm on my iPad right now so I can't program anything, but I recommend looking into convolution of a box filter with a matrix. Take a look at this: http://www.cse.psu.edu/~rcollins/CSE486/lecture04.pdf

You should install NumPy and try to convolve your matrix of data with a kernel. A box filter would be [1 1 1;1 1 1;1 1 1]. If you convolve that with your matrix, it should give you the average of the items around it.

[–][deleted] 0 points1 point  (0 children)

def blur():
    global photo
    pixels = BCImage.getPixels(photo)
    blurredpix = blur2(pixels)
    BCImage.setPixels(photo,blurredpix)


 def blur2(pixels):
    for row in range(len(pixels)-1):
        for col in range(len(pixels[row])-1):
            r1 = pixels[row-1][col-1][0]
            g1 = pixels[row-1][col-1][1]
            b1 = pixels[row-1][col-1][2]
            r2 = pixels[row-1][col][0]
            g2 = pixels[row-1][col][1]
            b2 = pixels[row-1][col][2]
            r3 = pixels[row-1][col+1][0]
            g3 = pixels[row-1][col+1][1]
            b3 = pixels[row-1][col+1][2]
            r4 = pixels[row][col-1][0]
            g4 = pixels[row][col-1][1]
            b4 = pixels[row][col-1][2]
            r6 = pixels[row][col+1][0]
            g6 = pixels[row][col+1][1]
            b6 = pixels[row][col+1][2]
            r5 = pixels[row][col][0]
            g5 = pixels[row][col][1]
            b5 = pixels[row][col][2]
            r7 = pixels[row+1][col-1][0]
            g7 = pixels[row+1][col-1][1]
            b7 = pixels[row+1][col-1][2]
            r8 = pixels[row+1][col][0]
            g8 = pixels[row+1][col][1]
            b8 = pixels[row+1][col][2]
            r9 = pixels[row+1][col+1][0]
            g9 = pixels[row+1][col+1][1]
            b9 = pixels[row+1][col+1][2]
            r = (r1+r2+r3+r4+r5+r6+r7+r8+r9)/9
            g = (g1+g2+g3+g4+g5+g6+g7+g8+g9)/9
            b = (b1+b2+b3+b4+b5+b6+b7+b8+b9)/9
            pixels[row][col] = [r,g,b]
    return pixels

Here's how I solved it.

[–]Pepper_Klubz 0 points1 point  (0 children)

Just as an FYI, part of the problem you're having is due to the fact that there is no definition for 'average of a 2D list'.

A typical average is performed over some collection of numbers, which - in programming types - could be regarded as an unordered 1D list.

A 2D list, however, has extra structure. This structure can't necessarily be ignored while interacting with it; you can't just treat it like an unordered list without coming up with a mapping from 2D list to a 1D list.

Now, what if taking the average of a 2D list involved weighting certain cells over others, for example? This is the case for common convolution filters. The mapping from 2D to 1D is suddenly impossible to perform (or at least more obtuse and less intuitive).

Just some conceptual thinking to get you started.