簡體   English   中英

PIL Image 如何保存具有非整數和非正值的 NumPy 數組?

[英]How does PIL Image save NumPy arrays with non-integer and non-positive values?

我有一個大小為 28 x 280 的 NumPy 數組,其中包含實數值(正值和負值)。 我正在使用以下代碼通過 PIL 圖像將此數組保存到文件中-

img = Image.fromarray(img)
img.save(save_path, "PNG")

現在,當我使用以下代碼使用 PIL 加載此保存的圖像時 -

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

問題 -

  1. 在這里,據我了解,PIL 隱式將實數轉換為uint8格式(0 到 255 像素值),但我想知道實數到uint8的轉換究竟是如何發生的? 實際像素值是否四舍五入或截斷為最接近的整數,如果是,負像素值會發生什么?
  2. 此外,當我嘗試通過簡單地打開 PIL 圖像來可視化它時,它只會顯示一個黑屏,就像這樣 - 在此處輸入圖像描述

但是,奇怪的是,當我將 np 數組乘以 255 時,就像這樣 - img = img * 255 ,然后保存它,它會顯示一些值,就像這樣 -

在此處輸入圖像描述

只是最初的像素值太輕而無法被我的眼睛感知嗎? 我想是的,但我只是想確認一下。

如果要將負數和浮點數據保存為圖像,您可能應該使用TIFF格式。

PNG最多只能存儲 16 位/通道的無符號整數數據,即范圍 0..65535。


這是在 TIFF 中保存正負浮點數然后檢索它們的演示:

import numpy as np
from PIL import Image

# Set height and width
h, w = 5, 4

# Create image from Numpy array of float32 and save as TIFF
naA = np.linspace(-1000, 1000, h*w, dtype=np.float32).reshape((h,w))
Image.fromarray(naA).save('floats.tif')

# Read back image and compare
imB = Image.open('floats.tif')
naB = np.array(imB)

現在打印兩者並檢查相同:

In [101]: naA
Out[101]: 
array([[-1000.     ,  -894.7368 ,  -789.4737 ,  -684.2105 ],
       [ -578.9474 ,  -473.6842 ,  -368.42105,  -263.1579 ],
       [ -157.89473,   -52.63158,    52.63158,   157.89473],
       [  263.1579 ,   368.42105,   473.6842 ,   578.9474 ],
       [  684.2105 ,   789.4737 ,   894.7368 ,  1000.     ]],
      dtype=float32)

In [102]: naB
Out[102]: 
array([[-1000.     ,  -894.7368 ,  -789.4737 ,  -684.2105 ],
       [ -578.9474 ,  -473.6842 ,  -368.42105,  -263.1579 ],
       [ -157.89473,   -52.63158,    52.63158,   157.89473],
       [  263.1579 ,   368.42105,   473.6842 ,   578.9474 ],
       [  684.2105 ,   789.4737 ,   894.7368 ,  1000.     ]],
      dtype=float32)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM