将PIL B& W图像转换为Numpy阵列时出错

时间:2010-05-03 22:30:41

标签: python numpy python-imaging-library

当我尝试将黑白PIL图像转换为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使其正确转换的特殊内容吗?

3 个答案:

答案 0 :(得分:7)

我相信您在PIL中发现了一个错误! (或者可能是笨拙的,但我会在事情的PIL方面下注......)

@ c上面的回答给出了一个解决方法(使用im.getdata()),虽然我不确定为什么numpy.asarry(image)对他来说是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]
 [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]]

Values in BW PIL image using numpy.asarray() <-- Unexpected!
   (Same occurs when using numpy.array() to copy and convert.)
[[0 0 0 0 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]
 [0 1 1 1 1 1 1 0 0 0 0 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 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 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 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 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]]

答案 1 :(得分: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()

答案 2 :(得分:0)

你的 numpy 版本是什么?我发现将 numpy 从 1.21 降级到 1.20 后,它起作用了。

df<-structure(list(serial = c(11011209, 11011209, 11011210), pnum = c(1, 
2, 2), `diary day` = c(4, 4, 5), work_minutes = c(450, 450, 480
), work_hours = c(8, 8, 0), work_minutes_squared = c(230400, 
230400, 0), cooking_betw = c(0.159090909, 0.371212121, 0.271428571
), dishwas_betw = c(NA, 0.28030303, 0.271428571), houseclean_betw = c(NA_real_, 
NA_real_, NA_real_), laundry_betw = c(NA_real_, NA_real_, NA_real_
), ironing_betw = c(NA_real_, NA_real_, NA_real_), tv1_betw = c(0.227272727, 
0.28030303, 0.088095238), tv2_betw = c(NA_real_, NA_real_, NA_real_
), tv3_betw = c(NA_real_, NA_real_, NA_real_), tv4_betw = c(0.242424242, 
0.242424242, NA), tv5_betw = c(NA_real_, NA_real_, NA_real_), 
    tv6_betw = c(NA, NA, NA), tv7_betw = c(NA_real_, NA_real_, 
    NA_real_), employment = structure(c(1L, 2L, 2L), .Label = c("FT", 
    "PT"), class = "factor"), cooking_timedep = c(2.773774781, 
    2.773774781, 3.417989523), dishwas_timedep = c(1.99418589, 
    1.99418589, 2.625000906), houseclean_timedep = c(5.327815177, 
    5.327815177, 4.750929283), laundry_timedep = c(0.964285364, 
    0.964285364, 3.126314747), ironing_timedep = c(2.559321793, 
    2.559321793, 2.292682967), TV_timedep = c(5.086074482, 5.086074482, 
    5.012631788), TV_dur = c(70, 240, 60), Dishwash_dur = c(0, 
    20, 10), Foodprep_dur = c(80, 150, 10), Cleaningdwelling_dur = c(0, 
    0, 10), Ironing_dur = c(0, 0, 0), Laundry_dur = c(0, 0, 0
    ), WorkType = c("RE", "RE", "RE"), DMSex = c(1, 2, 1), DVAge = c(69, 
    60, 36), DVHsize = c(2, 2, 4), Income = c(1500, 1500, 3500
    ), NumChild = c(0, 0, 2)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"))