Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jvoisin
mat2
Commits
397a18b0
Commit
397a18b0
authored
5 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Add support for ppm
parent
fc924239
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
libmat2/images.py
+24
-2
24 additions, 2 deletions
libmat2/images.py
tests/data/dirty.ppm
+8
-0
8 additions, 0 deletions
tests/data/dirty.ppm
tests/test_libmat2.py
+29
-0
29 additions, 0 deletions
tests/test_libmat2.py
with
61 additions
and
2 deletions
libmat2/images.py
+
24
−
2
View file @
397a18b0
import
imghdr
import
os
from
typing
import
Set
,
Dict
,
Union
import
re
from
typing
import
Set
,
Dict
,
Union
,
Any
import
cairo
...
...
@@ -9,10 +10,11 @@ gi.require_version('GdkPixbuf', '2.0')
gi
.
require_version
(
'
Rsvg
'
,
'
2.0
'
)
from
gi.repository
import
GdkPixbuf
,
GLib
,
Rsvg
from
.
import
exiftool
from
.
import
exiftool
,
abstract
# Make pyflakes happy
assert
Set
assert
Any
class
SVGParser
(
exiftool
.
ExiftoolParser
):
mimetypes
=
{
'
image/svg+xml
'
,
}
...
...
@@ -138,3 +140,23 @@ class TiffParser(GdkPixbufAbstractParser):
'
FilePermissions
'
,
'
FileSize
'
,
'
FileType
'
,
'
FileTypeExtension
'
,
'
ImageHeight
'
,
'
ImageSize
'
,
'
ImageWidth
'
,
'
MIMEType
'
,
'
Megapixels
'
,
'
SourceFile
'
}
class
PPMParser
(
abstract
.
AbstractParser
):
mimetypes
=
{
'
image/x-portable-pixmap
'
}
def
get_meta
(
self
)
->
Dict
[
str
,
Union
[
str
,
dict
]]:
meta
=
{}
# type: Dict[str, Union[str, Dict[Any, Any]]]
with
open
(
self
.
filename
)
as
f
:
for
idx
,
line
in
enumerate
(
f
):
if
line
.
lstrip
().
startswith
(
'
#
'
):
meta
[
str
(
idx
)]
=
line
.
lstrip
().
rstrip
()
return
meta
def
remove_all
(
self
)
->
bool
:
with
open
(
self
.
filename
)
as
fin
:
with
open
(
self
.
output_filename
,
'
w
'
)
as
fout
:
for
line
in
fin
:
if
not
line
.
lstrip
().
startswith
(
'
#
'
):
line
=
re
.
sub
(
r
"
\s+
"
,
""
,
line
,
flags
=
re
.
UNICODE
)
fout
.
write
(
line
)
return
True
This diff is collapsed.
Click to expand it.
tests/data/dirty.ppm
0 → 100644
+
8
−
0
View file @
397a18b0
P3
# A metadata
3 2 1
1 0 1 0 1 0 0 0 1
# And an other one
1 1 0 1 0 1 1 0 0
# and a final one here
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
29
−
0
View file @
397a18b0
...
...
@@ -113,6 +113,14 @@ class TestGetMeta(unittest.TestCase):
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
Comment
'
],
'
Created with GIMP
'
)
def
test_ppm
(
self
):
p
=
images
.
PPMParser
(
'
./tests/data/dirty.ppm
'
)
meta
=
p
.
get_meta
()
self
.
assertEqual
(
meta
[
'
1
'
],
'
# A metadata
'
)
self
.
assertEqual
(
meta
[
'
4
'
],
'
# And an other one
'
)
self
.
assertEqual
(
meta
[
'
6
'
],
'
# and a final one here
'
)
def
test_tiff
(
self
):
p
=
images
.
TiffParser
(
'
./tests/data/dirty.tiff
'
)
meta
=
p
.
get_meta
()
...
...
@@ -887,3 +895,24 @@ class TestCleaning(unittest.TestCase):
p
=
images
.
SVGParser
(
'
./tests/data/weird.svg
'
)
self
.
assertEqual
(
p
.
get_meta
()[
'
Xmlns
'
],
'
http://www.w3.org/1337/svg
'
)
def
test_ppm
(
self
):
shutil
.
copy
(
'
./tests/data/dirty.ppm
'
,
'
./tests/data/clean.ppm
'
)
p
=
images
.
PPMParser
(
'
./tests/data/clean.ppm
'
)
meta
=
p
.
get_meta
()
print
(
meta
)
self
.
assertEqual
(
meta
[
'
1
'
],
'
# A metadata
'
)
ret
=
p
.
remove_all
()
self
.
assertTrue
(
ret
)
p
=
images
.
PPMParser
(
'
./tests/data/clean.cleaned.ppm
'
)
self
.
assertEqual
(
p
.
get_meta
(),
{})
self
.
assertTrue
(
p
.
remove_all
())
os
.
remove
(
'
./tests/data/clean.ppm
'
)
os
.
remove
(
'
./tests/data/clean.cleaned.ppm
'
)
os
.
remove
(
'
./tests/data/clean.cleaned.cleaned.ppm
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment