summaryrefslogtreecommitdiff
path: root/libmat2/images.py
diff options
context:
space:
mode:
authorjvoisin2019-09-01 09:28:46 -0700
committerjvoisin2019-09-01 09:28:46 -0700
commit397a18b0cc6453c9d72ce2cd110f3724ac809313 (patch)
treee87174f3cbeeb4be071bf5deb2fc55a62f757ec1 /libmat2/images.py
parentfc924239febb3f186585d9ea6c263e1cb7dc690d (diff)
Add support for ppm
Diffstat (limited to 'libmat2/images.py')
-rw-r--r--libmat2/images.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/libmat2/images.py b/libmat2/images.py
index 2781e05..18fe4d3 100644
--- a/libmat2/images.py
+++ b/libmat2/images.py
@@ -1,6 +1,7 @@
1import imghdr 1import imghdr
2import os 2import os
3from typing import Set, Dict, Union 3import re
4from typing import Set, Dict, Union, Any
4 5
5import cairo 6import cairo
6 7
@@ -9,10 +10,11 @@ gi.require_version('GdkPixbuf', '2.0')
9gi.require_version('Rsvg', '2.0') 10gi.require_version('Rsvg', '2.0')
10from gi.repository import GdkPixbuf, GLib, Rsvg 11from gi.repository import GdkPixbuf, GLib, Rsvg
11 12
12from . import exiftool 13from . import exiftool, abstract
13 14
14# Make pyflakes happy 15# Make pyflakes happy
15assert Set 16assert Set
17assert Any
16 18
17class SVGParser(exiftool.ExiftoolParser): 19class SVGParser(exiftool.ExiftoolParser):
18 mimetypes = {'image/svg+xml', } 20 mimetypes = {'image/svg+xml', }
@@ -138,3 +140,23 @@ class TiffParser(GdkPixbufAbstractParser):
138 'FilePermissions', 'FileSize', 'FileType', 140 'FilePermissions', 'FileSize', 'FileType',
139 'FileTypeExtension', 'ImageHeight', 'ImageSize', 141 'FileTypeExtension', 'ImageHeight', 'ImageSize',
140 'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'} 142 'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
143
144class PPMParser(abstract.AbstractParser):
145 mimetypes = {'image/x-portable-pixmap'}
146
147 def get_meta(self) -> Dict[str, Union[str, dict]]:
148 meta = {} # type: Dict[str, Union[str, Dict[Any, Any]]]
149 with open(self.filename) as f:
150 for idx, line in enumerate(f):
151 if line.lstrip().startswith('#'):
152 meta[str(idx)] = line.lstrip().rstrip()
153 return meta
154
155 def remove_all(self) -> bool:
156 with open(self.filename) as fin:
157 with open(self.output_filename, 'w') as fout:
158 for line in fin:
159 if not line.lstrip().startswith('#'):
160 line = re.sub(r"\s+", "", line, flags=re.UNICODE)
161 fout.write(line)
162 return True