diff options
| author | jvoisin | 2019-09-01 09:28:46 -0700 |
|---|---|---|
| committer | jvoisin | 2019-09-01 09:28:46 -0700 |
| commit | 397a18b0cc6453c9d72ce2cd110f3724ac809313 (patch) | |
| tree | e87174f3cbeeb4be071bf5deb2fc55a62f757ec1 /libmat2 | |
| parent | fc924239febb3f186585d9ea6c263e1cb7dc690d (diff) | |
Add support for ppm
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/images.py | 26 |
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 @@ | |||
| 1 | import imghdr | 1 | import imghdr |
| 2 | import os | 2 | import os |
| 3 | from typing import Set, Dict, Union | 3 | import re |
| 4 | from typing import Set, Dict, Union, Any | ||
| 4 | 5 | ||
| 5 | import cairo | 6 | import cairo |
| 6 | 7 | ||
| @@ -9,10 +10,11 @@ gi.require_version('GdkPixbuf', '2.0') | |||
| 9 | gi.require_version('Rsvg', '2.0') | 10 | gi.require_version('Rsvg', '2.0') |
| 10 | from gi.repository import GdkPixbuf, GLib, Rsvg | 11 | from gi.repository import GdkPixbuf, GLib, Rsvg |
| 11 | 12 | ||
| 12 | from . import exiftool | 13 | from . import exiftool, abstract |
| 13 | 14 | ||
| 14 | # Make pyflakes happy | 15 | # Make pyflakes happy |
| 15 | assert Set | 16 | assert Set |
| 17 | assert Any | ||
| 16 | 18 | ||
| 17 | class SVGParser(exiftool.ExiftoolParser): | 19 | class 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 | |||
| 144 | class 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 | ||
