Quick search

Table Of Contents

Image

Core classes for loading image and convert them to Texture. The raw image data can be keep in memory for further access.

Note

Saving image is not yet supported.

class kivy.core.image.Image(arg, **kwargs)

Bases: kivy.event.EventDispatcher

Load an image, and store the size and texture.

New in version In: 1.0.7, mipmap attribute has been added, texture_mipmap and texture_rectangle have been deleted.

New in version In: 1.0.8, an Image widget might change its texture. A new event ‘on_texture’ has been introduced. New methods for handling sequenced animation too.

Parameters :
arg : can be str or Texture or Image object

A string is interpreted as a path to the image to be loaded. You can also provide a texture object or an already existing image object. In the latter case, a real copy of the given image object will be returned.

keep_data : bool, default to False

Keep the image data when texture is created

scale : float, default to 1.0

Scale of the image

mipmap : bool, default to False

Create mipmap for the texture

anim_delay: float, default to .25

Delay in seconds between each animation frame. Lower means faster animation.

anim_available

Return True if this Image instance have animation available.

New in version 1.0.8.

anim_delay

Delay betwean each animation frame. Lower means faster animation.

New in version 1.0.8.

anim_index

Return the index number of the image currently in the texture

New in version 1.0.8.

anim_reset(allow_anim)

Reset an animation if available.

New in version 1.0.8.

Parameters :
allow_anim: bool

Indicate if the animation should restart playing or not.

Usage:

# start/reset animation
image.anim_reset(True)

# or stop the animation
image.anim_reset(False)

You can change the animation speed in live:

# Set to 20 FPS
image.anim_delay = 1 / 20.
filename

Get/set the filename of image

height

Image height

image

Get/set the data image object

static load(filename, **kwargs)

Load an image

Parameters :
filename : str

Filename of the image

keep_data : bool, default to False

Keep the image data when texture is created

nocache

Indicate if the texture will not be stored in the cache

New in version 1.6.0.

on_texture(*largs)

This event is fired when the texture reference or content have been changed. It’s actually used for sequenced images.

New in version 1.0.8.

read_pixel(x, y)

For a given local x/y position, return the color at that position.

Warning

This function can be used only with images loaded with keep_data=True keyword. For examples:

m = Image.load('image.png', keep_data=True)
color = m.read_pixel(150, 150)
Parameters :
x : int

Local x coordinate of the pixel in question.

y : int

Local y coordinate of the pixel in question.

remove_from_cache()

Remove the Image from cache. This facilitates re-loading of image from disk in case of contents having been changed.

New in version 1.3.0.

Usage:

im = CoreImage('1.jpg')
# -- do something --
im.remove_from_cache()
im = CoreImage('1.jpg')
# this time image will be re-loaded from disk
save(filename)

Save image texture to file.

The filename should have the ‘.png’ extension, because the texture data readed from the GPU is in the RGBA format. ‘.jpg’ can work, but not heavilly tested, and some providers might break when using it. Any other extensions is not officially supported.

Example:

# Save an core image object
from kivy.core.image import Image
img = Image('hello.png')
img.save('hello2.png')

# Save a texture
texture = Texture.create(...)
img = Image(texture)
img.save('hello3.png')

New in version 1.7.0.

size

Image size (width, height)

texture

Texture of the image

width

Image width

class kivy.core.image.ImageData(width, height, fmt, data, source=None, flip_vertical=True)

Bases: object

Container for image and mipmap images. The container will always have at least the mipmap level 0.

add_mipmap(level, width, height, data)

Add a image for a specific mipmap level.

New in version 1.0.7.

data

Image data. (If the image is mipmapped, it will use the level 0)

flip_vertical

Indicate if the texture will need to be vertically flipped

fmt

Decoded image format, one of a available texture format

get_mipmap(level)

Get the mipmap image at a specific level if exist

New in version 1.0.7.

height

Image height in pixels. (If the image is mipmapped, it will use the level 0)

iterate_mipmaps()

Iterate over all mipmap images available

New in version 1.0.7.

mipmaps

Data for each mipmap.

size

Image (width, height) in pixels. (If the image is mipmapped, it will use the level 0)

source

Image source, if available

width

Image width in pixels. (If the image is mipmapped, it will use the level 0)