#!/usr/bin/env python3
# -*- coding: utf-8 -*-


class PNGWrongHeaderError(Exception):
    """Exception, that says the input file is probably not PNG file."""
    pass


class PNGNotImplementedError(Exception):
    """Exception, that says the PNG image has a structure we cannot process."""
    pass


class PngReader():
    """Class for reading PNG images."""
    
    def __init__(self, filepath):
        
        # RGB-data from image as a list of rows,
        #   each row contains pixels - triplets of color values
        self.rgb = []
        
        # save the data from the file
        self.binary = open(filepath, mode='rb').read()
        
        # check, if it is PNG
        if (self.binary[0:8] != b'\x89PNG\r\n\x1a\n'):
            raise PNGWrongHeaderError()


#
# just for testing
#
if __name__ == '__main__':
    PngReader('test_data/HelloWorld.png')

@@ -660,29 +660,130 @@
     
-# check, if it is PNG
+self._check_png()._parse_png()%0A            %0A    def _check_png(self):%0A        %22%22%22Checks, if it is PNG, strip theheader.%22%22%22
 %0A   
@@ -807,9 +807,8 @@
 ary%5B
-0
 :8%5D 
@@ -873,16 +873,655 @@
 Error()%0A
+            self.binary = self.binary%5B8:%5D%0A        return self%0A    %0A    def _bytes_to_num(self,bytes):%0A        n = 0%0A        for b in bytes:%0A            n = n*256 + b%0A        return n%0A    %0A    def _parse_png(self):%0A        p = 0%0A        self.data = %5B%5D%0A        while p %3C len(self.binary):%0A            l = self._bytes_to_num(self.binary%5Bp:p+4%5D)%0A            p += 4%0A            %0A            self.data += %5B%7B'head':self.binary%5Bp:p+4%5D,%0A                           'length':l,%0A                           'data':self.binary%5Bp+4:p+l+4%5D,%0A                           'crc':self.binary%5Bp+l+4:p+l+8%5D%7D%5D%0A            %0A            p += l+8%0A        return self%0A
 %0A%0A#%0A# ju

Parsing PNG, separated to methods