| 1 |
Images and Sprites |
| 2 |
================== |
| 3 |
|
| 4 |
pyglet provides functions for loading and saving images in various formats |
| 5 |
using native operating system services. If the `Pillow`_ library is installed, |
| 6 |
many additional formats can be supported. pyglet also includes built-in |
| 7 |
codecs for loading PNG and BMP without external dependencies. |
| 8 |
|
| 9 |
In addition to loading, pyglet also supports the following operations |
| 10 |
for both OpenGL textures and framebuffers: |
| 11 |
|
| 12 |
* converting to pyglet image objects |
| 13 |
* saving to disk as screenshots |
| 14 |
* manipulation as image data |
| 15 |
* converting to :py:class:`bytes` of raw pixel data |
| 16 |
|
| 17 |
For most users, the :py:class:`~pyglet.sprite.Sprite` class is the best way |
| 18 |
to draw an image. One or more instances may draw the same image data with |
| 19 |
individually configured values for position, scaling, rotation, and more. |
| 20 |
|
| 21 |
.. _Pillow: https://pillow.readthedocs.io |
| 22 |
|
| 23 |
Loading an image |
| 24 |
---------------- |
| 25 |
|
| 26 |
Images can be loaded using the :py:func:`pyglet.image.load` function:: |
| 27 |
|
| 28 |
kitten = pyglet.image.load('kitten.png') |
| 29 |
|
| 30 |
If you are distributing your application with included images, consider |
| 31 |
using the :py:mod:`pyglet.resource` module (see :ref:`guide_resources`). |
| 32 |
|
| 33 |
Without any additional arguments, :py:func:`pyglet.image.load` will |
| 34 |
attempt to load the filename specified using any available image decoder. |
| 35 |
This will allow you to load PNG, GIF, JPEG, BMP and DDS files, |
| 36 |
and possibly other files as well, depending on your operating system |
| 37 |
and additional installed modules (see the next section for details). |
| 38 |
If the image cannot be loaded, an |
| 39 |
:py:class:`~pyglet.image.codecs.ImageDecodeException` will be raised. |
| 40 |
|
| 41 |
You can load an image from any file-like object providing a `read` method by |
| 42 |
specifying the `file` keyword parameter:: |
| 43 |
|
| 44 |
kitten_stream = open('kitten.png', 'rb') |
| 45 |
kitten = pyglet.image.load('kitten.png', file=kitten_stream) |
| 46 |
|
| 47 |
In this case the filename ``kitten.png`` is optional, but gives a hint to |
| 48 |
the decoder as to the file type (it is otherwise unused when a file object |
| 49 |
is provided). |
| 50 |
|
| 51 |
Displaying images |
| 52 |
----------------- |
| 53 |
|
| 54 |
Image drawing is usually done in the window's |
| 55 |
:py:meth:`~pyglet.window.Window.on_draw` event handler. |
| 56 |
It is possible to draw individual images directly, but usually you will |
| 57 |
want to create a "sprite" for each appearance of the image on-screen. |
| 58 |
|
| 59 |
Sprites |
| 60 |
^^^^^^^ |
| 61 |
|
| 62 |
A Sprite is a full featured class for displaying instances of Images or |
| 63 |
Animations in the window. Image and Animation instances are mainly concerned |
| 64 |
with the image data (size, pixels, etc.), whereas Sprites also include |
| 65 |
additional properties. These include x/y location, scale, rotation, opacity, |
| 66 |
color tint, visibility, and both horizontal and vertical scaling. |
| 67 |
Multiple sprites can share the same image; for example, hundreds of bullet |
| 68 |
sprites might share the same bullet image. |
| 69 |
|
| 70 |
A Sprite is constructed given an image or animation, and can be directly |
| 71 |
drawn with the :py:meth:`~pyglet.sprite.Sprite.draw` method:: |
| 72 |
|
| 73 |
sprite = pyglet.sprite.Sprite(img=image, x=100, y=50) |
| 74 |
|
| 75 |
@window.event |
| 76 |
def on_draw(): |
| 77 |
window.clear() |
| 78 |
sprite.draw() |
| 79 |
|
| 80 |
If created with an animation, sprites automatically handle displaying |
| 81 |
the most up-to-date frame of the animation. The following example uses a |
| 82 |
scheduled function to gradually move the Sprite across the screen:: |
| 83 |
|
| 84 |
def update(dt): |
| 85 |
# Move 10 pixels per second |
| 86 |
sprite.x += dt * 10 |
| 87 |
|
| 88 |
# Call update 60 times a second |
| 89 |
pyglet.clock.schedule_interval(update, 1/60.) |
| 90 |
|
| 91 |
If you need to draw many sprites, using a :py:class:`~pyglet.graphics.Batch` |
| 92 |
to draw them all at once is strongly recommended. This is far more efficient |
| 93 |
than calling :py:meth:`~pyglet.sprite.Sprite.draw` on each of them in a loop:: |
| 94 |
|
| 95 |
batch = pyglet.graphics.Batch() |
| 96 |
|
| 97 |
sprites = [pyglet.sprite.Sprite(image, batch=batch), |
| 98 |
pyglet.sprite.Sprite(image, batch=batch), |
| 99 |
# ... ] |
| 100 |
|
| 101 |
@window.event |
| 102 |
def on_draw(): |
| 103 |
window.clear() |
| 104 |
batch.draw() |
| 105 |
|
| 106 |
When sprites are collected into a batch, no guarantee is made about the order |
| 107 |
in which they will be drawn. If you need to ensure some sprites are drawn |
| 108 |
before others (for example, landscape tiles might be drawn before character |
| 109 |
sprites, which might be drawn before some particle effect sprites), use two |
| 110 |
or more :py:class:`~pyglet.graphics.Group` objects to specify the |
| 111 |
draw order:: |
| 112 |
|
| 113 |
batch = pyglet.graphics.Batch() |
| 114 |
background = pyglet.graphics.Group(order=0) |
| 115 |
foreground = pyglet.graphics.Group(order=1) |
| 116 |
|
| 117 |
sprites = [pyglet.sprite.Sprite(image, batch=batch, group=background), |
| 118 |
pyglet.sprite.Sprite(image, batch=batch, group=background), |
| 119 |
pyglet.sprite.Sprite(image, batch=batch, group=foreground), |
| 120 |
pyglet.sprite.Sprite(image, batch=batch, group=foreground), |
| 121 |
# ...] |
| 122 |
|
| 123 |
@window.event |
| 124 |
def on_draw(): |
| 125 |
window.clear() |
| 126 |
batch.draw() |
| 127 |
|
| 128 |
For best performance, you should use as few batches and groups as required. |
| 129 |
(See the :ref:`guide_graphics` section for more details on batch |
| 130 |
and group rendering). This will reduce the number of internal and OpenGL |
| 131 |
operations for drawing each frame. |
| 132 |
|
| 133 |
In addition, try to combine your images into as few textures as possible; |
| 134 |
for example, by loading images with :py:func:`pyglet.resource.image` |
| 135 |
(see :ref:`guide_resources`) or with :ref:`guide_texture-bins-and-atlases`). |
| 136 |
A common pitfall is to use the :py:func:`pyglet.image.load` method to load |
| 137 |
a large number of images. This will cause a separate texture to be created |
| 138 |
for each image loaded, resulting in a lot of OpenGL texture binding overhead |
| 139 |
for each frame. |
| 140 |
|
| 141 |
Simple image blitting |
| 142 |
^^^^^^^^^^^^^^^^^^^^^ |
| 143 |
|
| 144 |
Drawing images directly is less efficient, but may be adequate for |
| 145 |
simple cases. Images can be drawn into a window with the |
| 146 |
:py:meth:`~pyglet.image.AbstractImage.blit` method:: |
| 147 |
|
| 148 |
@window.event |
| 149 |
def on_draw(): |
| 150 |
window.clear() |
| 151 |
image.blit(x, y) |
| 152 |
|
| 153 |
The `x` and `y` coordinates locate where to draw the anchor point of the |
| 154 |
image. For example, to center the image at ``(x, y)``:: |
| 155 |
|
| 156 |
kitten.anchor_x = kitten.width // 2 |
| 157 |
kitten.anchor_y = kitten.height // 2 |
| 158 |
kitten.blit(x, y) |
| 159 |
|
| 160 |
You can also specify an optional `z` component to the |
| 161 |
:py:meth:`~pyglet.image.AbstractImage.blit` method. |
| 162 |
This has no effect unless you have enabled depth testing. In the following example, |
| 163 |
the second image is drawn *behind* the first, even though it is drawn after it:: |
| 164 |
|
| 165 |
from pyglet.gl import * |
| 166 |
glEnable(GL_DEPTH_TEST) |
| 167 |
|
| 168 |
kitten.blit(x, y, 0) |
| 169 |
kitten.blit(x, y, -0.5) |
| 170 |
|
| 171 |
The default pyglet projection has a depth range of (-8192, 8192) -- images drawn |
| 172 |
with a z value outside this range will not be visible, regardless of whether |
| 173 |
depth testing is enabled or not. (You can create your own Window projection matrix |
| 174 |
if you have specific needs). |
| 175 |
|
| 176 |
Images with an alpha channel can be blended with the existing framebuffer. To |
| 177 |
do this you need to supply OpenGL with a blend equation. The following code |
| 178 |
fragment implements the most common form of alpha blending, however other |
| 179 |
techniques are also possible:: |
| 180 |
|
| 181 |
from pyglet.gl import * |
| 182 |
glEnable(GL_BLEND) |
| 183 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) |
| 184 |
|
| 185 |
You would only need to call the code above once during your program, before |
| 186 |
you draw any images (this is not necessary when using only sprites). |
| 187 |
|
| 188 |
Supported image decoders |
| 189 |
------------------------ |
| 190 |
The following table shows which codecs are available in pyglet. |
| 191 |
|
| 192 |
.. list-table:: |
| 193 |
:header-rows: 1 |
| 194 |
|
| 195 |
* - Module |
| 196 |
- Class |
| 197 |
- Description |
| 198 |
* - ``pyglet.image.codecs.dds`` |
| 199 |
- ``DDSImageDecoder`` |
| 200 |
- Reads Microsoft DirectDraw Surface files containing compressed |
| 201 |
textures |
| 202 |
* - ``pyglet.image.codecs.wic`` |
| 203 |
- ``WICDecoder`` |
| 204 |
- Uses Windows Imaging Component services to decode images. |
| 205 |
* - ``pyglet.image.codecs.gdiplus`` |
| 206 |
- ``GDIPlusDecoder`` |
| 207 |
- Uses Windows GDI+ services to decode images. |
| 208 |
* - ``pyglet.image.codecs.gdkpixbuf2`` |
| 209 |
- ``GdkPixbuf2ImageDecoder`` |
| 210 |
- Uses the GTK-2.0 GDK functions to decode images. |
| 211 |
* - ``pyglet.image.codecs.pil`` |
| 212 |
- ``PILImageDecoder`` |
| 213 |
- Wrapper interface around PIL Image class. |
| 214 |
* - ``pyglet.image.codecs.quicktime`` |
| 215 |
- ``QuickTimeImageDecoder`` |
| 216 |
- Uses Mac OS X QuickTime to decode images. |
| 217 |
* - ``pyglet.image.codecs.png`` |
| 218 |
- ``PNGImageDecoder`` |
| 219 |
- PNG decoder written in pure Python. |
| 220 |
* - ``pyglet.image.codecs.bmp`` |
| 221 |
- ``BMPImageDecoder`` |
| 222 |
- BMP decoder written in pure Python. |
| 223 |
|
| 224 |
Each of these classes registers itself with :py:mod:`pyglet.image` with |
| 225 |
the filename extensions it supports. The :py:func:`~pyglet.image.load` |
| 226 |
function will try each image decoder with a matching file extension first, |
| 227 |
before attempting the other decoders. Only if every image decoder fails |
| 228 |
to load an image will :py:class:`~pyglet.image.codecs.ImageDecodeException` |
| 229 |
be raised (the origin of the exception will be the first decoder that |
| 230 |
was attempted). |
| 231 |
|
| 232 |
You can override this behaviour and specify a particular decoding instance to |
| 233 |
use. For example, in the following example the pure Python PNG decoder is |
| 234 |
always used rather than the operating system's decoder:: |
| 235 |
|
| 236 |
from pyglet.image.codecs.png import PNGImageDecoder |
| 237 |
kitten = pyglet.image.load('kitten.png', decoder=PNGImageDecoder()) |
| 238 |
|
| 239 |
This use is not recommended unless your application has to work around |
| 240 |
specific deficiences in an operating system decoder. |
| 241 |
|
| 242 |
Supported image formats |
| 243 |
----------------------- |
| 244 |
|
| 245 |
The following table lists the image formats that can be loaded on each |
| 246 |
operating system. If Pillow is installed, any additional formats it |
| 247 |
supports can also be read. See the `Pillow docs`_ for a list of such |
| 248 |
formats. |
| 249 |
|
| 250 |
.. _Pillow docs: http://pillow.readthedocs.io/ |
| 251 |
|
| 252 |
.. list-table:: |
| 253 |
:header-rows: 1 |
| 254 |
|
| 255 |
* - Extension |
| 256 |
- Description |
| 257 |
- Windows |
| 258 |
- Mac OS X |
| 259 |
- Linux [#linux]_ |
| 260 |
* - ``.bmp`` |
| 261 |
- Windows Bitmap |
| 262 |
- X |
| 263 |
- X |
| 264 |
- X |
| 265 |
* - ``.dds`` |
| 266 |
- Microsoft DirectDraw Surface [#dds]_ |
| 267 |
- X |
| 268 |
- X |
| 269 |
- X |
| 270 |
* - ``.exif`` |
| 271 |
- Exif |
| 272 |
- X |
| 273 |
- |
| 274 |
- |
| 275 |
* - ``.gif`` |
| 276 |
- Graphics Interchange Format |
| 277 |
- X |
| 278 |
- X |
| 279 |
- X |
| 280 |
* - ``.jpg .jpeg`` |
| 281 |
- JPEG/JIFF Image |
| 282 |
- X |
| 283 |
- X |
| 284 |
- X |
| 285 |
* - ``.jp2 .jpx`` |
| 286 |
- JPEG 2000 |
| 287 |
- |
| 288 |
- X |
| 289 |
- |
| 290 |
* - ``.pcx`` |
| 291 |
- PC Paintbrush Bitmap Graphic |
| 292 |
- |
| 293 |
- X |
| 294 |
- |
| 295 |
* - ``.png`` |
| 296 |
- Portable Network Graphic |
| 297 |
- X |
| 298 |
- X |
| 299 |
- X |
| 300 |
* - ``.pnm`` |
| 301 |
- PBM Portable Any Map Graphic Bitmap |
| 302 |
- |
| 303 |
- |
| 304 |
- X |
| 305 |
* - ``.ras`` |
| 306 |
- Sun raster graphic |
| 307 |
- |
| 308 |
- |
| 309 |
- X |
| 310 |
* - ``.tga`` |
| 311 |
- Truevision Targa Graphic |
| 312 |
- |
| 313 |
- X |
| 314 |
- |
| 315 |
* - ``.tif .tiff`` |
| 316 |
- Tagged Image File Format |
| 317 |
- X |
| 318 |
- X |
| 319 |
- X |
| 320 |
* - ``.xbm`` |
| 321 |
- X11 bitmap |
| 322 |
- |
| 323 |
- X |
| 324 |
- X |
| 325 |
* - ``.xpm`` |
| 326 |
- X11 icon |
| 327 |
- |
| 328 |
- X |
| 329 |
- X |
| 330 |
|
| 331 |
The only supported save format is PNG, unless PIL is installed, in which case |
| 332 |
any format it supports can be written. |
| 333 |
|
| 334 |
.. [#linux] Requires GTK 2.0 or later. |
| 335 |
|
| 336 |
.. [#dds] Only S3TC compressed surfaces are supported. Depth, volume and cube |
| 337 |
textures are not supported. |
| 338 |
|
| 339 |
Working with images |
| 340 |
------------------- |
| 341 |
|
| 342 |
The :py:func:`pyglet.image.load` function returns an |
| 343 |
:py:class:`~pyglet.image.AbstractImage`. The actual class of the object depends |
| 344 |
on the decoder that was used, but all loaded imageswill have the following |
| 345 |
attributes: |
| 346 |
|
| 347 |
`width` |
| 348 |
The width of the image, in pixels. |
| 349 |
`height` |
| 350 |
The height of the image, in pixels. |
| 351 |
`anchor_x` |
| 352 |
Distance of the anchor point from the left edge of the image, in pixels |
| 353 |
`anchor_y` |
| 354 |
Distance of the anchor point from the bottom edge of the image, in pixels |
| 355 |
|
| 356 |
The anchor point defaults to (0, 0), though some image formats may contain an |
| 357 |
intrinsic anchor point. The anchor point is used to align the image to a |
| 358 |
point in space when drawing it. |
| 359 |
|
| 360 |
You may only want to use a portion of the complete image. You can use the |
| 361 |
:py:meth:`~pyglet.image.AbstractImage.get_region` method to return an image |
| 362 |
of a rectangular region of a source image:: |
| 363 |
|
| 364 |
image_part = kitten.get_region(x=10, y=10, width=100, height=100) |
| 365 |
|
| 366 |
This returns an image with dimensions 100x100. The region extracted from |
| 367 |
`kitten` is aligned such that the bottom-left corner of the rectangle is 10 |
| 368 |
pixels from the left and 10 pixels from the bottom of the image. |
| 369 |
|
| 370 |
Image regions can be used as if they were complete images. Note that changes |
| 371 |
to an image region may or may not be reflected on the source image, and |
| 372 |
changes to the source image may or may not be reflected on any region images. |
| 373 |
You should not assume either behaviour. |
| 374 |
|
| 375 |
The AbstractImage hierarchy |
| 376 |
--------------------------- |
| 377 |
|
| 378 |
The following sections deal with the various concrete image classes. All |
| 379 |
images subclass :py:class:`~pyglet.image.AbstractImage`, which provides |
| 380 |
the basic interface described in previous sections. |
| 381 |
|
| 382 |
.. figure:: img/abstract_image.png |
| 383 |
|
| 384 |
The :py:class:`~pyglet.image.AbstractImage` class hierarchy. |
| 385 |
|
| 386 |
An image of any class can be converted into a :py:class:`~pyglet.image.Texture` |
| 387 |
or :py:class:`~pyglet.image.ImageData` using the |
| 388 |
:py:meth:`~pyglet.image.AbstractImage.get_texture` and |
| 389 |
:py:meth:`~pyglet.image.ImageData.get_image_data` methods defined on |
| 390 |
:py:class:`~pyglet.image.AbstractImage`. For example, to load an image |
| 391 |
and work with it as an OpenGL texture:: |
| 392 |
|
| 393 |
kitten = pyglet.image.load('kitten.png').get_texture() |
| 394 |
|
| 395 |
There is no penalty for accessing one of these methods if object is already |
| 396 |
of the requested class. The following table shows how concrete classes are |
| 397 |
converted into other classes: |
| 398 |
|
| 399 |
.. list-table:: |
| 400 |
:header-rows: 1 |
| 401 |
:stub-columns: 1 |
| 402 |
|
| 403 |
* - Original class |
| 404 |
- ``.get_texture()`` |
| 405 |
- ``.get_image_data()`` |
| 406 |
* - :py:class:`~pyglet.image.Texture` |
| 407 |
- No change |
| 408 |
- ``glGetTexImage2D`` |
| 409 |
* - :py:class:`~pyglet.image.TextureRegion` |
| 410 |
- No change |
| 411 |
- ``glGetTexImage2D``, crop resulting image. |
| 412 |
* - :py:class:`~pyglet.image.ImageData` |
| 413 |
- ``glTexImage2D`` [1]_ |
| 414 |
- No change |
| 415 |
* - :py:class:`~pyglet.image.ImageDataRegion` |
| 416 |
- ``glTexImage2D`` [1]_ |
| 417 |
- No change |
| 418 |
* - :py:class:`~pyglet.image.CompressedImageData` |
| 419 |
- ``glCompressedTexImage2D`` [2]_ |
| 420 |
- N/A [3]_ |
| 421 |
* - :py:class:`~pyglet.image.BufferImage` |
| 422 |
- ``glCopyTexSubImage2D`` [4]_ |
| 423 |
- ``glReadPixels`` |
| 424 |
|
| 425 |
You should try to avoid conversions which use ``glGetTexImage2D`` or |
| 426 |
``glReadPixels``, as these can impose a substantial performance penalty by |
| 427 |
transferring data in the "wrong" direction of the video bus, especially on |
| 428 |
older hardware. |
| 429 |
|
| 430 |
.. [1] :py:class:`~pyglet.image.ImageData` caches the texture for future use, so there is no |
| 431 |
performance penalty for repeatedly blitting an |
| 432 |
:py:class:`~pyglet.image.ImageData`. |
| 433 |
|
| 434 |
.. [2] If the required texture compression extension is not present, the |
| 435 |
image is decompressed in memory and then supplied to OpenGL via |
| 436 |
``glTexImage2D``. |
| 437 |
|
| 438 |
.. [3] It is not currently possible to retrieve :py:class:`~pyglet.image.ImageData` for compressed |
| 439 |
texture images. This feature may be implemented in a future release |
| 440 |
of pyglet. One workaround is to create a texture from the compressed |
| 441 |
image, then read the image data from the texture; i.e., |
| 442 |
``compressed_image.get_texture().get_image_data()``. |
| 443 |
|
| 444 |
.. [4] :py:class:`~pyglet.image.BufferImageMask` cannot be converted to |
| 445 |
:py:class:`~pyglet.image.Texture`. |
| 446 |
|
| 447 |
Accessing or providing pixel data |
| 448 |
--------------------------------- |
| 449 |
|
| 450 |
The :py:class:`~pyglet.image.ImageData` class represents an image as a string |
| 451 |
or sequence of pixel data, or as a ctypes pointer. Details such as the pitch |
| 452 |
and component layout are also stored in the class. You can access an |
| 453 |
:py:class:`~pyglet.image.ImageData` object for any image with |
| 454 |
:py:meth:`~pyglet.image.ImageData.get_image_data`:: |
| 455 |
|
| 456 |
kitten = pyglet.image.load('kitten.png').get_image_data() |
| 457 |
|
| 458 |
The design of :py:class:`~pyglet.image.ImageData` is to allow applications |
| 459 |
to access the detail in the format they prefer, rather than having to |
| 460 |
understand the many formats that each operating system and OpenGL make use of. |
| 461 |
|
| 462 |
The `pitch` and `format` properties determine how the bytes are arranged. |
| 463 |
`pitch` gives the number of bytes between each consecutive row. The data is |
| 464 |
assumed to run from left-to-right, bottom-to-top, unless `pitch` is negative, |
| 465 |
in which case it runs from left-to-right, top-to-bottom. There is no need for |
| 466 |
rows to be tightly packed; larger `pitch` values are often used to align each |
| 467 |
row to machine word boundaries. |
| 468 |
|
| 469 |
The `format` property gives the number and order of color components. It is a |
| 470 |
string of one or more of the letters corresponding to the components in the |
| 471 |
following table: |
| 472 |
|
| 473 |
= ============ |
| 474 |
R Red |
| 475 |
G Green |
| 476 |
B Blue |
| 477 |
A Alpha |
| 478 |
L Luminance |
| 479 |
I Intensity |
| 480 |
= ============ |
| 481 |
|
| 482 |
For example, a format string of ``"RGBA"`` corresponds to four bytes of |
| 483 |
color data, in the order red, green, blue, alpha. Note that machine |
| 484 |
endianness has no impact on the interpretation of a format string. |
| 485 |
|
| 486 |
The length of a format string always gives the number of bytes per pixel. So, |
| 487 |
the minimum absolute pitch for a given image is ``len(kitten.format) * |
| 488 |
kitten.width``. |
| 489 |
|
| 490 |
To retrieve pixel data in a particular format, use the `get_data` method, |
| 491 |
specifying the desired format and pitch. The following example reads tightly |
| 492 |
packed rows in ``RGB`` format (the alpha component, if any, will be |
| 493 |
discarded):: |
| 494 |
|
| 495 |
kitten = kitten.get_image_data() |
| 496 |
data = kitten.get_data('RGB', kitten.width * 3) |
| 497 |
|
| 498 |
`data` always returns a string, however pixel data can be set from a |
| 499 |
ctypes array, stdlib array, list of byte data, string, or ctypes pointer. |
| 500 |
To set the image data use `set_data`, again specifying the format and pitch:: |
| 501 |
|
| 502 |
kitten.set_data('RGB', kitten.width * 3, data) |
| 503 |
|
| 504 |
You can also create :py:class:`~pyglet.image.ImageData` directly, by providing |
| 505 |
each of these attributes to the constructor. This is any easy way to load |
| 506 |
textures into OpenGL from other programs or libraries. |
| 507 |
|
| 508 |
Performance concerns |
| 509 |
^^^^^^^^^^^^^^^^^^^^ |
| 510 |
|
| 511 |
pyglet can use several methods to transform pixel data from one format to |
| 512 |
another. It will always try to select the most efficient means. For example, |
| 513 |
when providing texture data to OpenGL, the following possibilities are |
| 514 |
examined in order: |
| 515 |
|
| 516 |
1. Can the data be provided directly using a built-in OpenGL pixel format such |
| 517 |
as ``GL_RGB`` or ``GL_RGBA``? |
| 518 |
2. Is there an extension present that handles this pixel format? |
| 519 |
3. Can the data be transformed with a single regular expression? |
| 520 |
4. If none of the above are possible, the image will be split into separate |
| 521 |
scanlines and a regular expression replacement done on each; then the lines |
| 522 |
will be joined together again. |
| 523 |
|
| 524 |
The following table shows which image formats can be used directly with steps |
| 525 |
1 and 2 above, as long as the image rows are tightly packed (that is, the |
| 526 |
pitch is equal to the width times the number of components). |
| 527 |
|
| 528 |
.. list-table:: |
| 529 |
:header-rows: 1 |
| 530 |
|
| 531 |
* - Format |
| 532 |
- Required extensions |
| 533 |
* - ``"I"`` |
| 534 |
- |
| 535 |
* - ``"L"`` |
| 536 |
- |
| 537 |
* - ``"LA"`` |
| 538 |
- |
| 539 |
* - ``"R"`` |
| 540 |
- |
| 541 |
* - ``"G"`` |
| 542 |
- |
| 543 |
* - ``"B"`` |
| 544 |
- |
| 545 |
* - ``"A"`` |
| 546 |
- |
| 547 |
* - ``"RGB"`` |
| 548 |
- |
| 549 |
* - ``"RGBA"`` |
| 550 |
- |
| 551 |
* - ``"ARGB"`` |
| 552 |
- ``GL_EXT_bgra`` and ``GL_APPLE_packed_pixels`` |
| 553 |
* - ``"ABGR"`` |
| 554 |
- ``GL_EXT_abgr`` |
| 555 |
* - ``"BGR"`` |
| 556 |
- ``GL_EXT_bgra`` |
| 557 |
* - ``"BGRA"`` |
| 558 |
- ``GL_EXT_bgra`` |
| 559 |
|
| 560 |
If the image data is not in one of these formats, a regular expression will be |
| 561 |
constructed to pull it into one. If the rows are not tightly packed, or if |
| 562 |
the image is ordered from top-to-bottom, the rows will be split before the |
| 563 |
regular expression is applied. Each of these may incur a performance penalty |
| 564 |
-- you should avoid such formats for real-time texture updates if possible. |
| 565 |
|
| 566 |
Image sequences and atlases |
| 567 |
--------------------------- |
| 568 |
|
| 569 |
Sometimes a single image is used to hold several images. For example, a |
| 570 |
"sprite sheet" is an image that contains each animation frame required for a |
| 571 |
character sprite animation. |
| 572 |
|
| 573 |
pyglet provides convenience classes for extracting the individual images from |
| 574 |
such a composite image as if it were a simple Python sequence. Discrete |
| 575 |
images can also be packed into one or more larger textures with texture bins |
| 576 |
and atlases. |
| 577 |
|
| 578 |
.. figure:: img/image_sequence.png |
| 579 |
|
| 580 |
The AbstractImageSequence class hierarchy. |
| 581 |
|
| 582 |
Image grids |
| 583 |
^^^^^^^^^^^ |
| 584 |
|
| 585 |
An "image grid" is a single image which is divided into several smaller images |
| 586 |
by drawing an imaginary grid over it. The following image shows an image that |
| 587 |
can be used for an asteroid explosion animation. |
| 588 |
|
| 589 |
.. figure:: img/explosion.png |
| 590 |
|
| 591 |
An image consisting of eight animation frames arranged in a grid. |
| 592 |
|
| 593 |
This image has one row and eight columns. This is all the information you |
| 594 |
need to create an :py:class:`~pyglet.image.ImageGrid` with:: |
| 595 |
|
| 596 |
explosion = pyglet.image.load('explosion.png') |
| 597 |
explosion_seq = pyglet.image.ImageGrid(explosion, 1, 8) |
| 598 |
|
| 599 |
The images within the grid can now be accessed as if they were their own |
| 600 |
images:: |
| 601 |
|
| 602 |
frame_1 = explosion_seq[0] |
| 603 |
frame_2 = explosion_seq[1] |
| 604 |
|
| 605 |
Images with more than one row can be accessed either as a single-dimensional |
| 606 |
sequence, or as a (row, column) tuple; as shown in the following diagram. |
| 607 |
|
| 608 |
.. figure:: img/image_grid.png |
| 609 |
|
| 610 |
An image grid with several rows and columns, and the slices that can be |
| 611 |
used to access it. |
| 612 |
|
| 613 |
Image sequences can be sliced like any other sequence in Python. For example, |
| 614 |
the following obtains the first four frames in the animation:: |
| 615 |
|
| 616 |
start_frames = explosion_seq[:4] |
| 617 |
|
| 618 |
For efficient rendering, you should use a |
| 619 |
:py:class:`~pyglet.image.TextureGrid`. |
| 620 |
This uses a single texture for the grid, and each individual image returned |
| 621 |
from a slice will be a :py:class:`~pyglet.image.TextureRegion`:: |
| 622 |
|
| 623 |
explosion_tex_seq = image.TextureGrid(explosion_seq) |
| 624 |
|
| 625 |
Because :py:class:`~pyglet.image.TextureGrid` is also a |
| 626 |
:py:class:`~pyglet.image.Texture`, you can use it either as individual images |
| 627 |
or as the whole grid at once. |
| 628 |
|
| 629 |
3D textures |
| 630 |
^^^^^^^^^^^ |
| 631 |
|
| 632 |
:py:class:`~pyglet.image.TextureGrid` is extremely efficient for drawing many |
| 633 |
sprites from a single texture. One problem you may encounter, however, |
| 634 |
is bleeding between adjacent images. |
| 635 |
|
| 636 |
When OpenGL renders a texture to the screen, by default it obtains each pixel |
| 637 |
color by interpolating nearby texels. You can disable this behaviour by |
| 638 |
switching to the ``GL_NEAREST`` interpolation mode, however you then lose the |
| 639 |
benefits of smooth scaling, distortion, rotation and sub-pixel positioning. |
| 640 |
|
| 641 |
You can alleviate the problem by always leaving a 1-pixel clear border around |
| 642 |
each image frame. This will not solve the problem if you are using |
| 643 |
mipmapping, however. At this stage you will need a 3D texture. |
| 644 |
|
| 645 |
You can create a 3D texture from any sequence of images, or from an |
| 646 |
:py:class:`~pyglet.image.ImageGrid`. The images must all be of the same |
| 647 |
dimension, however they need not be powers of two (pyglet takes care of |
| 648 |
this by returning :py:class:`~pyglet.image.TextureRegion` |
| 649 |
as with a regular :py:class:`~pyglet.image.Texture`). |
| 650 |
|
| 651 |
In the following example, the explosion texture from above is uploaded into a |
| 652 |
3D texture:: |
| 653 |
|
| 654 |
explosion_3d = pyglet.image.Texture3D.create_for_image_grid(explosion_seq) |
| 655 |
|
| 656 |
You could also have stored each image as a separate file and used |
| 657 |
:py:meth:`pyglet.image.Texture3D.create_for_images` to create the 3D texture. |
| 658 |
|
| 659 |
Once created, a 3D texture behaves like any other |
| 660 |
:py:class:`~pyglet.image.AbstractImageSequence`; slices return |
| 661 |
:py:class:`~pyglet.image.TextureRegion` for an image plane within the texture. |
| 662 |
Unlike a :py:class:`~pyglet.image.TextureGrid`, though, you cannot blit a |
| 663 |
:py:class:`~pyglet.image.Texture3D` in its entirety. |
| 664 |
|
| 665 |
.. _guide_texture-bins-and-atlases: |
| 666 |
|
| 667 |
Texture bins and atlases |
| 668 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 669 |
|
| 670 |
Image grids are useful when the artist has good tools to construct the larger |
| 671 |
images of the appropriate format, and the contained images all have the same |
| 672 |
size. However it is often simpler to keep individual images as separate files |
| 673 |
on disk, and only combine them into larger textures at runtime for efficiency. |
| 674 |
|
| 675 |
A :py:class:`~pyglet.image.atlas.TextureAtlas` is initially an empty texture, |
| 676 |
but images of any size can be added to it at any time. The atlas takes care |
| 677 |
of tracking the "free" areas within the texture, and of placing images at |
| 678 |
appropriate locations within the texture to avoid overlap. |
| 679 |
|
| 680 |
It's possible for a :py:class:`~pyglet.image.atlas.TextureAtlas` to run out |
| 681 |
of space for new images, so applications will need to either know the correct |
| 682 |
size of the texture to allocate initially, or maintain multiple atlases as |
| 683 |
each one fills up. |
| 684 |
|
| 685 |
The :py:class:`~pyglet.image.atlas.TextureBin` class provides a simple means |
| 686 |
to manage multiple atlases. The following example loads a list of images, |
| 687 |
then inserts those images into a texture bin. The resulting list is a list of |
| 688 |
:py:class:`~pyglet.image.TextureRegion` images that map |
| 689 |
into the larger shared texture atlases:: |
| 690 |
|
| 691 |
images = [ |
| 692 |
pyglet.image.load('img1.png'), |
| 693 |
pyglet.image.load('img2.png'), |
| 694 |
# ... |
| 695 |
] |
| 696 |
|
| 697 |
bin = pyglet.image.atlas.TextureBin() |
| 698 |
images = [bin.add(image) for image in images] |
| 699 |
|
| 700 |
The :py:mod:`pyglet.resource` module (see :ref:`guide_resources`) uses |
| 701 |
texture bins internally to efficiently pack images automatically. |
| 702 |
|
| 703 |
Animations |
| 704 |
---------- |
| 705 |
|
| 706 |
While image sequences and atlases provide storage for related images, |
| 707 |
they alone are not enough to describe a complete animation. |
| 708 |
|
| 709 |
The :py:class:`~pyglet.image.Animation` class manages a list of |
| 710 |
:py:class:`~pyglet.image.AnimationFrame` objects, each of |
| 711 |
which references an image and a duration (in seconds). The storage of |
| 712 |
the images is up to the application developer: they can each be discrete, or |
| 713 |
packed into a texture atlas, or any other technique. |
| 714 |
|
| 715 |
An animation can be loaded directly from a GIF 89a image file with |
| 716 |
:py:func:`~pyglet.image.load_animation` (supported on Linux, Mac OS X |
| 717 |
and Windows) or constructed manually from a list of images or an image |
| 718 |
sequence using the class methods (in which case the timing information |
| 719 |
will also need to be provided). |
| 720 |
The :py:func:`~pyglet.image.Animation.add_to_texture_bin` method provides |
| 721 |
a convenient way to pack the image frames into a texture bin for efficient |
| 722 |
access. |
| 723 |
|
| 724 |
Individual frames can be accessed by the application for use with any kind of |
| 725 |
rendering, or the entire animation can be used directly with a |
| 726 |
:py:class:`~pyglet.sprite.Sprite` (see next section). |
| 727 |
|
| 728 |
The following example loads a GIF animation and packs the images in that |
| 729 |
animation into a texture bin. A sprite is used to display the animation in |
| 730 |
the window:: |
| 731 |
|
| 732 |
window = pyglet.window.Window() |
| 733 |
|
| 734 |
animation = pyglet.image.load_animation('animation.gif') |
| 735 |
bin = pyglet.image.atlas.TextureBin() |
| 736 |
animation.add_to_texture_bin(bin) |
| 737 |
sprite = pyglet.sprite.Sprite(img=animation) |
| 738 |
|
| 739 |
@window.event |
| 740 |
def on_draw(): |
| 741 |
window.clear() |
| 742 |
sprite.draw() |
| 743 |
|
| 744 |
pyglet.app.run() |
| 745 |
|
| 746 |
When animations are loaded with :py:mod:`pyglet.resource` (see |
| 747 |
:ref:`guide_resources`) the frames are automatically packed into a texture bin. |
| 748 |
|
| 749 |
The ``examples/programming_guide/`` folder of the `GitHub repository`_ |
| 750 |
includes: |
| 751 |
|
| 752 |
* this example program (``animation.py``) |
| 753 |
* a sample GIF animation file (``dinosaur.gif``) |
| 754 |
|
| 755 |
.. _GitHub repository: https://github.com/pyglet/pyglet/ |
| 756 |
|
| 757 |
|
| 758 |
Framebuffers |
| 759 |
------------ |
| 760 |
To simplify working with framebuffers, pyglet provides the |
| 761 |
:py:class:`~pyglet.image.FrameBuffer` and :py:class:`~pyglet.image.RenderBuffer` |
| 762 |
classes. These work as you would expect, and allow a simple way to add texture |
| 763 |
attachments. Attachment and target types can be specified as :: |
| 764 |
|
| 765 |
from pyglet.gl import * |
| 766 |
|
| 767 |
# Prepare the buffers. One texture (for easy access), and one Renderbuffer: |
| 768 |
color_buffer = pyglet.image.Texture.create(width, height, min_filter=GL_NEAREST, mag_filter=GL_NEAREST) |
| 769 |
depth_buffer = pyglet.image.Renderbuffer(width, height, GL_DEPTH_COMPONENT) |
| 770 |
|
| 771 |
# Create a Framebuffer, and attach: |
| 772 |
framebuffer = pyglet.image.Framebuffer() |
| 773 |
framebuffer.attach_texture(color_buffer, attachment=GL_COLOR_ATTACHMENT0) |
| 774 |
framebuffer.attach_renderbuffer(depth_buffer, attachment=GL_DEPTH_ATTACHMENT) |
| 775 |
|
| 776 |
# When drawing: |
| 777 |
framebuffer.bind() |
| 778 |
|
| 779 |
|
| 780 |
pyglet also provides a simple abstraction over the "default" framebuffer, |
| 781 |
as components of the :py:class:`~pyglet.image.AbstractImage` hierarchy. |
| 782 |
|
| 783 |
.. figure:: img/buffer_image.png |
| 784 |
|
| 785 |
The :py:class:`~pyglet.image.BufferImage` hierarchy. |
| 786 |
|
| 787 |
* One or more color buffers, represented by |
| 788 |
:py:class:`~pyglet.image.ColorBufferImage` |
| 789 |
* An optional depth buffer, represented by |
| 790 |
:py:class:`~pyglet.image.DepthBufferImage` |
| 791 |
* An optional stencil buffer, with each bit represented by |
| 792 |
:py:class:`~pyglet.image.BufferImageMask` |
| 793 |
|
| 794 |
You cannot create the buffer images directly; instead you must obtain |
| 795 |
instances via the :py:class:`~pyglet.image.BufferManager`. |
| 796 |
Use :py:func:`~pyglet.image.get_buffer_manager` to get this singleton:: |
| 797 |
|
| 798 |
buffers = image.get_buffer_manager() |
| 799 |
|
| 800 |
Only the back-left color buffer can be obtained (i.e., the front buffer is |
| 801 |
inaccessible, and stereo contexts are not supported by the buffer manager):: |
| 802 |
|
| 803 |
color_buffer = buffers.get_color_buffer() |
| 804 |
|
| 805 |
This buffer can be treated like any other image. For example, you could copy |
| 806 |
it to a texture, obtain its pixel data, save it to a file, and so on. This can |
| 807 |
be useful if you want to save a "screen shot" of the running application:: |
| 808 |
|
| 809 |
image_data = color_buffer.get_image_data() |
| 810 |
image_data.save("screenshot.png") |
| 811 |
|
| 812 |
The depth buffer can be obtained similarly:: |
| 813 |
|
| 814 |
depth_buffer = buffers.get_depth_buffer() |
| 815 |
|
| 816 |
|
| 817 |
The auxiliary buffers and stencil bits are obtained by requesting one, which |
| 818 |
will then be marked as "in-use". This permits multiple libraries and your |
| 819 |
application to work together without clashes in stencil bits or auxiliary |
| 820 |
buffer names. For example, to obtain a free stencil bit:: |
| 821 |
|
| 822 |
mask = buffers.get_buffer_mask() |
| 823 |
|
| 824 |
The buffer manager maintains a weak reference to the buffer mask, so that when |
| 825 |
you release all references to it, it will be returned to the pool of available |
| 826 |
masks. |
| 827 |
|
| 828 |
Similarly, a free auxiliary buffer is obtained:: |
| 829 |
|
| 830 |
aux_buffer = buffers.get_aux_buffer() |
| 831 |
|
| 832 |
When using the stencil or auxiliary buffers, make sure you explicitly request |
| 833 |
these when creating the window. See `OpenGL configuration options` for |
| 834 |
details. |
| 835 |
|
| 836 |
OpenGL imaging |
| 837 |
-------------- |
| 838 |
|
| 839 |
This section assumes you are familiar with texture mapping in OpenGL (for |
| 840 |
example, chapter 9 of the `OpenGL Programming Guide`_). |
| 841 |
|
| 842 |
To create a texture from any :py:class:`~pyglet.image.AbstractImage`, |
| 843 |
call :py:meth:`~pyglet.image.AbstractImage.get_texture`:: |
| 844 |
|
| 845 |
kitten = image.load('kitten.jpg') |
| 846 |
texture = kitten.get_texture() |
| 847 |
|
| 848 |
Textures are automatically created and used by |
| 849 |
:py:class:`~pyglet.image.ImageData` when blitted. Itis useful to use |
| 850 |
textures directly when aiming for high performance or 3D applications. |
| 851 |
|
| 852 |
The :py:class:`~pyglet.image.Texture` class represents any texture object. |
| 853 |
The :py:attr:`~pyglet.image.TextureRegion.target` attribute gives the |
| 854 |
texture target (for example, ``GL_TEXTURE_2D``) and |
| 855 |
:py:attr:`~pyglet.image.TextureRegion.id` the texturename. |
| 856 |
For example, to bind a texture:: |
| 857 |
|
| 858 |
glBindTexture(texture.target, texture.id) |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
.. _OpenGL Programming Guide: http://www.opengl-redbook.com/ |
| 863 |
|
| 864 |
Texture dimensions |
| 865 |
^^^^^^^^^^^^^^^^^^ |
| 866 |
|
| 867 |
Implementations of OpenGL prior to 2.0 require textures to have dimensions |
| 868 |
that are powers of two (i.e., 1, 2, 4, 8, 16, ...). Because of this |
| 869 |
restriction, pyglet will always create textures of these dimensions (there are |
| 870 |
several non-conformant post-2.0 implementations). This could have unexpected |
| 871 |
results for a user blitting a texture loaded from a file of non-standard |
| 872 |
dimensions. To remedy this, pyglet returns a |
| 873 |
:py:class:`~pyglet.image.TextureRegion` of the larger |
| 874 |
texture corresponding to just the part of the texture covered by the original |
| 875 |
image. |
| 876 |
|
| 877 |
A :py:class:`~pyglet.image.TextureRegion` has an `owner` attribute that |
| 878 |
references the larger texture. The following session demonstrates this:: |
| 879 |
|
| 880 |
>>> rgba = image.load('tests/image/rgba.png') |
| 881 |
>>> rgba |
| 882 |
<ImageData 235x257> # The image is 235x257 |
| 883 |
>>> rgba.get_texture() |
| 884 |
<TextureRegion 235x257> # The returned texture is a region |
| 885 |
>>> rgba.get_texture().owner |
| 886 |
<Texture 256x512> # The owning texture has power-2 dimensions |
| 887 |
>>> |
| 888 |
|
| 889 |
A :py:class:`~pyglet.image.TextureRegion` defines a |
| 890 |
:py:attr:`~pyglet.image.TextureRegion.tex_coords` attribute that gives |
| 891 |
the texture coordinates to use for a quad mapping the whole image. |
| 892 |
:py:attr:`~pyglet.image.TextureRegion.tex_coords` is a 4-tuple of 3-tuple |
| 893 |
of floats; i.e., each texture coordinate is given in 3 dimensions. |
| 894 |
The following code can be used to render a quad for a texture region:: |
| 895 |
|
| 896 |
texture = kitten.get_texture() |
| 897 |
t = texture.tex_coords |
| 898 |
w, h = texture.width, texture.height |
| 899 |
array = (GLfloat * 32)( |
| 900 |
t[0][0], t[0][1], t[0][2], 1., |
| 901 |
x, y, z, 1., |
| 902 |
t[1][0], t[1][1], t[1][2], 1., |
| 903 |
x + w, y, z, 1., |
| 904 |
t[2][0], t[2][1], t[2][2], 1., |
| 905 |
x + w, y + h, z, 1., |
| 906 |
t[3][0], t[3][1], t[3][2], 1., |
| 907 |
x, y + h, z, 1.) |
| 908 |
|
| 909 |
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) |
| 910 |
glInterleavedArrays(GL_T4F_V4F, 0, array) |
| 911 |
glDrawArrays(GL_QUADS, 0, 4) |
| 912 |
glPopClientAttrib() |
| 913 |
|
| 914 |
The :py:meth:`~pyglet.image.Texture.blit` method does this. |
| 915 |
|
| 916 |
Use the :py:meth:`pyglet.image.Texture.create` method to create |
| 917 |
either a texture region from a larger power-2 sized texture, |
| 918 |
or a texture with the exact dimensions using the |
| 919 |
``GL_texture_rectangle_ARB`` extension. |
| 920 |
|
| 921 |
Texture internal format |
| 922 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 923 |
|
| 924 |
pyglet automatically selects an internal format for the texture based on the |
| 925 |
source image's `format` attribute. The following table describes how it is |
| 926 |
selected. |
| 927 |
|
| 928 |
.. list-table:: |
| 929 |
:header-rows: 1 |
| 930 |
|
| 931 |
* - Format |
| 932 |
- Internal format |
| 933 |
* - Any format with 3 components |
| 934 |
- ``GL_RGB`` |
| 935 |
* - Any format with 2 components |
| 936 |
- ``GL_LUMINANCE_ALPHA`` |
| 937 |
* - ``"A"`` |
| 938 |
- ``GL_ALPHA`` |
| 939 |
* - ``"L"`` |
| 940 |
- ``GL_LUMINANCE`` |
| 941 |
* - ``"I"`` |
| 942 |
- ``GL_INTENSITY`` |
| 943 |
* - Any other format |
| 944 |
- ``GL_RGBA`` |
| 945 |
|
| 946 |
Note that this table does not imply any mapping between format components and |
| 947 |
their OpenGL counterparts. For example, an image with format ``"RG"`` will use |
| 948 |
``GL_LUMINANCE_ALPHA`` as its internal format; the luminance channel will be |
| 949 |
averaged from the red and green components, and the alpha channel will be |
| 950 |
empty (maximal). |
| 951 |
|
| 952 |
Use the :py:meth:`pyglet.image.Texture.create` class method to create a texture |
| 953 |
with a specific internal format. |
| 954 |
|
| 955 |
|
| 956 |
Texture filtering |
| 957 |
^^^^^^^^^^^^^^^^^ |
| 958 |
|
| 959 |
By default, all textures are created with smooth (:py:data:`~pyglet.gl.GL_LINEAR`) |
| 960 |
filtering. |
| 961 |
|
| 962 |
To use a different filter for a specific texture, pass the filtering constant(s) |
| 963 |
to the :py:attr:`pyglet.image.Texture` class via the ``min_filter`` and ``mag_filter`` |
| 964 |
arguments. |
| 965 |
|
| 966 |
Pixel art |
| 967 |
""""""""" |
| 968 |
|
| 969 |
To enable nearest-neighbor filtering for retro-style games, set the |
| 970 |
corresponding variables of :py:class:`pyglet.image.Texture` to |
| 971 |
:py:data:`~pyglet.gl.GL_NEAREST`: |
| 972 |
|
| 973 |
.. code-block:: python |
| 974 |
|
| 975 |
pyglet.image.Texture.default_min_filter = GL_LINEAR |
| 976 |
pyglet.image.Texture.default_mag_filter = GL_LINEAR |
| 977 |
|
| 978 |
Afterward, all textures pyglet creates will default |
| 979 |
to nearest-neighbor sampling. |
| 980 |
|
| 981 |
Saving an image |
| 982 |
--------------- |
| 983 |
|
| 984 |
Any image can be saved using the `save` method:: |
| 985 |
|
| 986 |
kitten.save('kitten.png') |
| 987 |
|
| 988 |
or, specifying a file-like object:: |
| 989 |
|
| 990 |
kitten_stream = open('kitten.png', 'wb') |
| 991 |
kitten.save('kitten.png', file=kitten_stream) |
| 992 |
|
| 993 |
The following example shows how to grab a screenshot of your application |
| 994 |
window:: |
| 995 |
|
| 996 |
pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png') |
| 997 |
|
| 998 |
Note that images can only be saved in the PNG format unless the Pillow library |
| 999 |
is installed. |