PIS图像如何使用非直觉和非阳性值保存Numpy阵列?
我有一个尺寸28 x 280的数量阵列,其中包含实际的数字值(均为正值和负值)。我正在使用以下代码来保存此数组以通过pil图像归档 -
img = Image.fromarray(img)
img.save(save_path, "PNG")
现在,当我使用以下代码加载此保存的图像时 -
img = Image.open(save_path)
img = np.array(img)
print(img[:,:,0]) # since the image is saved in RGB by default, and the channels are simply all the same, I am printing out only one of the channels
print(img.shape) # printing shape of loaded image for sanity check
上面给我以下输出 -
array([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[14, 14, 14],
[ 0, 0, 0],
[14, 14, 14],
[ 0, 0, 0],
[ 2, 2, 2],
[10, 10, 10],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 6, 6, 6],
[ 0, 0, 0],
[19, 19, 19],
[16, 16, 16],
[ 0, 0, 0],
[ 9, 9, 9],
[14, 14, 14],
[ 5, 5, 5],
-- omitting the remaining matrix for spatial reasons
原始矩阵看起来像以下内容,如下所示。帮助 -
1.54009545e+00 1.14122391e-01 -5.44282794e-01 -1.66106954e-01]
[-2.70073628e+00 -6.25280142e+00 1.77814519e+00 -8.72797012e+00
9.91206944e-01 6.63580036e+00 6.84081888e+00 -1.18705761e+00
-4.54479456e+00 -5.26672935e+00 4.91975927e+00 -5.48409176e+00
-3.93164325e+00 5.19110155e+00 1.26516495e+01 9.93665600e+00
-5.70824432e+00 5.72582603e-01 -4.31831169e+00 -9.31297874e+00
2.13714447e-02 -9.82507896e+00 -2.47176766e+00 -1.94778728e+00
-1.85507727e+00 -8.01630592e+00 -4.42644596e+00 5.74180269e+00]
[ 3.32923412e+00 1.50732050e+01 -1.01800518e+01 1.85193479e-01
-1.77801073e+00 -4.91134501e+00 -4.94232035e+00 5.52533197e+00
-3.84771490e+00 -5.61370182e+00 -2.91945863e+00 -9.53506768e-01
7.03971624e-01 1.26758552e+00 -1.29794350e+01 -1.08105397e+00
-5.57984650e-01 -1.50801647e+00 -3.45247960e+00 -6.14299655e-01
-4.83907032e+00 5.44770575e+00 2.50088573e+00 -2.45785332e+00
-3.94766003e-01 7.80810177e-01 -1.66951954e+00 -5.23118067e+00]
[ 1.24226892e+00 -4.30912447e+00 1.14384556e+00 -5.38896322e+00
-5.95073175e+00 5.03882837e+00 4.15563917e+00 -7.99412632e+00
-1.68129158e+00 -2.23124218e+00 2.24080634e+00 -5.57195246e-01
-2.29391623e+00 -2.70431495e+00 9.87635612e+00 -2.90223390e-01
3.25407982e+00 3.67051101e+00 -2.86848998e+00 -4.53229618e+00
-3.80941963e+00 3.66697168e+00 3.98574305e+00 -1.50027335e-01
-8.77485275e+00 2.20300531e+00 4.97666216e+00 2.27730870e+00]]
-- again, omitting large chunks for spatial reasons
问题 -
- 在这里,从我的理解中,PIL隐含将真实物转换为
uint8
格式(0到255像素值),但是我想知道如何确切地将reals转换为uint88
发生?真实的像素值是否将其四舍五入或截断为最接近整数,如果是的,负像素值会发生什么? - 另外,当我尝试通过简单地打开图像来可视化pil映像时,它只是向我展示了一个黑屏,就像这样 -
但是,奇怪的是,当我将NP数组乘以255时,就像So -so -IMG = IMG * 255
,然后保存它,它显示了一些值,就像这样-So -
只是像素值最初太轻而无法被我的眼睛感知吗?我认为是这样,但我只想确认。
I have a NumPy array of size 28 x 280, which contains real number values (both positive and negative values). I am using the following code to save this array to file through a PIL Image -
img = Image.fromarray(img)
img.save(save_path, "PNG")
Now, when I load this saved image using PIL using the following code -
img = Image.open(save_path)
img = np.array(img)
print(img[:,:,0]) # since the image is saved in RGB by default, and the channels are simply all the same, I am printing out only one of the channels
print(img.shape) # printing shape of loaded image for sanity check
The above gives me the following output -
array([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[14, 14, 14],
[ 0, 0, 0],
[14, 14, 14],
[ 0, 0, 0],
[ 2, 2, 2],
[10, 10, 10],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 6, 6, 6],
[ 0, 0, 0],
[19, 19, 19],
[16, 16, 16],
[ 0, 0, 0],
[ 9, 9, 9],
[14, 14, 14],
[ 5, 5, 5],
-- omitting the remaining matrix for spatial reasons
The original matrix looks something like the following if that helps -
1.54009545e+00 1.14122391e-01 -5.44282794e-01 -1.66106954e-01]
[-2.70073628e+00 -6.25280142e+00 1.77814519e+00 -8.72797012e+00
9.91206944e-01 6.63580036e+00 6.84081888e+00 -1.18705761e+00
-4.54479456e+00 -5.26672935e+00 4.91975927e+00 -5.48409176e+00
-3.93164325e+00 5.19110155e+00 1.26516495e+01 9.93665600e+00
-5.70824432e+00 5.72582603e-01 -4.31831169e+00 -9.31297874e+00
2.13714447e-02 -9.82507896e+00 -2.47176766e+00 -1.94778728e+00
-1.85507727e+00 -8.01630592e+00 -4.42644596e+00 5.74180269e+00]
[ 3.32923412e+00 1.50732050e+01 -1.01800518e+01 1.85193479e-01
-1.77801073e+00 -4.91134501e+00 -4.94232035e+00 5.52533197e+00
-3.84771490e+00 -5.61370182e+00 -2.91945863e+00 -9.53506768e-01
7.03971624e-01 1.26758552e+00 -1.29794350e+01 -1.08105397e+00
-5.57984650e-01 -1.50801647e+00 -3.45247960e+00 -6.14299655e-01
-4.83907032e+00 5.44770575e+00 2.50088573e+00 -2.45785332e+00
-3.94766003e-01 7.80810177e-01 -1.66951954e+00 -5.23118067e+00]
[ 1.24226892e+00 -4.30912447e+00 1.14384556e+00 -5.38896322e+00
-5.95073175e+00 5.03882837e+00 4.15563917e+00 -7.99412632e+00
-1.68129158e+00 -2.23124218e+00 2.24080634e+00 -5.57195246e-01
-2.29391623e+00 -2.70431495e+00 9.87635612e+00 -2.90223390e-01
3.25407982e+00 3.67051101e+00 -2.86848998e+00 -4.53229618e+00
-3.80941963e+00 3.66697168e+00 3.98574305e+00 -1.50027335e-01
-8.77485275e+00 2.20300531e+00 4.97666216e+00 2.27730870e+00]]
-- again, omitting large chunks for spatial reasons
Question -
- Here, from what I understand, PIL is implicitly converting the reals to
uint8
format (0 to 255 pixel values), but I want to know how exactly conversion of reals touint8
takes place? Are the real pixel values rounded off or truncated to the closest integer, if so, what happens to the negative pixel values? - Also, when I try to visualize the PIL image by simply opening it, it just shows me a black screen, like so -
But, the weird thing is that, when I multiply the np array by 255, like so - img = img * 255
, and then save it, it shows some values, like so -
Is it just that the pixel values initially are too light to be perceived by my eyes? I think so, but I just want to confirm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要将负面和浮点数据作为图像保存,则可能应使用 tiff 格式。
png 只能以最多16位/通道存储未签名的整数数据,即范围为0..65535。
这是在TIFF中保存正浮点数和负面浮点数,然后检索它们的演示:
现在打印并检查相同:
If you want to save negative and floating point data as an image, you should probably use TIFF format.
PNG is only able to store unsigned integer data at up to 16-bit/channel, i.e. in range 0..65535.
Here is a demonstration of saving positive and negative floating point numbers in a TIFF and then retrieving them:
Now print both and check same: