Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to save extracted contours into a text file?

I tried using the command in this link

Here's my code

for i in range(len(contours)):
    i_contour=contours[i]
    np.savetxt('SmallBLockXY.txt',(contours[i]),fmt="%01d",delimiter=" ",newline='\n')

This is the error I am getting

Traceback (most recent call last):
File "/Users/admin/.virtualenvs/test/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1254, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: %d format: a number is required, not numpy.ndarray

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
np.savetxt('SmallBLockXY.txt',(contours[i]),fmt="%01d",delimiter=" ",newline='\n')
  File "/Users/admin/.virtualenvs/test/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int32') and format specifier ('%01d')

I tried an alternative solution

i_contour.tofile('SamllBlockXY.txt',sep=' ')

But with this I'm getting the output of only the last contour which was detected that too all in one like back to back

Any suggestions on how to get the contour coordinates on each point on the contour in a separate line into a text file.