Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61039854
faksimile_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, May 4, 03:52
Size
4 KB
Mime Type
text/x-python
Expires
Mon, May 6, 03:52 (2 d)
Engine
blob
Format
Raw Data
Handle
17456513
Attached To
rNIETZSCHEPYTHON nietzsche-python
faksimile_image.py
View Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This class can be used to represent faksimile images.
"""
# 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"
import
fnmatch
from
lxml
import
etree
as
ET
import
os
from
os.path
import
basename
,
dirname
,
isfile
,
realpath
,
sep
import
sys
from
.image
import
Image
from
.matrix
import
Matrix
from
.text_field
import
TextField
sys
.
path
.
append
(
'svgscripts'
)
from
local_config
import
FAKSIMILE_LOCATION
class
FaksimileImage
(
Image
):
"""
This class represents a faksimile image.
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
x (float): x
y (float): y
"""
XML_TAG
=
'faksimile-image'
#OLD_NIETZSCHE_SOURCES_URL = 'http://www.nietzschesource.org/DFGAapi/api/page/download/'
NIETZSCHE_SOURCES_URL
=
'http://www.nietzschesource.org/DFGAapi/api/page/'
def
__init__
(
self
,
node
=
None
,
file_name
=
None
,
local_path
=
None
,
URL
=
None
,
height
=
0.0
,
width
=
0.0
,
x
=
0.0
,
y
=
0.0
,
matrix
=
None
,
text_field
=
None
):
super
(
FaksimileImage
,
self
)
.
__init__
(
node
=
node
,
file_name
=
file_name
,
URL
=
URL
,
local_path
=
local_path
,
\
height
=
height
,
width
=
width
,
matrix
=
matrix
,
text_field
=
text_field
,
tag
=
self
.
XML_TAG
)
self
.
x
=
x
self
.
y
=
y
def
get_image_joined_with_text_field
(
self
,
text_field
):
"""Returns a new instance of itself that has a text_field (text_field.TextField).
"""
return
FaksimileImage
(
file_name
=
self
.
file_name
,
local_path
=
self
.
local_path
,
URL
=
self
.
URL
,
height
=
self
.
height
,
\
width
=
self
.
width
,
x
=
self
.
x
,
y
=
self
.
y
,
text_field
=
text_field
)
# @classmethod
# def get_semantic_dictionary(cls):
# """ Creates and returns a semantic dictionary as specified by SemanticClass.
# """
# dictionary = super(FaksimileImage,cls).get_semantic_dictionary()
# dictionary[cls.PROPERTIES_KEY].update(cls.create_semantic_property_dictionary('text_field', TextField))
# #dictionary[cls.PROPERTIES_KEY].update(cls.create_semantic_property_dictionary('URL', str, cardinality=1))
# return cls.return_dictionary_after_updating_super_classes(dictionary)
@staticmethod
def
CREATE_IMAGE
(
image_node
,
source_file
=
None
):
"""Instantiates a FaksimileImage from a (lxml.etree.Element) image_node.
"""
namespaces
=
image_node
.
nsmap
if
len
(
namespaces
)
==
0
:
namespaces
=
{
'xlink'
:
''
}
local_path
=
image_node
.
get
(
'{
%s
}href'
%
namespaces
[
'xlink'
])
file_name
=
basename
(
local_path
)
if
file_name
!=
local_path
and
source_file
is
not
None
:
local_path
=
realpath
(
dirname
(
source_file
))
+
sep
+
local_path
local_path
=
realpath
(
local_path
)
if
not
isfile
(
local_path
):
local_path
=
None
for
path
,
dirs
,
files
in
os
.
walk
(
os
.
path
.
abspath
(
FAKSIMILE_LOCATION
)):
for
filename
in
fnmatch
.
filter
(
files
,
file_name
):
local_path
=
os
.
path
.
join
(
path
,
filename
)
break
URL
=
FaksimileImage
.
NIETZSCHE_SOURCES_URL
+
file_name
.
replace
(
'.jpg'
,
''
)
height
=
float
(
image_node
.
get
(
'height'
))
if
bool
(
image_node
.
get
(
'height'
))
else
0.0
width
=
float
(
image_node
.
get
(
'width'
))
if
bool
(
image_node
.
get
(
'width'
))
else
0.0
x
=
float
(
image_node
.
get
(
'x'
))
if
bool
(
image_node
.
get
(
'x'
))
else
0.0
y
=
float
(
image_node
.
get
(
'y'
))
if
bool
(
image_node
.
get
(
'y'
))
else
0.0
matrix
=
Matrix
(
transform_matrix_string
=
image_node
.
get
(
'transform'
))
\
if
bool
(
image_node
.
get
(
'transform'
))
\
else
None
return
FaksimileImage
(
file_name
=
file_name
,
local_path
=
local_path
,
URL
=
URL
,
height
=
height
,
width
=
width
,
x
=
x
,
y
=
y
,
matrix
=
matrix
)
Event Timeline
Log In to Comment