Quick search

Table Of Contents

Image

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

In-memory image loading

バージョン 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")
class kivy.core.image.Image(arg, **kwargs)[ソース]

ベースクラス: kivy.event.EventDispatcher

Load an image and store the size and texture.

バージョン 1.0.7 で変更: mipmap attribute has been added. The texture_mipmap and texture_rectangle have been deleted.

バージョン 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.

scale: float, defaults to 1.0

Scale of the image.

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.

anim_available

Return True if this Image instance has animation available.

バージョン 1.0.8 で追加.

anim_delay

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

バージョン 1.0.8 で追加.

anim_index

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

バージョン 1.0.8 で追加.

anim_reset(allow_anim)[ソース]

Reset an animation if available.

バージョン 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.
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, defaults to False

Keep the image data when the texture is created.

load_memory(data, ext, filename='__inline__')[ソース]

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

nocache

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

バージョン 1.6.0 で追加.

on_texture(*largs)[ソース]
This event is fired when the texture reference or content has
changed. It is normally used for sequenced images.

バージョン 1.0.8 で追加.

read_pixel(x, y)[ソース]

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

警告

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()[ソース]

Remove the Image from cache. This facilitates re-loading of images from disk in case the image content has changed.

バージョン 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)[ソース]

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')

バージョン 1.7.0 で追加.

バージョン 1.8.0 で変更: Parameter flipped added to flip the image before saving, default to False.

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, source_image=None, rowlength=0)[ソース]

ベースクラス: 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)[ソース]

Add a image for a specific mipmap level.

バージョン 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 it exists

バージョン 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.

バージョン 1.0.7 で追加.

mipmaps

Data for each mipmap.

rowlength

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

バージョン 1.9.0 で追加.

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)