【问题标题】:why is every pixel of an image are equal to 255?为什么图像的每个像素都等于 255?
【发布时间】:2022-01-22 18:46:31
【问题描述】:

我正在试验 PIL 并尝试分析我附加的图像。 由于我的目标是最终能够通过神经网络识别它, 我希望所有像素都具有不同的强度,因此具有不同的值 范围从 0 到 255。我不知道为什么,这个图像的每个像素都等于 255.怎么会?我到底做错了什么?

import numpy as np
import pandas as pd
import PIL
from PIL import Image

img = Image.open(r'key_1.jpg')
print(img.format)
print(img.size)
print(img.mode)
img # displays the image

img_sequence = img.getdata()
img_array = np.array(img_sequence)
print((img_array)) # all pixels = 255

【问题讨论】:

  • 你只能看到第一行和最后几行的像素都是白色的(255)。
  • 我不确定你的意思。我打印了数组的长度,等于 160000,这意味着所有行都包括在内,不是吗?
  • 试试print(np.min(img_array))

标签: python image numpy python-imaging-library pixel


【解决方案1】:

实际上,它们不是。您只能在显示 255 的位置看到顶部和最后几行,因为那里的像素是白色的。如果您尝试使用 PIL/OpenCV 读取相同的数组,结果将正确显示。

但是,使用这种方法可以看到 NumPy 数组的所有结果-

img_sequence = img.getdata()
img_array = np.array(img_sequence)

np.set_printoptions(threshold=np.inf)
print(img_array)

我为同一张图片获得的输出的随机部分的示例屏幕截图 -

【讨论】:

  • 使用您的解决方案,我可以看到所有像素值,但是,需要很长时间才能全部打印出来。您将如何使用 PIL 和 openCV 打印相同的数组?注释掉这一行: np.set_printoptions(threshold=np.inf) 不会禁用它,因此无论如何都需要一段时间。如何禁用此行?
  • 你不再需要这条线了。不 ?这只是为了解释。在现实生活场景中,您不需要查看整个数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
相关资源
最近更新 更多