我相信你已经找到了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 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 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 1]
[1 1 1 1 1 1 1 1 1 1 1 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]]
子公司的问题是我们刚刚发现,转换似乎在抖动,我们希望有实线。 – Elliot 2010-05-04 14:35:45
在这种情况下(抖动不可取),将图像保持为灰度(模式'L'/ dtype np.uint8)并使用numpy阈值可能最有意义。例如。 “data = np.array(im); data = data> 127”对于灰度图像应该可以很好地工作,并且完全避免抖动。你也可以在PIL中设定阈值,但是如果你转换为numpy数组,那么使用numpy可以更容易。无论如何,只是我的想法...祝你好运! – 2010-05-04 19:03:22