comparison src/cagou/core/image.py @ 15:56838ad5c84b

files reorganisation, cagou is now launched with python2 cagou.py in src/
author Goffi <goffi@goffi.org>
date Sat, 09 Jul 2016 17:24:01 +0200
parents src/image.py@d9095d1dd7ae
children d5ede9281e4c
comparison
equal deleted inserted replaced
14:21a432afd06d 15:56838ad5c84b
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 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