2010-05-03 60 views
3

當我嘗試將黑白PIL圖像轉換爲numpy數組時,出現奇怪的錯誤。下面是我正在使用的代碼示例。將PIL B&W圖像轉換爲Numpy陣列時出錯

if image.mode != '1': 
     image = image.convert('1') #convert to B&W 
    data = np.array(image) #Have also tried np.asarray(image) 
    n_lines = data.shape[0] #number of raster passes 
    line_range = range(data.shape[1]) 
    for l in range(n_lines): 
     # process one horizontal line of the image 
     line = data[l] 
     for n in line_range: 
      if line[n] == 1: 
       write_line_to(xl, z+scale*n, speed) #conversion to other program code 
      elif line[n] == 0: 
       run_to(xl, z+scale*n) #conversion to other program code 

我已經嘗試過使用數組和asarray進行轉換,並且得到了不同的錯誤。如果我使用數組,那麼我得到的數據與我放入的數據不同。它看起來像是幾個非常縮小的局部圖像並排放置,其餘圖像空間填充爲黑色。如果我使用asarray,那麼在柵格步驟期間(在隨機線上)整個python崩潰。如果我使用灰度圖像('L'),那麼無論是陣列還是asarray都不會出現這些錯誤。

有誰知道我在做什麼錯? PIL編碼B & W圖像的方式有些奇怪,或者需要通過numpy才能正確轉換的東西?

+0

子公司的問題是我們剛剛發現,轉換似乎在抖動,我們希望有實線。 – Elliot 2010-05-04 14:35:45

+0

在這種情況下(抖動不可取),將圖像保持爲灰度(模式'L'/ dtype np.uint8)並使用numpy閾值可能最有意義。例如。 「data = np.array(im); data = data> 127」對於灰度圖像應該可以很好地工作,並且完全避免抖動。你也可以在PIL中設定閾值,但是如果你轉換爲numpy數組,那麼使用numpy可以更容易。無論如何,只是我的想法...祝你好運! – 2010-05-04 19:03:22

回答

7

我相信你已經找到了PIL的錯誤! (或可能在numpy,但我打賭它是在PIL方面的東西...)

@ c的答案上面給出了一個解決方法(使用im.getdata()),但我不知道爲什麼numpy .asarry(圖像)是segfaulting爲他...(舊版本的PIL和/或numpy,也許?)它適用於我,但在1位PIL圖像上產生亂碼(並且適用於其他任何事情,我經常使用它!)。

另一個解決方法是在轉換爲numpy數組之前將BW圖像轉換回灰度(模式'L')。

如果速度很重要,在轉換爲numpy數組之前將BW圖像轉換回灰度似乎更快。

In [35]: %timeit np.array(im_bw.convert('L')).astype(np.uint8) 
10000 loops, best of 3: 28 us per loop 

In [36]: %timeit np.reshape(im_bw.getdata(), im_bw.size) 
10000 loops, best of 3: 57.3 us per loop 

在一個單獨的音符,如果修改就地數組內容,一定要使用numpy.array而不是numpy.asarray,因爲後者將創建由PIL圖像實例陣列時不復制內存,從而返回一個只讀數組。僅僅提到這一點,因爲我使用asarray()以下...

下面是這印證了錯誤一個獨立的例子...

import numpy as np 
import Image 

x = np.arange(256, dtype=np.uint8).reshape((16,16)) 
print 'Created array' 
print x 

im = Image.fromarray(x) 
print 'Vales in grayscale PIL image using numpy.asarray <-- Works as expected' 
print np.asarray(im) 

print 'Converted to BW PIL image...' 
im_bw = im.convert('1') 

print 'Values in BW PIL image, using Image.getdata() <-- Works as expected' 
print ' (Not a simple threshold due to dithering...)' 
# Dividing by 255 to make the comparison easier 
print np.reshape(im_bw.getdata(), (16, 16))/255 

print 'Values in BW PIL image using numpy.asarray() <-- Unexpected!' 
print ' (Same occurs when using numpy.array() to copy and convert.)' 
print np.asarray(im_bw).astype(np.uint8) 

print 'Workaround, convert back to type "L" before array conversion' 
print np.array(im_bw.convert('L')).astype(np.uint8)/255 

,輸出:

Created array 
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[ 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31] 
[ 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47] 
[ 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] 
[ 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79] 
[ 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95] 
[ 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111] 
[112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127] 
[128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143] 
[144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159] 
[160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175] 
[176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191] 
[192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207] 
[208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223] 
[224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239] 
[240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255]] 

Vales in grayscale PIL image using numpy.asarray <-- Works as expected 
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] 
[ 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31] 
[ 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47] 
[ 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63] 
[ 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79] 
[ 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95] 
[ 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111] 
[112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127] 
[128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143] 
[144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159] 
[160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175] 
[176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191] 
[192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207] 
[208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223] 
[224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239] 
[240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255]] 

Converted to BW PIL image... 

Values in BW PIL image, using Image.getdata() <-- Works as expected 
    (Not a simple threshold due to dithering...) 
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0] 
[0 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0] 
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1 1 1 1 1]] 

Workaround, convert back to type "L" before array conversion 
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0] 
[0 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0] 
[0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 1] 
[0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0] 
[1 0 1 0 1 0 1 0 1 0 0 0 1 1 0 1] 
[0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 0] 
[1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1] 
[0 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1] 
[1 1 0 1 1 1 1 0 1 1 0 1 1 0 1 1] 
[1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0] 
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 
[1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1] 
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1] 
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]] 
+0

我不認爲這個bug已經消失。我仍然得到'print(np.array(Image。新的('1',(4,2),color = 1)))' - >'[[True True False False] [False False False False]]'在我的樹莓派上Image.VERSION 1.1。 7和'np .__ version__' 1.13.3。但有趣的是,在Mac OS X上,我得到了正確的結果:'[[True True True True] [True True True True]]與兩個版本相同。 – Bill 2017-11-05 04:42:43

3

不知道這行:

data = numpy.array(image) 

事實上,這給了我一個段錯誤。但我只是嘗試以下,它工作正常:

import numpy 
import Image 

im = Image.open("some_photo.jpg") 
im = im.convert("1") 

pixels = im.getdata() # returns 1D list of pixels 
n = len(pixels) 
data = numpy.reshape(pixels, im.size) # turn into 2D numpy array 

for row in data: 
    # do your processing 
    pass 

# Check that the numpy array's data is good 
im2 = Image.new("1", im.size) 
im2.putdata(numpy.reshape(data, [n, 1])) 
im2.show() 
+0

黑白轉換有些奇怪。它沒有正確註冊邊緣。所以不是一個標誌,我正在變成一個大圈子。 – Elliot 2010-05-04 15:37:07