title: Flutter / Dart : convert image to 1 bit black and white
body: <p>I'm writing a code to print an image using ESC * command (using an ESC POS thermal receipts printer).</p>
  
  <p>Basically, I'm trying to adapt Python Algorithm for Dart/Flutter. It sounds easy : open image -> grayscale -> invert colors -> convert to b&amp;w 1 bit:</p>
  
  <pre class="lang-py prettyprint-override"><code>im = Image.open(filename)
  im = im.convert("L")  # Invert: Only works on 'L' images
  im = ImageOps.invert(im)  # Bits are sent with 0 = white, 1 = black in ESC/POS
  
  print(len(im.tobytes())) # len = 576 (image size: 24*24)
  im = im.convert("1")  # Pure black and white
  print(len(im.tobytes())) # leng = 72 (image size: 24*24)
  ...
  </code></pre>
  
  <p>I only have a problem with the last step (1 bit conversion).</p>
  
  <p>As you can see, Python code (Pillow library) will decrease the number of bytes after im.convert("1") command and it's exactly what I need to correctly generate a ESC/POS command. Each value is between 0 and 255.</p>
  
  <p>How to achieve it using Dart?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import 'package:image/image.dart';
  
  const String filename = './test_24x24.png';
  final Image image = decodeImage(File(filename).readAsBytesSync());
  
  grayscale(image);
  invert(image);
  
  </code></pre>
  
  <p>Source image: 24px * 24px</p>
  
  <p>At the end I have a grey/inverted image containing (24 * 24 * 3) bytes in RGB mode. All the r/g/b values are equal because of the greyscaling, so I can keep only one channel which gives me (24 * 24) bytes.</p>
  
  <p>How can I achieve the last step <code>im.convert("1")</code> and keep only 24 * 3 bytes?</p>