code: def write_packed(self, outfile, rows):
          
  
          self.write_preamble(outfile)
  
          # http://www.w3.org/TR/PNG/#11IDAT
          if self.compression is not None:
              compressor = zlib.compressobj(self.compression)
          else:
              compressor = zlib.compressobj()
  
          # data accumulates bytes to be compressed for the IDAT chunk;
          # it's compressed when sufficiently large.
          data = bytearray()
  
          for i, row in enumerate(rows):
              # Add "None" filter type.
              # Currently, it's essential that this filter type be used
              # for every scanline as
              # we do not mark the first row of a reduced pass image;
              # that means we could accidentally compute
              # the wrong filtered scanline if we used
              # "up", "average", or "paeth" on such a line.
              data.append(0)
              data.extend(row)
              if len(data) > self.chunk_limit:
                  # :todo: bytes() only necessary in Python 2
                  compressed = compressor.compress(bytes(data))
                  if len(compressed):
                      write_chunk(outfile, b'IDAT', compressed)
                  data = bytearray()
  
          compressed = compressor.compress(bytes(data))
          flushed = compressor.flush()
          if len(compressed) or len(flushed):
              write_chunk(outfile, b'IDAT', compressed + flushed)
          # http://www.w3.org/TR/PNG/#11IEND
          write_chunk(outfile, b'IEND')
          return i + 1
docs: Write PNG file to `outfile`.
          `rows` should be an iterator that yields each packed row;
          a packed row being a sequence of packed bytes.
  
          The rows have a filter byte prefixed and
          are then compressed into one or more IDAT chunks.
          They are not processed any further,
          so if bitdepth is other than 1, 2, 4, 8, 16,
          the pixel values should have been scaled
          before passing them to this method.
  
          This method does work for interlaced images but it is best avoided.
          For interlaced images, the rows should be
          presented in the order that they appear in the file.