0

I am attempting to define a function that will blur an image in python without the use of PIL. The color of each pixel needs to be averaged with the colors of the 8 surrounding pixels i.e.:

The value of o needs to be averaged with all the values of x.

x x x
x o x
x x x

I have:

def blur():
global photo
pixels = Image.getPixels(photo)
for row in range(photo.height()):
    for col in range(photo.width()):
        red = pixels[row][col][0]
        green = pixels[row][col][1]
        blue = pixels[row][col][2]
Image.setPixels(photo,pixels)

Where Image.getPixels() returns the red, green, and blue values between 0 and 255 in the same list ([0] returns red, [1] returns green, and [2] returns blue) and their x and y values represented by row and col. I have searched fairly extensively for a hint in the right direction but haven't found anything all that relevant. Any idea/help would be appreciated. Thanks!

user2884456
  • 49
  • 1
  • 4
  • Sounds like you already have an idea of what to do. Average each pixel with its eight surrounding neighbors. What's the problem? – Kevin Oct 28 '13 at 18:32
  • I would say you need to create a hard copy of Image, so that you manipulate the copy with the values of the original (a trick to avoid, for example, to use the already changed values to further change others). Then I would suggest you think about using slices for the average. And all of that being careful with the borders of the image (we don't want to use, say, pixels[row-3] if row=1, don't you agree? Hope they are enough hints in the right direction. – Jblasco Oct 28 '13 at 18:36
  • I guess my problem is I don't fundamentally understand how to obtain the values of the 8 surrounding pixels. I understand how I would average them once I obtain those values but that is where I am stuck. – user2884456 Oct 28 '13 at 18:41
  • Yes -- my apologies if I was supposed to specify that initially – user2884456 Oct 28 '13 at 18:46
  • Well, I am no expert on the rules, but it changes completely the way we try to help you, so you learn instead of you copy... What is Image? how did you import it? Try doing slices with pixels (http://stackoverflow.com/questions/509211/pythons-slice-notation). – Jblasco Oct 28 '13 at 18:48
  • Image is simply a module imported at the beginning of the program that returns the RGB color values of each pixel and their respective x, y values. Thank you, I will take a look at that. – user2884456 Oct 28 '13 at 18:53
  • 1
    I would suggest you open an answer yourself and start rewritting your code with our advice. That way the original question would be available, and you would build the answer with some help. – Jblasco Oct 28 '13 at 18:56

2 Answers2

1

I think this is homework so I am not gonna write out the code. You can probably use numpy vectorize and here is a pointer How to vectorize this python code? to sum three above and the three below then hard code adding in the two to the side and divide by 8. Then reassign red blue and green in a new three dimensional array which you can preallocate to be the size of the old image.

Community
  • 1
  • 1
ford prefect
  • 7,096
  • 11
  • 56
  • 83
0

Ofc to count the average of 8 elements, you need to have the sum of those elements, so start with that. Let's think what indexes those elements would have. If the pixel in the middle is [x][y], than the pixel on it's right would have [x+1][y] and the pixel upon it would have [x][y+1]. Figure out all eight indexes you need and just sum this 8 values. At the end divide the result by 8. Also important thing is to use two different tables (just make a copy of an image to another table) - one you take values from to count the avg, and second in which you save new pixels, because otherwise, using only one table, for counting next pixel you would use the the average of pixel you have already changed.

Natalia
  • 375
  • 3
  • 11