Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like there are some bugs with converting to/from numpy with the <code>1</code> mode.</p> <p>If you convert it to <code>L</code> first, it works fine:</p> <pre><code>&gt;&gt;&gt; im &lt;BmpImagePlugin.BmpImageFile image mode=1 size=2x2 at 0x17F17E8&gt; &gt;&gt;&gt; im2 = im.convert('L') &gt;&gt;&gt; numpy.asarray(im2) array([[ 0, 255], [255, 0]], dtype=uint8) </code></pre> <p>Also, if you try and convert a <code>bool</code> numpy array to PIL, you get strange results:</p> <pre><code>&gt;&gt;&gt; testarr = numpy.array([[True,False],[True,False]], dtype=numpy.bool) &gt;&gt;&gt; testpil = Image.fromarray(testarr, mode='1') &gt;&gt;&gt; numpy.asarray(testpil) array([[False, False], [False, False]], dtype=bool) </code></pre> <p>However, the exact same thing with <code>uint8</code> works fine:</p> <pre><code>&gt;&gt;&gt; testarr = numpy.array([[255,0],[0,255]], dtype=numpy.uint8) &gt;&gt;&gt; Image.fromarray(testarr) &lt;Image.Image image mode=L size=2x2 at 0x1B51320&gt; &gt;&gt;&gt; numpy.asarray(Image.fromarray(testarr)) array([[255, 0], [ 0, 255]], dtype=uint8) </code></pre> <p>So I would suggest using <code>L</code> as an intermediate datatype and then converting to <code>1</code> before saving if you need to save it in that format. Something like this:</p> <pre><code>&gt;&gt;&gt; im &lt;BmpImagePlugin.BmpImageFile image mode=1 size=2x2 at 0x17F17E8&gt; &gt;&gt;&gt; im2 = im.convert('L') &gt;&gt;&gt; arr = numpy.asarray(im2) &gt;&gt;&gt; arr array([[ 0, 255], [255, 0]], dtype=uint8) &gt;&gt;&gt; arr = arr == 255 &gt;&gt;&gt; arr array([[False, True], [ True, False]], dtype=bool) </code></pre> <p>Then to convert back:</p> <pre><code>&gt;&gt;&gt; backarr = numpy.zeros(arr.shape, dtype=numpy.uint8) &gt;&gt;&gt; backarr[arr] = 255 &gt;&gt;&gt; backarr array([[ 0, 255], [255, 0]], dtype=uint8) &gt;&gt;&gt; Image.fromarray(backarr).convert('1') &lt;Image.Image image mode=1 size=2x2 at 0x1B41CB0&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload