docs: def write_passes(self, outfile, rows, packed=False):
          """
          Write a PNG image to the output file.
  
          Most users are expected to find the :meth:`write` or
          :meth:`write_array` method more convenient.
  
          The rows should be given to this method in the order that
          they appear in the output file.  For straightlaced images,
          this is the usual top to bottom ordering, but for interlaced
          images the rows should have already been interlaced before
          passing them to this function.
  
          `rows` should be an iterable that yields each row.  When
          `packed` is ``False`` the rows should be in boxed row flat pixel
          format; when `packed` is ``True`` each row should be a packed
          sequence of bytes.
          """
          # Try to optimize
          if not packed:
              if self.greyscale == 'try':
                  rows1, rows2 = tee(rows)
                  greyrows = try_greyscale(rows1, self.alpha)
                  if greyrows is not False:
                      rows = greyrows
                      self.greyscale = True
                      self.planes -= 2
                  else:
                      self.greyscale = False
                      rows = rows2
  
          if not self.palette:
              # No palette, check for rescale
              targetbitdepth = None
              srcbitdepth = self.bitdepth
              if self.alpha or not self.greyscale:
                  if self.bitdepth not in (8, 16):
                      targetbitdepth = (8, 16)[self.bitdepth > 8]
              else:
                  assert self.greyscale
                  assert not self.alpha
                  if self.bitdepth not in (1, 2, 4, 8, 16):
                      if self.bitdepth > 8:
                          targetbitdepth = 16
                      elif self.bitdepth == 3:
                          targetbitdepth = 4
                      else:
                          assert self.bitdepth in (5, 6, 7)
                          targetbitdepth = 8
  
              if targetbitdepth:
                  if packed:
                      raise Error("writing packed pixels not suitable for"
                                  " bit depth %d" % self.bitdepth)
                  self.bitdepth = targetbitdepth
                  factor = \
                      float(2**targetbitdepth - 1) / float(2**srcbitdepth - 1)
  
                  def scalerow(inrows):
                      """Rescale all pixels"""
                      for row in inrows:
                          yield [int(round(factor * x)) for x in row]
  
                  rows = scalerow(rows)
  
          self.write_idat(outfile, self.comp_idat(self.idat(rows, packed)))
          return self.irows