Home > Enterprise >  Change 2D array default .txt formatting
Change 2D array default .txt formatting

Time:10-13

Default = 37 vals per line

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 1 0 0 0

Wanted = 2D array height & len to resemble image, as text.

Why = to make next processing step is easier to imagine.

Solution didn't jump out to me in help(np.array, np.reshape, np.array2string, np.set_printoptions).

array2string (max_line_width =None)
    max_line_width : int, optional
        Inserts newlines if text is longer than `max_line_width`.

set_printoptions (linewidth=None)
        linewidth : int, optional
        The number of characters per line for the purpose of inserting
        line breaks (default 75).    

  
import cv2, os, sys, numpy as np
os.chdir("D:/ ")
img1 = cv2.imread("line-drawing.jpg", cv2.IMREAD_REDUCED_GRAYSCALE_8)
img = 255-img1
np.set_printoptions(threshold=sys.maxsize)
for i in img:
    print(np.array2string(i)   '\n')

    x = len(i)
    i.reshape(x,1)
    print(np.array2string(i))
    1/0 #stop loop

Edit:

Clarifying the question: How is 37 vals per line limit removed? Goal is for the array to visually resemble the image, as text

CodePudding user response:

If you need to visualize a grayscale 0-255 image as text then you can try np.array2string function: https://numpy.org/doc/stable/reference/generated/numpy.array2string.html

You may need to specify threshold, max_line_width, seperator and formatter parameters.

Example:

import cv2
import numpy as np

image = np.zeros((40, 40), np.uint8)
cv2.circle(image, (20, 20), 10, color=100, thickness=-1)
cv2.circle(image, (20, 20), 5, color=255, thickness=-1)

print(np.array2string(image, separator='', threshold=np.inf, max_line_width=np.inf, formatter={'int': lambda x: f'{x:03}'}))

Results:

[[  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0100  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100255100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100100255255255255255255255100100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100255255255255255255255255255100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0100100100100100100255255255255255255255100100100100100100  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100255100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0100100100100100100100100100  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0100  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
 [  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]]

or binary image (without formatter argument):

[[0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000010000000000000000000]
 [0000000000000000111111111000000000000000]
 [0000000000000011111111111110000000000000]
 [0000000000000111111111111111000000000000]
 [0000000000001111111111111111100000000000]
 [0000000000001111111111111111100000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000111111111111111111111000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000011111111111111111110000000000]
 [0000000000001111111111111111100000000000]
 [0000000000001111111111111111100000000000]
 [0000000000000111111111111111000000000000]
 [0000000000000011111111111110000000000000]
 [0000000000000000111111111000000000000000]
 [0000000000000000000010000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]
 [0000000000000000000000000000000000000000]]
  • Related