I already referred to 'How to print the full NumPy array without wrapping (in Jupyter Notebook)'.
However, my array size is 256x256.
Therefore, it is automatically wrapping in jupyter as below.
My array is based on image and only consisting of 0 and 1.
I just want to see my image shape with 0 and 1 such as below.
I also tried below code.
np.set_printoptions(threshold = np.inf)
np.set_printoptions(linewidth = np.inf)
Reference for that post is here.
Two problems seem to mix here:
The line breaks introduced by Numpy:
You can fix these, as already commented and as you already tried, by
import numpy as np
np.set_printoptions(linewidth=np.inf)
The line breaks (or rather, line wraps) introduced by Jupyter:
You can fix these, following this answer, by
from IPython.display import display, HTML
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
Together, we have
import numpy as np
from IPython.display import display, HTML
# Avoid line breaks by Jupyter (following https://stackoverflow.com/a/70433850/7395592)
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
# Avoid premature line breaks by Numpy and show all array entries
np.set_printoptions(linewidth=np.inf, threshold=np.inf)
image = np.zeros((256, 256), dtype=np.uint8)
image
This, for me, produces the following output:
Alternatively, as you wrote that your image consists of 0
s and 1
s only (and thus exclusively of single-digit numbers): you can condense the output even more, by suppressing the commas and spaces, for example like so:
import numpy as np
from IPython.display import display, HTML
# Avoid line breaks by Jupyter (following https://stackoverflow.com/a/70433850/7395592)
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
image = np.zeros((256, 256), dtype=np.uint8)
image_vals = "\n".join(f"[{''.join(str(val) for val in row)}]" for row in image)
print(image_vals)
Which produces the following output (note that I had to use print()
in this case):
The only inconvenience that I could find in both cases is that a horizontal scrollbar only appears in the output cell if one scrolls to its very bottom (I tried with Firefox and Chrome).