Take the path of the image and then read the binary before decoding it. Store a list of tuples, in form of [[[r, g, b, alpha], [...], ...], [...], ...] in self.pixels
(self, image_path)
| 122 | |
| 123 | |
| 124 | def decode(self, image_path) -> None: |
| 125 | """ |
| 126 | Take the path of the image and then read the binary before decoding it. |
| 127 | Store a list of tuples, in form of |
| 128 | [[[r, g, b, alpha], [...], ...], [...], ...] in self.pixels |
| 129 | """ |
| 130 | # Check whether crc can be carried out if it is demanded. |
| 131 | # I can write python code to do the check but since it may not |
| 132 | # be so important. So ... |
| 133 | with open(image_path, "rb") as image: |
| 134 | self.bin:bytes = image.read() |
| 135 | |
| 136 | # Validation: |
| 137 | # Check whether png HEADER and IHDR chunk exist |
| 138 | if self.bin[:16] != b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR': |
| 139 | raise Exception("This is not a valid png image.") |
| 140 | |
| 141 | # Read the IHDR chunk to get the properties |
| 142 | self.get_image_properties() |
| 143 | |
| 144 | # Get palette if the color type is 3 |
| 145 | if self.color_type == 3: |
| 146 | self.get_palette() |
| 147 | elif self.color_type == 0 and self.bit_depth <= 4: |
| 148 | self.palette = Png.grayscale_palette[self.bit_depth] |
| 149 | |
| 150 | # Start to deal with IDAT chuck |
| 151 | bytes_rows = self.get_all_idat_data() |
| 152 | defiltered_bytes_rows = self.defilter(bytes_rows) |
| 153 | self.pixels = self.interpret_bytes_to_color(defiltered_bytes_rows) |
| 154 | |
| 155 | |
| 156 | def get_image_properties(self) -> None: |
no test coverage detected