Version

Quick search

Image

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

Changed in version 1.11.0: Add support for argb and abgr image data

In-memory image loading

New in version 1.9.0: Official support for in-memory loading. Not all the providers support it, but currently SDL2, pygame, pil and imageio work.

To load an image with a filename, you would usually do:

from kivy.core.image import Image as CoreImage
im = CoreImage("image.png")

You can also load the image data directly from a memory block. Instead of passing the filename, you’ll need to pass the data as a BytesIO object together with an “ext” parameter. Both are mandatory:

import io
from kivy.core.image import Image as CoreImage
data = io.BytesIO(open("image.png", "rb").read())
im = CoreImage(data, ext="png")

By default, the image will not be cached as our internal cache requires a filename. If you want caching, add a filename that represents your file (it will be used only for caching):

import io
from kivy.core.image import Image as CoreImage
data = io.BytesIO(open("image.png", "rb").read())
im = CoreImage(data, ext="png", filename="image.png")

Saving an image

A CoreImage can be saved to a file:

from kivy.core.image import Image as CoreImage
image = CoreImage(...)
image.save("/tmp/test.png")

Or you can get the bytes (new in 1.11.0):

import io from kivy.core.image import Image as CoreImage data = io.BytesIO() image = CoreImage(…) image.save(data, fmt=”png”) png_bytes = data.read()

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

Bases: kivy.event.EventDispatcher

Load an image and store the size and texture.

Changed in version 1.0.7: mipmap attribute has been added. The texture_mipmap and texture_rectangle have been deleted.

Changed in version 1.0.8: An Image widget can change its texture. A new event ‘on_texture’ has been introduced. New methods for handling sequenced animation have been added.

Parameters
arg: can be a string (str), Texture, BytesIO or Image object

A string path to the image file or data URI to be loaded; or a Texture object, which will be wrapped in an Image object; or a BytesIO object containing raw image data; or an already existing image object, in which case, a real copy of the given image object will be returned.

keep_data: bool, defaults to False

Keep the image data when the texture is created.

mipmap: bool, defaults to False

Create mipmap for the texture.

anim_delay: float, defaults to .25

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

ext: str, only with BytesIO arg

File extension to use in determining how to load raw image data.

filename: str, only with BytesIO arg

Filename to use in the image cache for raw image data.

property anim_available

Return True if this Image instance has animation available.

New in version 1.0.8.

property anim_delay

Delay between each animation frame. A lower value means faster animation.

New in version 1.0.8.

property anim_index

Return the index number of the image currently in the texture.

New in version 1.0.8.

anim_reset(allow_anim)[source]

Reset an animation if available.

New in version 1.0.8.

Parameters
allow_anim: bool

Indicate whether 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 whilst it is playing:

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

Get/set the filename of image

property height

Image height

property image

Get/set the data image object

static load(filename, **kwargs)[source]

Load an image

Parameters
filename: str

Filename of the image.

keep_data: bool, defaults to False

Keep the image data when the texture is created.

load_memory(data, ext, filename='__inline__')[source]

(internal) Method to load an image from raw data.

property nocache

Indicate whether the texture will not be stored in the cache or not.

New in version 1.6.0.

on_texture(*largs)[source]
This event is fired when the texture reference or content has

changed. It is normally used for sequenced images.

New in version 1.0.8.

read_pixel(x, y)[source]

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

Warning

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

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()[source]

Remove the Image from cache. This facilitates re-loading of images from disk in case the image content has 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, flipped=False, fmt=None)[source]

Save image texture to file.

The filename should have the ‘.png’ extension because the texture data read from the GPU is in the RGBA format. ‘.jpg’ might work but has not been heavily tested so some providers might break when using it. Any other extensions are not officially supported.

The flipped parameter flips the saved image vertically, and defaults to False.

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.

Changed in version 1.8.0: Parameter flipped added to flip the image before saving, default to False.

Changed in version 1.11.0: Parameter fmt added to force the output format of the file Filename can now be a BytesIO object.

property size

Image size (width, height)

property texture

Texture of the image

property width

Image width

class kivy.core.image.ImageData(width, height, fmt, data, source=None, flip_vertical=True, source_image=None, rowlength=0)[source]

Bases: builtins.object

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

add_mipmap(level, width, height, data, rowlength)[source]

Add a image for a specific mipmap level.

New in version 1.0.7.

property 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)[source]

Get the mipmap image at a specific level if it exists

New in version 1.0.7.

property height

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

iterate_mipmaps()[source]

Iterate over all mipmap images available.

New in version 1.0.7.

mipmaps

Data for each mipmap.

property rowlength

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

New in version 1.9.0.

property size

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

source

Image source, if available

property width

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