{"action":"created","issue":{"url":"https://api.github.com/repos/python-pillow/Pillow/issues/1228","labels_url":"https://api.github.com/repos/python-pillow/Pillow/issues/1228/labels{/name}","comments_url":"https://api.github.com/repos/python-pillow/Pillow/issues/1228/comments","events_url":"https://api.github.com/repos/python-pillow/Pillow/issues/1228/events","html_url":"https://github.com/python-pillow/Pillow/issues/1228","id":75383346,"number":1228,"title":"Array interface for 1-bit images seems wrong.","user":{"login":"LimEJET","id":4407285,"avatar_url":"https://avatars.githubusercontent.com/u/4407285?v=3","gravatar_id":"","url":"https://api.github.com/users/LimEJET","html_url":"https://github.com/LimEJET","followers_url":"https://api.github.com/users/LimEJET/followers","following_url":"https://api.github.com/users/LimEJET/following{/other_user}","gists_url":"https://api.github.com/users/LimEJET/gists{/gist_id}","starred_url":"https://api.github.com/users/LimEJET/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LimEJET/subscriptions","organizations_url":"https://api.github.com/users/LimEJET/orgs","repos_url":"https://api.github.com/users/LimEJET/repos","events_url":"https://api.github.com/users/LimEJET/events{/privacy}","received_events_url":"https://api.github.com/users/LimEJET/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-11T22:41:12Z","updated_at":"2015-05-12T16:24:24Z","closed_at":null,"body":"Python 3.4.2 Windows 64 bit\r\nNumPy 1.9.2\r\npillow  2.8.1\r\n\r\nConverting a pillow image to 1-bit format and attempting to create a numpy array of it results in a lot of junk data. The numpy guys don't actually support bit-arrays, but the array interface seems to push one out anyway.\r\n\r\nI've tested this with a small image and a large image.\r\n\r\nSmall image (3x3):\r\n![test](https://cloud.githubusercontent.com/assets/4407285/7554999/853ef4c2-f73d-11e4-9747-2e9c79190344.png)\r\n\r\nPillow represents it like this:\r\n```python\r\n>>> i = Image.open(\"test.png\")\r\n>>> for k in i.convert(\"1\").getdata(): print(k)\r\n0\r\n255\r\n0\r\n255\r\n0\r\n255\r\n0\r\n255\r\n0\r\n```\r\nwhile the resulting numpy array varies in how it looks:\r\n```python\r\n>>> p = np.array(i.convert(\"1\"))\r\n>>> p\r\narray([[ True,  True,  True],\r\n       [False,  True,  True],\r\n       [ True,  True,  True]], dtype=bool)\r\n>>> i = Image.open(\"test.png\")\r\n>>> p = np.array(i.convert(\"1\"))\r\n>>> p\r\narray([[ True,  True,  True],\r\n       [False, False, False],\r\n       [False, False,  True]], dtype=bool)\r\n```\r\n\r\nI tried this out on a bigger image (represented below), and I saw some interesting patterns in the output.\r\n\r\nThe code I tested with:\r\n```python\r\ni = Image.open(\"a.png\")\r\ni.show()\r\np = numpy.array(i.convert(\"1\")) #becomes array of booleans\r\nj = Image.fromarray(numpy.uint8(p)*255)\r\nj.show()\r\n```\r\nInput image:\r\n![2015-05-10_17-50-18](https://cloud.githubusercontent.com/assets/4407285/7554994/1031077e-f73d-11e4-9923-f07bf9117c4e.png)\r\n\r\nOutput image:\r\n![2015-05-10_17-48-37](https://cloud.githubusercontent.com/assets/4407285/7554995/121907da-f73d-11e4-8e1f-f574fba57567.png)\r\n\r\nNotice the row of miniaturized versions of the input along the top."},"comment":{"url":"https://api.github.com/repos/python-pillow/Pillow/issues/comments/101338844","html_url":"https://github.com/python-pillow/Pillow/issues/1228#issuecomment-101338844","issue_url":"https://api.github.com/repos/python-pillow/Pillow/issues/1228","id":101338844,"user":{"login":"wiredfool","id":19792,"avatar_url":"https://avatars.githubusercontent.com/u/19792?v=3","gravatar_id":"","url":"https://api.github.com/users/wiredfool","html_url":"https://github.com/wiredfool","followers_url":"https://api.github.com/users/wiredfool/followers","following_url":"https://api.github.com/users/wiredfool/following{/other_user}","gists_url":"https://api.github.com/users/wiredfool/gists{/gist_id}","starred_url":"https://api.github.com/users/wiredfool/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wiredfool/subscriptions","organizations_url":"https://api.github.com/users/wiredfool/orgs","repos_url":"https://api.github.com/users/wiredfool/repos","events_url":"https://api.github.com/users/wiredfool/events{/privacy}","received_events_url":"https://api.github.com/users/wiredfool/received_events","type":"User","site_admin":false},"created_at":"2015-05-12T16:24:24Z","updated_at":"2015-05-12T16:24:24Z","body":"Known issue, see #350. \r\n\r\nI've got this workaround in my code:\r\n```\r\n\t@classmethod\r\n\tdef to_nimage(self, img):\r\n\t\t\"\"\" PIL -> numpy array for scipy work\"\"\"\r\n\t\tnimg = numpy.asarray(img.convert(\"L\"))\r\n\t\tnimg.setflags(True) # writable\r\n\t\treturn nimg\r\n\r\n\t@classmethod\r\n\tdef to_pimage(self, nimg):\r\n\t\t\"\"\" numpy array -> PIL Image \"\"\"\r\n\t\treturn Image.fromarray(numpy.uint8(nimg), mode='L').convert('1')\r\n```"}}