Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

python - A for loop reads an image and returns a list of integers in square brackets. However, it is not formatted correctly

I need to add square brackets. Have been attempting to use 'regex' for this, without success.

Attempts at this has produced many error messages.

Exception in Tkinter callback   
Traceback (most recent call last):   
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__   
    return self.func(*args)    
  File "/home/grumpy/other-stuff/change-pixel.py", line 167, in array   
    data = re.sub(r'(])', r'1]]', data)    
  File "/usr/lib/python3.8/re.py", line 210, in sub   
    return _compile(pattern, flags).sub(repl, string, count)  
TypeError: expected string or bytes-like object   

This is the most encountered.
The data structure is;

[[[255, 255, 255] <- needs '[[
[255, 255, 255] 
[255, 255, 255] 
[255, 255, 255] 
[255, 255, 255]]  And every 578 lines it needs
[[255, 255, 255]  And every 579 lines
[255, 255, 255] 
[255, 255, 255] 
[255, 255, 255]
[255, 255, 255]  <- needs ']]'

The code is

for y1 in range(y):
   for x1 in range(x):
        R1, G1, B1 = rgb_im.getpixel((y1, x1)) 
        if R1 == r1 and G1 == g1 and B1 == b1:
            R1 = r2;  G1 = g2;  B1 = b2 
        data = [y1, x1,R1, G1, B1]
        if x1 == x and y1 == y: break 
        if x1 < x:
            x1 = x1 + 1
        if x1 == x:
            data = re.sub(r'(])', r'1]]', data)  <-- ERROR POINT
            y1 = y1 + 1
        print (data)
        arry = np.array(data, dtype=np.uint8)

Also, every thing in the list is integers, and must stay that way However, the square brackets are strings.

question from:https://stackoverflow.com/questions/65623216/a-for-loop-reads-an-image-and-returns-a-list-of-integers-in-square-brackets-how

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's a change that may set you on the right track.

arr = np.zeros((y, x, 3), dtype=np.uint8)    # a "blank" array of the right size
for y1 in range(y):
   for x1 in range(x):
        R1, G1, B1 = rgb_im.getpixel((y1, x1)) 
        if R1 == r1 and G1 == g1 and B1 == b1:
            R1 = r2;  G1 = g2;  B1 = b2 
        arr[y1, x1, :] = [R1, G1, B1]    # set a "pixel' value 

An alternative is

alist = []
for y1 in range(y):
   blist = []
   for x1 in range(x):
        R1, G1, B1 = rgb_im.getpixel((y1, x1)) 
        if R1 == r1 and G1 == g1 and B1 == b1:
            R1 = r2;  G1 = g2;  B1 = b2 
        blist.append([R1, G1, B1])
   alist.append(blist)
   # make a nested list of lists (of lists)
arr = np.array(alist)  # should also be a (y,x,3) array

Print alist or arr to see how the brackets are just a part of the display. Commas, spaces, and new lines are also part of the display. They aren't strings in either the list or the array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...