comparison cagou/core/image.py @ 126:cd99f70ea592

global file reorganisation: - follow common convention by puttin cagou in "cagou" instead of "src/cagou" - added VERSION in cagou with current version - updated dates - moved main executable in /bin - moved buildozer files in root directory - temporary moved platform to assets/platform
author Goffi <goffi@goffi.org>
date Thu, 05 Apr 2018 17:11:21 +0200
parents src/cagou/core/image.py@d5ede9281e4c
children 1cca97e27a69
comparison
equal deleted inserted replaced
125:b6e6afb0dc46 126:cd99f70ea592
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from sat.core import log as logging
22 log = logging.getLogger(__name__)
23 from kivy.uix import image as kivy_img
24 from kivy.core.image import Image as CoreImage
25 from kivy.resources import resource_find
26 import io
27 import PIL
28
29
30 class Image(kivy_img.Image):
31 """Image widget which accept source without extension"""
32
33 def texture_update(self, *largs):
34 if not self.source:
35 self.texture = None
36 else:
37 filename = resource_find(self.source)
38 self._loops = 0
39 if filename is None:
40 return log.error('Image: Error reading file {filename}'.
41 format(filename=self.source))
42 mipmap = self.mipmap
43 if self._coreimage is not None:
44 self._coreimage.unbind(on_texture=self._on_tex_change)
45 try:
46 self._coreimage = ci = CoreImage(filename, mipmap=mipmap,
47 anim_delay=self.anim_delay,
48 keep_data=self.keep_data,
49 nocache=self.nocache)
50 except Exception as e:
51 # loading failed probably because of unmanaged extention,
52 # we try our luck with with PIL
53 try:
54 im = PIL.Image.open(filename)
55 ext = im.format.lower()
56 del im
57 # we can't use im.tobytes as it would use the
58 # internal decompressed representation from pillow
59 # and im.save would need processing to handle format
60 data = io.BytesIO(open(filename, "rb").read())
61 cache_filename = u"{}.{}".format(filename,ext) # needed for kivy's Image to use cache
62 self._coreimage = ci = CoreImage(data, ext=ext,
63 filename=cache_filename, mipmap=mipmap,
64 anim_delay=self.anim_delay,
65 keep_data=self.keep_data,
66 nocache=self.nocache)
67 except Exception as e:
68 log.warning(u"Can't load image: {}".format(e))
69 self._coreimage = ci = None
70
71 if ci:
72 ci.bind(on_texture=self._on_tex_change)
73 self.texture = ci.texture
74
75
76 class AsyncImage(kivy_img.AsyncImage):
77 """AsyncImage which accept file:// schema"""
78
79 def _load_source(self, *args):
80 if self.source.startswith('file://'):
81 self.source = self.source[7:]
82 else:
83 super(AsyncImage, self)._load_source(*args)
84