I’m new to image processing with Python and met a strange problem.
For example I have a 2*2 black-white bitmap image which pixels are following:
black white
white black
Use PIL and convert it to numpy:
>>> import Image
>>> import numpy
>>> im = Image.open('test.bmp')
>>> im
<BmpImagePlugin.BmpImageFile image mode=1 size=2x2 at 0x95A54EC>
>>> numpy.asarray(im)
array([[ True, True],
[False, False]], dtype=bool)
>>>
What puzzles me is the order of pixel in the array. Why not [[True, False], [False, True]]? Thanks.
UPDATE: the bitmap is here:
http://njuer.us/clippit/test.bmp
It looks like there are some bugs with converting to/from numpy with the
1mode.If you convert it to
Lfirst, it works fine:Also, if you try and convert a
boolnumpy array to PIL, you get strange results:However, the exact same thing with
uint8works fine:So I would suggest using
Las an intermediate datatype and then converting to1before saving if you need to save it in that format. Something like this:Then to convert back: