-1

I have a problem stacking while functions to make an algorithm that fills black an image except where it's black within a 2d array the image i want the algorithm to go along the columns anf fill black and when it encounters color it goes back reverse mode until he meet black again. it should color the withe in the picture black here is my code :

the code : thecode

the result : theresult

i dont know why the indentation i = i+1 is not working. Help ?

could someone explain indentation please ?

sonique
  • 13
  • 1
  • Welcome to Stack Overflow. Please read [ask], and show code and textual program results [as text, not an image](https://meta.stackoverflow.com/questions/285551). – Karl Knechtel Feb 01 '23 at 20:55
  • As for the question, it's hard to understand what you are getting at, and "explain indentation" is much too broad for a direct Q&A. However, you may try the existing question [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722); if that resolves the issue then we can consider this a duplicate. The code that you show has numerous indentation errors; the result cannot possibly be the actual result of the code, because that code would not run at all. – Karl Knechtel Feb 01 '23 at 20:56
  • 1
    1) post your code as plain text. 2) post a COMPLETE code example, that we can actually run ourselves, that does not depend on hidden variables. (i.e. it's not enough to just post a function -- you have to show us how that function is called, and the exact parameter values.) – John Gordon Feb 01 '23 at 20:57
  • You only need to indent *inside* the loop. Don't add an extra indent on the line that starts the loop (the `while` or `for`). – Samwise Feb 02 '23 at 13:17

1 Answers1

0

The best I understand your requirement to be is to change the white (background) to black, or change any array value 240 and above to 1.
If so you don't need all the loops, just change the value as you loop through each element if it's greater or equal to 240

from PIL import Image
import numpy as np


def cleanw(t):
    i = 0
    while i < len(t):  # 682 is number of arrays elements in t array
        j = 0
        while j < len(t[i]):  # 1100 is the length of each element
            if t[i, j] >= 240:
                t[i, j] = 1
            j += 1
        i += 1


img = Image.open("sk.png")
t = np.array(img)
cleanw(t)
print(t)
img_out = Image.fromarray(t)
img_out.save('sk2.png')
img_out.show() 

Output (if interested)

[[1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]
 ...
 [1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]]

###----------Additional Information ---------###
OK, being that the above code sample is your end requirement and answers the question 'how to indent and stack while loops', I'll include this code sample which is a better way to achieve the element changes with numpy. Looping through each elelment is not necessary, you can just use the numpy indexing.

from PIL import Image
import numpy as np


img = Image.open("sk.png")
t = np.array(img)
# cleanw(t)
t[t >= 240] = 1   # Change all elements that are greater or equal to 240 to the value 1
print(t)
img_out = Image.fromarray(t)
img_out.save('sk2.png')
img_out.show()
moken
  • 3,227
  • 8
  • 13
  • 23