MCPcopy Create free account
hub / github.com/ICE27182/Python-3D-renderer / decode

Method decode

png.py:124–153  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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:

Callers 1

__init__Method · 0.95

Calls 5

get_image_propertiesMethod · 0.95
get_paletteMethod · 0.95
get_all_idat_dataMethod · 0.95
defilterMethod · 0.95

Tested by

no test coverage detected