forked from sugarlabs/maps-activity
/
utils.py
142 lines (107 loc) · 3.62 KB
/
utils.py
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
# Copyright (C) 2007, One Laptop Per Child
# Copyright (c) 2008, Media Modifications Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import base64
import re
import os
import cairo
from hashlib import md5
import time
from time import strftime
from gi.repository import Rsvg
from gi.repository import GdkPixbuf
from sugar3 import util
# import _camera
def get_string_from_pixbuf(pixbuf):
data = [""]
pixbuf.save_to_callbackv(_save_data_to_buffer_cb, "png", {}, data)
return base64.b64encode(str(data[0]))
def _save_data_to_buffer_cb(buf, data):
data[0] += buf
return True
def get_pixbuf_from_string(str):
pbl = GdkPixbuf.PixbufLoader()
data = base64.b64decode(str)
pbl.write(data)
pbl.close()
return pbl.get_pixbuf()
def load_svg(data, stroke, fill):
if stroke is None or fill is None:
return Rsvg.Handle.new_from_data(data)
entity = '<!ENTITY fill_color "%s">' % fill
data = re.sub('<!ENTITY fill_color .*>', entity, data)
entity = '<!ENTITY stroke_color "%s">' % stroke
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
return Rsvg.Handle.new_from_data(data)
def get_unique_filepath(path, i):
path_ob = os.path.abspath(path)
new_path = os.path.join(
os.path.dirname(path_ob), str(
str(i) + os.path.basename(path_ob)))
if (os.path.exists(new_path)):
i = i + 1
return get_unique_filepath(path_ob, i)
else:
return os.path.abspath(new_path)
def md5_file(filepath):
md = md5()
f = file(filepath, 'rb')
md.update(f.read())
digest = md.hexdigest()
hash = util.printable_hash(digest)
return hash
def generate_thumbnail(pixbuf, scale, thumbw, thumbh):
# need to generate thumbnail version here
thumb_img = cairo.ImageSurface(cairo.FORMAT_ARGB32, thumbw, thumbh)
tctx = cairo.Context(thumb_img)
# img = _camera.cairo_surface_from_gdk_pixbuf(pixbuf)
# tctx.scale(scale, scale)
# tctx.set_source_surface(img, 0, 0)
# tctx.paint()
# gc.collect()
return None # thumbImg
def scale_svg_to_dim(handle, dim):
# todo...
scale = 1.0
svg_dim = handle.get_dimension_data()
if (svg_dim[0] > dim[0]): # wtf?
pass
return scale
def get_date_string(when):
# todo: internationalize the date
return strftime("%a, %b %d, %I:%M:%S %p", time.localtime(when))
def gray_scale_pixbuf2(pb, copy):
arr = pb.get_pixels_array()
if (copy):
arr = arr.copy()
for row in arr:
for pxl in row:
y = 0.3 * pxl[0][0] + 0.59 * pxl[1][0] + 0.11 * pxl[2][0]
pxl[0][0] = y
pxl[1][0] = y
pxl[2][0] = y
return GdkPixbuf.Pixbuf.new_from_data(
arr, pb.get_colorspace(), pb.get_bits_per_sample())
def gray_scale_pixBuf(pb, copy):
pb2 = GdkPixbuf.Pixbuf(
GdkPixbuf.Colorspace.RGB,
False,
8,
pb.get_width(),
pb.get_height())
pb.saturate_and_pixelate(pb2, 0, 0)
return pb2