[02:39:25] <cats> hey all, I'm having some trouble working with bilevel pngs with pillow, can anyone help me out real quick?
[15:49:46] <wiredfool1> hi cats -- what seems to be the problem?
[15:58:55] <cats> wiredfool: I'm getting different array representations when I open an image from a filename and when I open from a buffer
[16:00:49] <cats> on top of that, it seems that writing pngs back to disk with mode '1' is broken sometimes? It appears that instead of treating each value in the array as a bit value, it's considered as an entire byte - leading to 7 black pixels + 1 black/white pixel depending on the array value
[16:32:35] <wiredfool> internally, everything is stored as bytes, not bits
[16:32:48] <wiredfool> so if you're doing bit twiddling, that's going to throw you off.
[16:44:48] <cats> I can't share the image I'm testing on directly since it contains sensitive info, but you can generate a sample image from a pdf as I did using ghostscript: gs -sDEVICE=pnggray -sOutputFile=OUTPUTPNG -dFirstPage=1 -dLastPage=1 -dSAFER -dQUIET -dBATCH -dNOPAUSE -r300 INPUTPDF
[16:45:17] <cats> and I would replace the -sDEVICE parameter with pngmono
[17:00:49] <cats> yup, both asserts pass and also argwhere.shape is (0, 1)
[17:01:16] <cats> well I guess I can proceed this way and simply mark the actual mode of the image in another attribute
[17:02:11] <cats> more importantly, how deep is this bug? I'm wondering what the relevant parts of Image.py need modification
[17:02:45] <wiredfool> so, I've looked at this, and it's been a bug that I've run into
[17:03:04] <wiredfool> essentially, 1 bit and L images are the same in Pillow, they're both byte oriented.
[17:03:46] <wiredfool> to get the 1 bit images into numpy as a 1 bit array, the image needs to be packed into the smaller format, and hten handed off to numpy in image_toarray
[17:04:12] <wiredfool> and the mode for numpy needs to be handled correctly
[17:05:31] <cats> when you say '1' and 'L' are treated the same, do I see this behavior when I try to convert an array obtained from a '1' image to np.uint8?
[17:05:37] <wiredfool> sorry, it's not toarrya, it's the __getattr__ "__array_interface__ oart
[17:05:47] <cats> like np.asarray(mono_img).astype(np.uint8)
[17:06:05] <cats> because I see that I don't get just 0 and 255s, in fact there's the entire range in between
[17:07:04] <wiredfool> good question. that's .... I wonder if this has changed when I wasn't looking
[17:07:11] <cats> so I'm guessing those uint8 values are the bytes representing 8 pixels
[17:10:24] <cats> well wiredfool, this was very helpful, thanks for your time
[17:11:08] <wiredfool> cats: sorry I didn't help further
[17:27:22] <wiredfool> so, we have to do path twiddling to get the installed PIl
[17:28:55] <cats> wiredfool: quick update, conversion to 'L' and then back to '1' avoids the corrupted output png, so this will be a decent patch for now