Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
executable file
·143 lines (116 loc) · 4.06 KB
/
Copy pathtools.py
File metadata and controls
executable file
·143 lines (116 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = "Ricardo Ribeiro"
__credits__ = ["Ricardo Ribeiro"]
__license__ = "MIT"
__version__ = "0.0"
__maintainer__ = "Ricardo Ribeiro"
__email__ = "ricardojvr@gmail.com"
__status__ = "Development"
import os
import sys
# import importlib
import math
from numpy import *
import zipfile
from subprocess import Popen, PIPE
import subprocess
# THIS DOESN'T WORK BECAUSE CONF IS IMPORTED WITH PYFORMS
# def load_everything_from(module_names):
# """
# Load all
# :param module_names: modules to be imported
# :type module_names: list of strings
# """
# g = globals()
# for module_name in module_names:
# m = importlib.import_module(module_name)
# names = getattr(m, '__all__', None)
# if names is None:
# names = [name for name in dir(m) if not name.startswith('_')]
# for name in names:
# g[name] = getattr(m, name)
# THIS DOESN'T WORK BECAUSE CONF IS IMPORTED WITH PYFORMS
# def register_settings_module(module):
# """
# Append another settings module to PYFORMS_APP_SETTINGS
# :param module:
# :type module:
# """
# modules = os.getenv('PYFORMS_APP_SETTINGS', '').split(';')
# modules.append(module)
# os.environ['PYFORMS_APP_SETTINGS'] = ';'.join(modules)
def getFileInSameDirectory(file, name):
module_path = os.path.abspath(os.path.dirname(file))
return os.path.join(module_path, name)
def groupImagesHorizontally( images, color=False ):
final_width, final_height = sum([ x.shape[1] for x in images ]), max([ x.shape[0] for x in images ])
if color:
final_image = zeros( (final_height, final_width, 3), dtype=uint8 )
else:
final_image = zeros( (final_height, final_width), dtype=uint8 )
cursor = 0
for image in images:
final_image[0:image.shape[0],cursor:cursor+image.shape[1]] = image
cursor += image.shape[1]
return final_image
def groupImagesVertically( images, color=False ):
final_width, final_height = max([ x.shape[1] for x in images ]), sum([ x.shape[0] for x in images ])
if color:
final_image = zeros( (final_height, final_width, 3), dtype=uint8 )
else:
final_image = zeros( (final_height, final_width), dtype=uint8 )
cursor = 0
for image in images:
final_image[cursor:cursor+image.shape[0],0:image.shape[1]] = image
cursor += image.shape[0]
return final_image
def groupImage( images,color=False ):
himages = []
for group in images:
if isinstance(group, list):
himages.append( groupImagesVertically(group,color) )
else:
himages.append( group )
return groupImagesHorizontally(himages,color)
def get_object_class_path(obj):
path = os.path.abspath(sys.modules[obj.__module__].__file__)
head, tail = os.path.split(path)
return head
def lin_dist(p0, p1): return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
def lin_dist3d(p0, p1): return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2 + (p0[2] - p1[2])**2)
def zipdir(path, zippath):
"""
walkfiles = os.walk(path)
zippath = zipfile.ZipFile(zippath, 'w')
for root, dirs, files in walkfiles:
for filename in files: zippath.write(os.path.join(root, filename))
"""
execStr = ['zip', '-r',zippath, path]
print(' '.join(execStr))
proc = subprocess.Popen(execStr, stdout=PIPE, stderr=PIPE)
(output, error) = proc.communicate()
if error: print ('error: '+ error)
print('output: '+ output)
def zipfiles(files, zippath):
zippath = zipfile.ZipFile(zippath, 'w')
for filename in files: zippath.write( filename)
def points_angle(p1, p2):
x1, y1 = p1
x2, y2 = p2
rads = math.atan2(-(y2-y1),x2-x1)
rads %= 2*pi
return rads
def getTranslationMatrix2d(dx, dy):
"""
Returns a numpy affine transformation matrix for a 2D translation of
(dx, dy)
"""
return matrix([[1, 0, dx], [0, 1, dy], [0, 0, 1]])
def combinations( l1, l2 ):
"""
Make combinations between the 2 lists without repeating
"""
for i in range(len(l1)):
yield zip( l1,l2)
l1.insert(0,l1.pop())