Here is my code snippets. It prints the means and the standard deviations from the image pixels.
from numpy import asarray
from PIL import Image
import os
os.chdir("../images")
image = Image.open("dubai_2020.jpg")
pixels = asarray(image)
pixels = pixels.astype("float32")
means, stds = pixels.mean(axis=(0, 1), dtype="float64"), pixels.std(
axis=(0, 1), dtype="float64")
print(f"Means: {means:%.2f}, Stds: {stds:%.2f} ")
And the output is
File "pil_local_standard5.py", line 15, in <module>
print(f"Means: {means:%.2f, %.2f, %.2f}, Stds: {stds:%.2f, %.2f, %.2f} ")
TypeError: unsupported format string passed to numpy.ndarray.__format__
How do I define the f-strings format of the data in this case?
numpy
uses its own formatting specifications. The Python ones, whether '%', 'str.format' or 'f' don't work within an array.f{x!s}
andf{x!r}
work, but not much else. Oh, and '%.2f' isn't right. Use thestr.format
style, e.g.f'{12.23:.2f}'
– Mikes