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