[英]convert float image array to int in PIL via Image.fromarray
我有一個形狀為3,256,256
的 PyTorch 張量,其中 3 是通道數,圖像尺寸為 256,所有浮點值。
我正在嘗試將其輸入網絡並使用 PIL 進行一些轉換。 為此,我這樣做:
img = Image.fromarray((255*imgs[i]).numpy().astype(np.uint8))
但我得到:
TypeError: Cannot handle this data type: (1, 1, 256), |u1
當我檢查(255*imgs[i]).numpy().astype(np.uint8)
的 output 時,我確實看到了,例如:
[[[ 62 57 59 ... 63 46 36]
[ 72 71 67 ... 80 76 82]
[ 58 63 63 ... 145 152 169]
...
[238 240 243 ... 7 7 7]
[241 239 240 ... 5 5 6]
[241 243 242 ... 4 3 5]]
[[ 83 78 80 ... 86 70 61]
[ 91 90 85 ... 95 93 98]
[ 80 83 80 ... 141 150 168]
...
[176 178 181 ... 14 14 14]
[177 176 178 ... 15 15 17]
[179 180 180 ... 13 13 15]]
[[147 141 143 ... 150 136 128]
[147 149 148 ... 154 149 154]
[141 149 148 ... 178 182 196]
...
[129 131 134 ... 43 43 43]
[130 130 131 ... 45 45 47]
[133 134 133 ... 44 44 46]]]
從長遠來看,我不是圖像專家,我現在正在努力解決這個問題。
您需要匹配維度的順序,而不僅僅是dtype
。 PIL.Image
期望其 RGB 圖像的形狀為h
x w
x3 - 通道尺寸最后,而pytorch更喜歡將圖像表示為 3x h
x w
- 通道尺寸首先。
因此你需要:
img = Image.fromarray((255*imgs[i]).numpy().astype(np.uint8).transpose(1, 2, 0))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.