Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60084346
image.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Apr 27, 09:09
Size
6 KB
Mime Type
text/x-python
Expires
Mon, Apr 29, 09:09 (2 d)
Engine
blob
Format
Raw Data
Handle
17297846
Attached To
rNIETZSCHEPYTHON nietzsche-python
image.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This super class can be used to represent all image types.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
__author__
=
"Christian Steiner"
__maintainer__
=
__author__
__copyright__
=
'University of Basel'
__email__
=
"christian.steiner@unibas.ch"
__status__
=
"Development"
__license__
=
"GPL v3"
__version__
=
"0.0.1"
from
lxml
import
etree
as
ET
from
os.path
import
isfile
,
basename
import
sys
from
.attachable_object
import
AttachableObject
from
.matrix
import
Matrix
from
.text_field
import
TextField
sys
.
path
.
append
(
'py2ttl'
)
from
class_spec
import
SemanticClass
class
Image
(
AttachableObject
,
SemanticClass
):
"""
This super class represents all types of images.
Args:
file_name (str): name of the image file.
node (lxml.etree.Element) node, containing information
URL (str): URL of image file.
height (float): height of image
width (float): width of image
text_field (.text_field.TextField) text_field on image representation
"""
stringKeys
=
[
'file_name'
,
'URL'
,
'local_path'
]
floatKeys
=
[
'height'
,
'width'
]
XML_TAG
=
'image'
SECONDARY_URL
=
'http://localhost:8000/'
FAKSIMILE_DIR
=
'faksimiles/'
def
__init__
(
self
,
node
=
None
,
file_name
=
None
,
local_path
=
None
,
URL
=
None
,
height
=
0.0
,
width
=
0.0
,
matrix
=
None
,
text_field
=
None
,
tag
=
XML_TAG
):
self
.
text_field
=
text_field
self
.
tag
=
tag
if
node
is
not
None
:
self
.
file_name
=
node
.
get
(
'file-name'
)
self
.
local_path
=
node
.
get
(
'local-path'
)
self
.
URL
=
node
.
get
(
'URL'
)
self
.
height
=
float
(
node
.
get
(
'height'
))
self
.
width
=
float
(
node
.
get
(
'width'
))
self
.
transform
=
Matrix
(
node
.
get
(
'transform'
))
if
bool
(
node
.
get
(
'transform'
))
and
'matrix('
in
node
.
get
(
'transform'
)
else
None
if
len
(
node
.
findall
(
TextField
.
XML_TAG
))
>
0
:
self
.
text_field
=
TextField
(
node
=
node
.
find
(
TextField
.
XML_TAG
))
else
:
self
.
file_name
=
file_name
self
.
local_path
=
local_path
self
.
URL
=
URL
self
.
height
=
height
self
.
width
=
width
self
.
transform
=
matrix
self
.
primaryURL
=
self
.
URL
self
.
secondaryURL
=
None
if
self
.
file_name
is
not
None
:
self
.
secondaryURL
=
self
.
SECONDARY_URL
+
self
.
file_name
.
replace
(
'./'
,
''
)
\
if
self
.
file_name
is
not
None
and
self
.
file_name
.
endswith
(
'svg'
)
\
else
self
.
SECONDARY_URL
+
self
.
FAKSIMILE_DIR
+
self
.
file_name
self
.
transform_string
=
self
.
transform
.
toString
()
\
if
self
.
transform
is
not
None
\
else
None
def
attach_object_to_tree
(
self
,
target_tree
):
"""Attach object to tree.
"""
if
target_tree
.
__class__
.
__name__
==
'_ElementTree'
:
target_tree
=
target_tree
.
getroot
()
obj_node
=
target_tree
.
find
(
'.//'
+
self
.
tag
)
\
if
(
len
(
target_tree
.
findall
(
'.//'
+
self
.
tag
))
>
0
)
\
else
ET
.
SubElement
(
target_tree
,
self
.
tag
)
for
key
in
self
.
floatKeys
:
if
self
.
__dict__
[
key
]
is
not
None
:
obj_node
.
set
(
key
.
replace
(
'_'
,
'-'
),
str
(
round
(
self
.
__dict__
[
key
],
3
)))
for
key
in
self
.
stringKeys
:
if
self
.
__dict__
[
key
]
is
not
None
:
obj_node
.
set
(
key
.
replace
(
'_'
,
'-'
),
self
.
__dict__
[
key
])
if
self
.
transform
is
not
None
and
self
.
transform
.
isRotationMatrix
():
obj_node
.
set
(
'transform'
,
self
.
transform
.
toString
())
if
self
.
text_field
is
not
None
:
self
.
text_field
.
attach_object_to_tree
(
obj_node
)
@classmethod
def
get_semantic_dictionary
(
cls
):
""" Creates and returns a semantic dictionary as specified by SemanticClass.
"""
dictionary
=
{}
class_dict
=
cls
.
get_class_dictionary
()
properties
=
{}
for
floatKey
in
Image
.
floatKeys
:
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
floatKey
,
float
,
cardinality
=
1
))
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
'file_name'
,
str
,
cardinality
=
1
))
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
'text_field'
,
TextField
))
#properties.update(cls.create_semantic_property_dictionary('transform', str))
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
'transform_string'
,
str
,
name
=
'hasTransform'
))
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
'primaryURL'
,
str
,
cardinality
=
1
,
subPropertyOf
=
cls
.
HAS_URL
))
properties
.
update
(
cls
.
create_semantic_property_dictionary
(
'secondaryURL'
,
str
,
cardinality
=
1
,
subPropertyOf
=
cls
.
HAS_URL
))
dictionary
.
update
({
'class'
:
class_dict
})
dictionary
.
update
({
'properties'
:
properties
})
return
dictionary
class
SVGImage
(
Image
):
"""This class represents a svg image.
"""
XML_TAG
=
'svg-image'
ASSETS_FOLDER
=
'/assets/'
URL_PREFIX
=
'https://nietzsche.philhist.unibas.ch/svg/'
def
__init__
(
self
,
node
=
None
,
file_name
=
None
,
URL
=
None
,
height
=
0.0
,
width
=
0.0
,
text_field
=
None
,
tag
=
XML_TAG
):
if
node
is
not
None
and
node
.
tag
!=
self
.
XML_TAG
:
file_name
=
node
.
get
(
'file'
)
height
=
float
(
node
.
get
(
'height'
))
if
bool
(
node
.
get
(
'height'
))
else
0.0
width
=
float
(
node
.
get
(
'width'
))
if
bool
(
node
.
get
(
'width'
))
else
0.0
node
=
None
super
(
SVGImage
,
self
)
.
__init__
(
node
=
node
,
file_name
=
file_name
,
URL
=
URL
,
\
height
=
height
,
width
=
width
,
text_field
=
text_field
,
tag
=
self
.
XML_TAG
)
self
.
primaryURL
=
self
.
URL_PREFIX
+
basename
(
self
.
file_name
.
replace
(
'./'
,
''
))
self
.
secondaryURL
=
self
.
ASSETS_FOLDER
+
self
.
file_name
.
replace
(
'./'
,
''
)
def
decontextualize_file_name
(
self
,
update_url
=
None
):
"""Decontextualize file name.
"""
self
.
file_name
=
self
.
file_name
.
replace
(
'./'
,
''
)
if
update_url
is
not
None
:
self
.
URL
=
update_url
+
self
.
file_name
# @classmethod
# def get_semantic_dictionary(cls):
# """ Creates and returns a semantic dictionary as specified by SemanticClass.
# """
# dictionary = super(SVGImage,cls).get_semantic_dictionary()
# return cls.return_dictionary_after_updating_super_classes(dictionary)
Event Timeline
Log In to Comment