Guidance for Chapter 8, exercises 11 and 12

Hello, I did study the chapter 8 last week, and I resolved the 5 exercises, anyway the weekends Im trying to finish 100% of the exercises for each chapter for practicing.
I made the code for exercises 11 and 12 too, and it is working (I mean, my code do what they asw to do) but it took very long time to run.
I added the setExecutionLimit and the program finish and show the image as it should. But I keep thinking I made a wrong solution because is very long time.
I finally used 4 nested loops.
The first 2 loops(for) is to run along every pixel into the image. Im not starting in pixel 0,0, instead, Im starting in pixel1,1
the next 2 loop (for) Im runing through every pixel around (to get the average value)
Im not sure if this is the reason cause it take so long for finish. and if there are other way.
I thought I can try to make the average getting the values in each pixel without the last 2 nested loops, but it seems like silly to repeat 9 times the same code for get the red, green and blue values and make the average like that.

Im not sure what code can I paste here, or if someone could give me an idea to do it in a diferent way to make it faster.
Thank you very much!!!


I am assuming your solution for Exercise 11 is building on your Exercise 10 solution. In Exercise 10, to enlarge the image, I needed only one nested loop (two loops total: the columns loop inside the rows loop).

Is this how you solved Exercise 10?

Hi Leonid!
Yes. In every exercise before the 11 exercise I just need 2 loops because I just need the information of 1 pixel each time.
The exercise 11 and 12 make the new pixel with the average of values of 9 pixels.
Then I made the other 2 loops to go through the 9 pixels every time.

Hi again,

We are on the right track then. :slight_smile:

Were you in class for the Blurring Studio? Do you remember how a random surrounding pixel fwas picked? It only required 2 loops (one nested inside the other). You can use the same approach here, except you will get the values from all of the 9 pixels, within the same iteration. If you use i for the columns and j for the rows, the indexes for the surrounding pixels will range from i-1 to i+1 and j-1 to j+1.

Thanks for keep answering !

Im not going to class yet cause Im out of the country and I was not able to back before start classes… But Im doing the studio cases for myself.

Yes… I did the bluring studio.
And yes…it need 1 random pixel from pixels around the main pixel…but is just 1…and I choose it with the random.randrange function… Whit this function I get random numbers, then I take this numbers as point x and point y for the new pixel and then remplace those values…
That is just 2 code lines.
In this case…I need to open each one of the 9 pixels and take the 3 values (red,green and blue) make the avg and then make the new pixel. That is 4 code lines for each pixel. 4x9=36… That is why I made it into other nested loops. Im doing wrong? Is just tht I am not imagining other answer?

This got me into re-thinkign and re-factoring my own code, and now I feel like I may be lost myself. :fearful::grinning:

Anyway, back to your original question: You said you used 4 sets nested loops, correct? If I understand your approach, you will need only 3 sets total:

  1. First, to enlarge the image;
  2. Then to iterate over all the pixels of the enlarged image;
  3. Finally, to iterate over the 9 surrounding pixels.

Or am I wrong?

Im sorry. I need improve my English. Maybe Im not explain right the idea here. Not rethink in your code… It must to be me who dont explain this corectly.

Back to the problem…

The exercise 10 (make bigger the original image) I did with 2 loops. First for rows. Second for columns.

The exercise 11 and 12, I could do with 2 loops too. But the block for get the average of the 9 pixels around would result 36 code lines.
For example, if I want work in the pixel (1,1), I need to do something like that

  1. Get the pixel (0,0)

  2. Get the red intense value for this pixel

  3. Get the green " " " " "

  4. Get the blue…"…

  5. Get the pixel (0,1)

  6. Get the red value and sum the red value of this pixel to the red value for the step 2

  7. Get the green value and sum the green value of this pixel, to the green value in step 3

  8. Get the blue value and sum the blue value of this pixel to the blue value in step 4

  9. Get the pixel (0,2)

  10. Get the red value and sum with the acumulate pater for red

  11. Get the green and sum with the acumulate p.for the green

  12. Get the blue value and sum with the acumulate p. for the blue

  13. Get the pixel (1,0)

  14. Get the pixel (1,1)
    21.pixel (1,2)

29…pixel (2,1)
33. Get the pixel (2,2)
34. Get the red valie for this pixel and sum it to the acumulate for red
35. Get the green value for this pixel and sum to the …
36. Get the blue value for this pixel and sum…

  1. Make the new pixel with :
    Red = acumulate_red//9
    Green = acumulate_green//9
    Blue = acumulate_blue //9

You see? Just to give the average value in red, blue and green for 1 pixel I would need 4 code lines repeat 9 times…
Then I put this 4 code lines in the other 2 loops…1 for rows…1 for columns…

There are other way to do this without the new 2 loops and without repeat 4 code lines 9 times?


You definitely got it. You are doing it correctly. Yes, it will be a lot of line of code if you don’t use a loop to iterate over the 9 surrounding pixels. It’ll look better if you use a nested loop (one for rows, the other for columns) for the surrounding pixels. I don’t see any other ways of doing it.

If you are on Slack, you may have seen me asking if we could share the code. Unfortunately, I was told no. You may want to check with your TA.

Just a quick additional note: You could combine some of your code into fewer lines. Admittedly, that may make your lines very long and possibly hard to read. Example:

sum_of_red = img.getPixel(col-1, row-1).getRed()
         + img.getPixel(col-1, row).getRed()
         + img.getPixel(col-1, row+1).getRed()
         + img.getPixel(col+1, row+1).getRed()

This combines 9 or 10 statements into 1. Same for green and red blue. You can obviously wrap the right side into parentheses and divide by the number of pixels to get the average value for each color. That will save you a few more individual statements.

I am not saying that this is the most elegant code, but it works.

1 Like

Ohhh…yes! I did not know that you can get the pixel and get the color in 1 statment. Even if (as you said) it not look “so elegant” I think it could fix the problem of the long time!
I apreciate your help sooo soooo much!
I will try it just now! :raised_hands::slight_smile::fire:Thank youuuu!

1 Like