File: index.rst

package info (click to toggle)
kaa-imlib2 0.2.3%2Bsvn4596-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 484 kB
  • ctags: 351
  • sloc: ansic: 1,219; python: 529; makefile: 73
file content (179 lines) | stat: -rw-r--r-- 6,054 bytes parent folder | download
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
kaa.imlib2 --- Image Processing Library
=======================================

What is kaa.imlib2?
-------------------

kaa.imlib2 provides thread-safe Python bindings for Imlib2, a featureful and
efficient image processing library, which produces high quality, anti-aliased
output.

Although kaa.imlib2 does not yet provide complete coverage of the Imlib2
library, most common functions are implemented.  It also implements some custom
functionality not found in Imlib2, such as :meth:`Image.draw_mask
<imlib2.Image.draw_mask>`.

In addition to Imlib2 itself, kaa.imlib2, as with all kaa modules, requires
`kaa.base <http://doc.freevo.org/api/kaa/base/>`_.


Where do I get kaa.imlib2?
--------------------------

The easiest and recommended way to install kaa.imlib2 is using *pip* (available
as the ``python-pip`` package in Ubuntu and Fedora)::

    sudo pip install --upgrade kaa-imlib2


Or, if you prefer to install kaa.imlib2 as an egg using *setuptools* (package
``python-setuptools`` on Ubuntu and Fedora)::

    sudo easy_install -U kaa-imlib2

Your distribution might already have kaa.imlib2 included in its standard
repositories, but be aware that these are almost certainly very out of date::

    # For Ubuntu and Debian
    sudo apt-get install python-kaa-imlib2

    # For Fedora
    yum install python-kaa-imlib2


The most recent in-development version can be obtained via subversion::

    svn co svn://svn.freevo.org/kaa/trunk/imlib2 kaa-imlib2
    cd kaa-imlib2
    sudo python setup.py install


Finally, source packages are `available on SourceForge
<http://sourceforge.net/projects/freevo/files/kaa-imlib2/>`_.



API Documentation
=================

.. toctree::
   :maxdepth: 2

   image
   font

How do I use kaa.imlib2?
------------------------

Here are some examples to give you a feeling for basic usage.

First, import the module:

>>> from kaa import imlib2

Imlib2 supports most common image formats (jpeg, png, gif, tiff, bmp, etc.),
and can be loaded with :func:`imlib2.open`, which returns an
:class:`~imlib2.Image` object:

>>> img = imlib2.open('file.png')
>>> img
<kaa.imlib2.Image object size=1920x1080 at 0xb74c470c>

By default, decoded images are cached internally so that subsequent loads
pull from the cache.  If for some reason you want to bypass the cache, you
can use :func:`imlib2.open_without_cache`:

>>> img = imlib2.open_without_cache('file.png')

Or, you can disable caching altogether:

>>> imlib2.set_cache_size(0)
>>> imlib2.get_cache_size()
0L

There are a number of useful properties:

>>> img.filename, img.format, img.width, img.height
('file.png', 'png', 1920, 1080)

Rasterizing SVG into :class:`~imlib2.Image` objects is supported (provided
libsvg support is compiled into kaa.imlib2).  You can render the SVG at the
native resolution (if one is stored in the SVG) or at a custom resolution:

>>> img = imlib2.open('file.svg', (1920, 1080))

If you have an image file in memory, you can decode directly from the buffer
using :func:`imlib2.open_from_memory`.

Lastly, you can create a :func:`~imlib2.new` image, optionally with a pixel
buffer (in some RGB colorspace, e.g. ``BGRA``, ``RGB``, ``ARGB``, etc.):

>>> img = imlib2.new((1920, 1080))

Many manipulations to :class:`~imlib2.Image` objects are possible.  Some
return new objects, while others return self (to allow for convenient chaining
of manipulations); you'll need to consult the method documentation to see
what the return value is.

>>> img.draw_rectangle((10, 10), (-10, -10), '#ff0000aa')
<kaa.imlib2.Image object size=1920x1080 at 0x8571c0c>

Notice that sizes can be negative (and zero), in which case they produce a
width or height relative to the far edge of the image.  Negative positions are
also relative to the edge.  Also notice that colors can be specified as the
familiar hex color code used in HTML.  3- or 4-tuples of RGB[A] values are also
supported.

You can overlay other images using the flexible :meth:`~imlib2.Image.blend`
method, which lets you scale, crop, and composite simultaneously:

>>> watermark = imlib2.open('watermark.png')
>>> img.blend(watermark, dst_pos=(-watermark.width, -watermark.height), alpha=180)

Imlib2 can render text from TrueType fonts.  You first need to tell Imlib2
where to find fonts, but kaa.imlib2 offers a convenience function to initialize
the font path from what Fontconfig knows:

>>> imlib2.auto_set_font_path()

Then you reference the font names based on the (case-sensitive) filename of
the ``.ttf`` file.  Assuming you had ``VeraBd`` in your font path:

>>> img.draw_text((50, 50), 'Hello world!', '#ffffff', 'VeraBd/60')
(546, 92, 546, 93)

The return value shows the metrics of the rendered text.
:meth:`imlib2.Image.draw_text` has many options to render text.  It also supports
text styles such as drop shadows and outlines.  Rather than specifying the font
name, size, style, color, etc. each time, you can instead create a :class:`~imlib2.Font`
object and assign it to the image's :attr:`~imlib2.Image.font` property:

>>> img.font = imlib2.Font('VeraBd/60', '#ffffff')
>>> img.font.set_style(imlib2.TEXT_STYLE_SOFT_SHADOW, shadow='#558855')
>>> img.draw_text((50, 200), 'Uses the default style')
(989, 92, 989, 93)
>>> img.draw_text((50, 400), 'But you can still override', color='#ff000055',
...               shadow='#00ff00aa')
(1134, 92, 1134, 93)

One benefit of using Font objects is access to the method
:func:`~imlib2.Font.get_text_size`, to precompute the metrics of the text
as it would be rendered.  This is useful if, for example, you want to center
text, or otherwise position it where you'd need to know the rendered size
beforehand.

>>> img.font.size = 90
>>> w, h = img.font.get_text_size('Centered')[:2]
>>> img.draw_text(((img.width - w) / 2, (img.height - h) / 2), 'Centered')
(618, 139, 618, 139)

The above is just a small sample of what can be done with kaa.imlib2.  Refer
to the library documentation for full details and more examples.


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`