/usr/share/pyshared/kivy/uix/image.py is in python-kivy 1.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | '''
Image
=====
The :class:`Image` widget is used to display an image::
wimg = Image(source='mylogo.png')
Asynchronous Loading
--------------------
To load an image asynchronously (for example from an external webserver), use
the :class:`AsyncImage` subclass::
aimg = AsyncImage(source='http://mywebsite.com/logo.png')
Alignment
---------
By default, the image is centered and fit inside the widget bounding box.
If you don't want that, you can inherit from Image and create your own style.
For example, if you want your image to take the same size of your widget, you
can do::
class FullImage(Image):
pass
And in your kivy language file, you can do::
<FullImage>:
canvas:
Color:
rgb: (1, 1, 1)
Rectangle:
texture: self.texture
size: self.size
pos: self.pos
'''
__all__ = ('Image', 'AsyncImage')
from kivy.uix.widget import Widget
from kivy.core.image import Image as CoreImage
from kivy.resources import resource_find
from kivy.properties import StringProperty, ObjectProperty, ListProperty, \
AliasProperty, BooleanProperty, NumericProperty
from kivy.loader import Loader
from kivy.logger import Logger
class Image(Widget):
'''Image class, see module documentation for more information.
'''
source = StringProperty(None)
'''Filename / source of your image.
:data:`source` is a :class:`~kivy.properties.StringProperty`, default to
None.
'''
texture = ObjectProperty(None, allownone=True)
'''Texture object of the image.
Depending of the texture creation, the value will be a
:class:`~kivy.graphics.texture.Texture` or
:class:`~kivy.graphics.texture.TextureRegion` object.
:data:`texture` is a :class:`~kivy.properties.ObjectProperty`, default to
None.
'''
texture_size = ListProperty([0, 0])
'''Texture size of the image.
.. warning::
The texture size is set after the texture property. So if you listen on
the change to :data:`texture`, the property texture_size will not be
up-to-date. Use self.texture.size instead.
'''
def get_image_ratio(self):
if self.texture:
return self.texture.width / float(self.texture.height)
return 1.
mipmap = BooleanProperty(False)
'''Indicate if you want OpenGL mipmapping to be applied on the texture.
Read :ref:`mipmap` for more information.
.. versionadded:: 1.0.7
:data:`mipmap` is a :class:`~kivy.properties.BooleanProperty`, default to
False.
'''
image_ratio = AliasProperty(get_image_ratio, None, bind=('texture', ))
'''Ratio of the image (width / float(height).
:data:`image_ratio` is a :class:`~kivy.properties.AliasProperty`, and is
read-only.
'''
color = ListProperty([1, 1, 1, 1])
'''Image color, in the format (r, g, b, a). This attribute can be used to
'tint' an image. Be careful, if the source image is not gray/white, the
color will not really work as expected.
.. versionadded:: 1.0.6
:data:`color` is a :class:`~kivy.properties.ListProperty`, default to [1, 1,
1, 1].
'''
allow_stretch = BooleanProperty(False)
'''If True, the normalized image size will be maximized to fit in the image
box. Otherwise, if the box is too tall, the image will not be stretched more
than 1:1 pixels.
.. versionadded:: 1.0.7
:data:`allow_stretch` is a :class:`~kivy.properties.BooleanProperty`,
default to False
'''
keep_ratio = BooleanProperty(True)
'''If False along with allow_stretch being True, the normalized image
size will be maximized to fit in the image box, disregarding the aspect
ratio of the image.
Otherwise, if the box is too tall, the image will not be stretched more
than 1:1 pixels.
.. versionadded:: 1.0.8
:data:`keep_ratio` is a :class:`~kivy.properties.BooleanProperty`,
default to True
'''
keep_data = BooleanProperty(False)
'''If true the underlaying _coreimage have to keep the raw image data.
Useful to perform pixel based collision detection
.. versionadded:: 1.3.0
:data:`keep_data` is a :class:`~kivy.properties.BooleanProperty`, default
to False
'''
anim_delay = NumericProperty(.25)
'''Delay of animation if the image is sequenced (like an animated gif).
If the anim_delay is set to -1, the animation will be stopped.
.. versionadded:: 1.0.8
:data:`anim_delay` is a :class:`~kivy.properties.NumericProperty`, default
to .25 (4 FPS)
'''
nocache = BooleanProperty(False)
'''If this property is set True, the image will not be added to the
internal cache anymore. (the cache will simply ignore any calls trying to
append the core image)
.. versionadded:: 1.6.0
:data:`nocache` is a :class:`~kivy.properties.BooleanProperty`, default
to False
'''
def get_norm_image_size(self):
if not self.texture:
return self.size
ratio = self.image_ratio
w, h = self.size
tw, th = self.texture.size
# ensure that the width is always maximized to the containter width
if self.allow_stretch:
if not self.keep_ratio:
return w, h
iw = w
else:
iw = min(w, tw)
# calculate the appropriate height
ih = iw / ratio
# if the height is too higher, take the height of the container
# and calculate appropriate width. no need to test further. :)
if ih > h:
if self.allow_stretch:
ih = h
else:
ih = min(h, th)
iw = ih * ratio
return iw, ih
norm_image_size = AliasProperty(get_norm_image_size, None, bind=(
'texture', 'size', 'image_ratio', 'allow_stretch'))
'''Normalized image size within the widget box.
This size will always be fit to the widget size, and will preserve the image
ratio.
:data:`norm_image_size` is a :class:`~kivy.properties.AliasProperty`, and is
read-only.
'''
def __init__(self, **kwargs):
self._coreimage = None
super(Image, self).__init__(**kwargs)
self.bind(source=self.texture_update,
mipmap=self.texture_update)
if self.source:
self.texture_update()
def texture_update(self, *largs):
if not self.source:
self.texture = None
else:
filename = resource_find(self.source)
if filename is None:
return Logger.error('Image: Error reading file {filename}'.
format(filename=self.source))
mipmap = self.mipmap
if self._coreimage is not None:
self._coreimage.unbind(on_texture=self._on_tex_change)
self._coreimage = ci = CoreImage(filename, mipmap=mipmap,
anim_delay=self.anim_delay, keep_data=self.keep_data,
nocache=self.nocache)
ci.bind(on_texture=self._on_tex_change)
self.texture = ci.texture
def on_anim_delay(self, instance, value):
if self._coreimage is None:
return
self._coreimage.anim_delay = value
if value < 0:
self._coreimage.anim_reset(False)
def on_texture(self, instance, value):
if value is not None:
self.texture_size = list(value.size)
def _on_tex_change(self, *largs):
# update texture from core image
self.texture = self._coreimage.texture
def reload(self):
'''Reload image from disk. This facilitates re-loading of
image from disk in case contents change.
.. versionadded:: 1.3.0
Usage::
im = Image(source = '1.jpg')
# -- do something --
im.reload()
# image will be re-loaded from disk
'''
self._coreimage.remove_from_cache()
olsource = self.source
self.source = ''
self.source = olsource
def on_nocache(self, *args):
if self.nocache and self._coreimage:
self._coreimage.remove_from_cache()
self._coreimage._nocache = True
class AsyncImage(Image):
'''Asynchronous Image class. See module documentation for more information.
'''
def __init__(self, **kwargs):
self._coreimage = None
super(AsyncImage, self).__init__(**kwargs)
self.bind(source=self._load_source)
if self.source:
self._load_source()
def _load_source(self, *args):
source = self.source
if not source:
if self._coreimage is not None:
self._coreimage.unbind(on_texture=self._on_tex_change)
self.texture = None
self._coreimage = None
else:
if not self.is_uri(source):
source = resource_find(source)
self._coreimage = image = Loader.image(source,
nocache=self.nocache, mipmap=self.mipmap)
image.bind(on_load=self._on_source_load)
image.bind(on_texture=self._on_tex_change)
self.texture = image.texture
def _on_source_load(self, value):
image = self._coreimage.image
if not image:
return
self.texture = image.texture
def is_uri(self, filename):
proto = filename.split('://', 1)[0]
return proto in ('http', 'https', 'ftp', 'smb')
def _on_tex_change(self, *largs):
if self._coreimage:
self.texture = self._coreimage.texture
def texture_update(self, *largs):
pass
|