aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/contentviews/image/image_parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'mitmproxy/contentviews/image/image_parser.py')
-rw-r--r--mitmproxy/contentviews/image/image_parser.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/mitmproxy/contentviews/image/image_parser.py b/mitmproxy/contentviews/image/image_parser.py
new file mode 100644
index 00000000..0af58a88
--- /dev/null
+++ b/mitmproxy/contentviews/image/image_parser.py
@@ -0,0 +1,30 @@
+import io
+import typing
+
+from kaitaistruct import KaitaiStream
+
+from mitmproxy.contrib.kaitaistruct import png
+
+Metadata = typing.List[typing.Tuple[str, str]]
+
+
+def parse_png(data: bytes) -> Metadata:
+ img = png.Png(KaitaiStream(io.BytesIO(data)))
+ parts = [
+ ('Format', 'Portable network graphics')
+ ]
+ parts.append(('Size', "{0} x {1} px".format(img.ihdr.width, img.ihdr.height)))
+ for chunk in img.chunks:
+ if chunk.type == 'gAMA':
+ parts.append(('gamma', str(chunk.body.gamma_int / 100000)))
+ elif chunk.type == 'pHYs':
+ aspectx = chunk.body.pixels_per_unit_x
+ aspecty = chunk.body.pixels_per_unit_y
+ parts.append(('aspect', "{0} x {1}".format(aspectx, aspecty)))
+ elif chunk.type == 'tEXt':
+ parts.append((chunk.body.keyword, chunk.body.text))
+ elif chunk.type == 'iTXt':
+ parts.append((chunk.body.keyword, chunk.body.text))
+ elif chunk.type == 'zTXt':
+ parts.append((chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')))
+ return parts